testing if my jms listener is working - jms

i want to test if my jms listener is working perfectly
when sending 5 messages ( for example ) i added a timer
"Threat.sleep(5000)" and after 5 seconds i want to compare the old messageID with the new messageID that means i want to know if the messages are listened or not so if the ID changes that means that they are successfuly listened
here is my OnMessage code ... but it doesnt work for me :((
public class Consumer implements MessageListener{
public Consumer() {
}
//#Override
public void onMessage(Message message) {
try {
TextMessage tm = (TextMessage) message;
int i;
TextMessage tm2 =tm;
for(i=0;i<1;i++)
{
try {
Thread.sleep(5000);
if (!tm.getJMSMessageID().equals(tm2.getJMSMessageID()))
{
System.out.println("\t----Listener not working----");
}
else {
System.out.println("Message reçu:");
System.out.println("\tTemps: " + System.currentTimeMillis() + " ms");
System.out.println("\tMessage ID: " + tm.getJMSMessageID());
System.out.println("\tCorrel. ID: " + tm.getJMSCorrelationID());
System.out.println("\tConsumed message: " + tm.getText());
System.out.println("\t----Listener working----");
}
//fin else
}
catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
catch (JMSException jex) {
System.out.println("Exception: " + jex);
}
}
}

So you're checking the JMS message id to ensure that no two messages have identical id's? This is probably overkill since, if your listener is receiving messages, it's working!...Nevertheless, your approach has a few problems....
TextMessage tm = (TextMessage) message;
...
TextMessage tm2 =tm;
Thread.sleep(5000);
if (!tm.getJMSMessageID().equals(tm2.getJMSMessageID()))
The above if() will always be false because you're comparing the same message; also, the sleep() statement is useless.
If you want to compare message id's to ensure they are always unique, although if the broker fails to deliver a message the same message will be redelivered and the id would be the same, but anyway, you can use this an an alternative to your code..
private static ConcurrentHashMap<String,String> mesgIdMap =
new ConcurrentHashMap<String, String>();
public Consumer() {
}
//#Override
public void onMessage(Message message) {
try {
TextMessage tm = (TextMessage) message;
if( mesgIdMap.contains( tm.getJMSMessageID()))
System.out.println("\tProcessing JMS message with same ID again!");
//add the message id to the map
mesgIdMap.put( tm.getJMSMessageID(),tm.getJMSMessageID());
//print statements here...

Related

Store the message which is received/sent to the queue using JmsListener

Is there any way to put interceptor in jms listener..requirement is to store the request and response while reading and writing to message queue
#JmsListener(destination = "${ibm.mq.queueName}", containerFactory = "containerFactory")
public void readAndProcessMessage(Message<?> message) throws Exception {
UnProcessedEvent unProcessedEvent;
String eventMessage = message.getPayload().toString();
log.info("Received an event: " + eventMessage);
try {
String iban = getIban(eventMessage);
// Identifying Topic
for (Mandate mandate : configProperties.getMandates()) {
for (Topic topic : mandate.getTopic()) {
if (topic.getAccountNumber().equals(iban)) {
publisherService.publishEvent(iban, eventMessage);
return;
}
}
}
unProcessedEvent = UnProcessedEvent.builder().incomingReqPayload((eventMessage)).reason("No Topic Found")
.reasonCode(HttpStatus.BAD_REQUEST.toString()).build();
unprocessedEventRepository.save(unProcessedEvent);
} catch (JAXBException e) {
log.info("Exception while parsing the event message: " + e.getMessage());
unProcessedEvent = UnProcessedEvent.builder().incomingReqPayload((eventMessage)).reason("Bad Request")
.reasonCode(HttpStatus.BAD_REQUEST.toString()).build();
unprocessedEventRepository.save(unProcessedEvent);
}
}

unable to make activemq consumer to deque

I am creating a JMS chat application using activemq and spring boot. I am trying to send message from producer to multiple subscribers. I am able to send message i.e message is en-queued. but in my receiver part message is unable to de-queue.` I am using the below code for communicating message from producer to multiple subscribers.
public class WelcomeController implements MessageListener {
public static Boolean TRANSACTIONAL = false;
public static String TOPIC_NAME = "firstTopic";
public static String BROKER_URL = "tcp://localhost:61616";
public static String BROKER_USERNAME = "admin";
public static String BROKER_PASSWORD = "admin";
public void createProducer() throws JMSException {
Connection connection = null;
Session session = null;
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(BROKER_URL);
connectionFactory.setPassword(BROKER_USERNAME);
connectionFactory.setUserName(BROKER_PASSWORD);
connection = connectionFactory.createConnection();
connection.setClientID("CircliTopic");
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
for (int i = 1; i <= 3; i++) {
session = connection.createSession(TRANSACTIONAL,
Session.AUTO_ACKNOWLEDGE);
Topic destination = session.createTopic(TOPIC_NAME);
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
message.setText( "My text message was send and received");//
System.out.println("Sending text '" + message + "'");
producer.send(message);
MessageConsumer consumer = session
.createDurableSubscriber(destination, "Listener" + i);
consumer.setMessageListener(new WelcomeController());
}
} finally {
connection.close();
}`
}
#Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;
System.out.println(" - Consuming text msg: " + text.getText());
} else if (message instanceof ObjectMessage) {
ObjectMessage objmsg = (ObjectMessage) message;
Object obj = objmsg.getObject();
System.out.println(" - Consuming object msg: " + obj);
} else {
System.out.println(
" - Unrecognized Message type " + message.getClass());
}
} catch (JMSException e) {
e.printStackTrace();
}
}
I am able to get consuming text message in my console but my message is not de-queued to the subscribers and also in my activemq server message is not dequeued.
You are creating a Topic subscription only after the message has been sent and that won't work because these are Topics and a Topic with no subscriptions simply discards all messages sent to it. You need to establish a durable Topic subscription prior to any messages being sent, or switch to Queues if your design allows as a Queue will store a message sent to it until consumed.
It is hard to say more without knowing your requirements but it seems you need to spend a little bit more time understanding how Topics work.

Remove Thread.sleep() in case of Sending Message to ActiveMQ

I was fixing sonar lint error in my project. I have seen a block of Code where sonar lint is giving me error of rule squid:S2276 to replace Thread.sleep(100); with wait(). But wait() should be in conditional loop to escape spurious wakeup problem. But I am not getting such a condition how I should use.
Can i achieve the same thing without sleep()
public class ACTOOBEventSubSMSProducer {
private ACTOOBEventSubSMSProducer(){
super();
}
private static Logger logger = LoggerManager.getInstance().getCoreProcessingLogger();
public static final String CONNECTION_FACTORY = "java:jboss/activemq/ConnectionFactory";
static final String QUEUE_NAME = "java:jboss/exported/jms/queue/actOOBEventSubscriptionSMS";
static ConnectionFactory connectionFactory = null;
static Connection connection = null;
static Session session = null;
static Destination destination = null;
static MessageProducer messageProducer = null;
static {
connectionFactory = ServiceLocator.getJmsConnectionFactory(CONNECTION_FACTORY);
try {
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.DUPS_OK_ACKNOWLEDGE);
destination = ServiceLocator.getJmsDestination(QUEUE_NAME);
messageProducer = session.createProducer(destination);
messageProducer.setDisableMessageID(true);
messageProducer.setDisableMessageTimestamp(true);
} catch (JMSException e) {
logger.error("Error in creating ConnectionFactory",e);
}
}
/**
* This method sends OOB Event SMS Message in Queue.
*
* #param message
*/
public static synchronized void sendMessage(Serializable payload) throws JmsProducerException {
try {
ObjectMessage message = session.createObjectMessage(payload);
messageProducer.send(message, javax.jms.DeliveryMode.NON_PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY,
1800000);
} catch (JMSException je) {
try {
Thread.sleep(100);
ObjectMessage message = session.createObjectMessage(payload);
messageProducer.send(message, javax.jms.DeliveryMode.PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY,
1800000);
} catch (JMSException jee) {
logger.error("Error in sendMessage()",jee);
throw new JmsProducerException(jee);
} catch (InterruptedException ie) {
logger.error("Error in sendMessage()",ie);
Thread.currentThread().interrupt();
throw new JmsProducerException(ie);
}
} catch (ServiceLocatorException sle) {
logger.error("Error in sendMessage()",sle);
throw new JmsProducerException(sle);
}
}
}
A carefully crafted for loop with the iterations count being the number of times you wanted to retry and some exception handling logic to break the loop if things work would do the trick in at a slightly clearer manner perhaps, otherwise using a ScheduledThreadPoolExecutor or or Timer would be an option.

How to get number of pending message in a jms queue

Is there any way to get count number of pending messages in jms queue. My aim is to close the connection if there is no message remaining in the queue to process. how can i achieve this.
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection("admin", "admin");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
MessageConsumer consumer = session.createConsumer(destination);
while (true) {
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Incoming Message:: '" + textMessage.getText() + "'");
}
}
The only reliable way to get the true Queue count form the broker is to use the JMX MBean for the Queue and call the getQueueSize method.
The other programmatic alternative is to use the Statistics Broker Plugin which requires that you be able to change broker configuration to install it. Once installed you can send a special message to the control queue and get a response with details for the destination you want to monitor.
Using a QueueBrowser doesn't give you a true count because the browser has a max limit on how many messages it will page into memory to send you, so if your queue is deeper than the limit you won't get the actual size, just the value of the max page size limit.
I have done this by using createBrowser method below is my updated code.
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection("admin", "admin");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
int queueSize = QueueConsumer.getQueueSize(session, (Queue) destination);
System.out.println("QUEUE SIZE: " + queueSize);
MessageConsumer consumer = session.createConsumer(destination);
for (int i = 0; i < queueSize; i++) {
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Incomming Message: '" + textMessage.getText() + "'");
}
}
connection.close();
}
private int getQueueSize(Session session, Queue queue) {
int count = 0;
try {
QueueBrowser browser = session.createBrowser(queue);
Enumeration elems = browser.getEnumeration();
while (elems.hasMoreElements()) {
elems.nextElement();
count++;
}
} catch (JMSException ex) {
ex.printStackTrace();
}
return count;
}
I have done this with jmx it worked thanx #Tim Bish
here is my updated code
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://0.0.0.0:44444/jndi/rmi://0.0.0.0:1099/karaf-root");
HashMap<String, String[]> environment = new HashMap<String, String[]>();
String[] creds = { "admin", "admin" };
environment.put(JMXConnector.CREDENTIALS, creds);
JMXConnector jmxc = JMXConnectorFactory.connect(url, environment);
MBeanServerConnection connection = jmxc.getMBeanServerConnection();
ObjectName nameConsumers = new ObjectName("org.apache.activemq:type=Broker,brokerName=amq,destinationType=Queue,destinationName=myqueue");
DestinationViewMBean mbView = MBeanServerInvocationHandler.newProxyInstance(connection, nameConsumers, DestinationViewMBean.class, true);
long queueSize = mbView.getQueueSize();
System.out.println(queueSize);
Just break the loop and close the connection if your JMS Message is null..
while (true) {
Message message = consumer.receive(2000);
if (message == null){
break;
}
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Incoming Message:: '" + textMessage.getText() + "'");
}
}
connection.close();

Trying to get the message from server but receive call gets blocked

// This is send message from where I am sending message to server, Its working fine
public void sendMessage(com.google.protobuf.Message sendmessage) {
try {
createJmsTemplate();
createJmsTemplateReciever();
JmsMessageCreator jmsMessageCreator = new JmsMessageCreator() {
#Override
public Message createMessage(Session session) throws JMSException {
BytesMessage msg = session.createBytesMessage();
msg.writeBytes(sendmessage.toByteArray());
return msg;
}
};
MessageCreator messageCreator = new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
Message msg = jmsMessageCreator.createMessage(session);
msg.setJMSCorrelationID("2708");
return msg;
}
};
jmsTemplate.send(messageCreator);
System.out.println("Message sent... ");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
//But when i am calling this method, at receive call it gets blocked...
public void recieveMessage() {
try {
byteMessage = (BytesMessage) jmsTemplateReciever.receive();
try {
if (byteMessage != null) {
byte[] byteArr = new byte[(int) byteMessage.getBodyLength()];
for (int i = 0; i < (int) byteMessage.getBodyLength(); i++) {
byteArr[i] = byteMessage.readByte();
String s = new String(byteArr);
System.out.println(s);
}
String s = new String(byteArr);
System.out.println(s);
byteMessage.acknowledge();
}
} catch (JMSException e) {
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
As described in section 9.2.2 of JMS 1.1 Specification, the receive() call blocks indefinitely until a message arrives on the queue. Hence the call is getting blocked in your application.
One option for you is to specify a wait time, for example receive(3000) which waits for 3 seconds and comes out if no message arrives in 3 seconds. JMS implementer might be providing another form of receive method where the method returns immediately if there are no messages in the queue.
The other option is to use a message listener for receiving messages asynchronously as described JMS 1.1 Specifications section 9.3.1. Your application gets notified by the JMS provider whenever a message arrives in a queue.

Resources