Skip to content

Instantly share code, notes, and snippets.

@Kshitij-Dhakal
Created September 15, 2022 15:32
Show Gist options
  • Save Kshitij-Dhakal/1c23e4b9c947c6c9043395a84b092621 to your computer and use it in GitHub Desktop.
Save Kshitij-Dhakal/1c23e4b9c947c6c9043395a84b092621 to your computer and use it in GitHub Desktop.
package com.example.demo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import javax.jms.JMSException;
import javax.jms.TextMessage;
@Slf4j
@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class MqListener {
public static final String DESTINATION_NAME = "DESTINATION.NAME";
private final JmsTemplate jmsTemplate;
@Scheduled(fixedDelay = 1000)
public void onNewMessage() throws JMSException {
log.info("Fetching new messages");
String selectorExpression = String.format("JMSCorrelationId='ID:%s'", bytesToHex("correlation.id".getBytes())); // what is selector expression
TextMessage message = (TextMessage) jmsTemplate.receiveSelected(DESTINATION_NAME, selectorExpression);
log.info("Received message : {}", message.getText()); // xml data
//further processing
}
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
@Kshitij-Dhakal
Copy link
Author

To run IBM MQ locally, install docker and run following 3 commands

 docker pull icr.io/ibm-messaging/mq:latest
 docker volume create qm1data
 docker run --env LICENSE=accept --env MQ_QMGR_NAME=QM1 --volume qm1data:/mnt/mqm --publish 1414:1414 --publish 9443:9443 --detach --env MQ_APP_PASSWORD=passw0rd --name QM1 icr.io/ibm-messaging/mq:latest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment