Tech-Today
How to receive a synchronous jms message
This tutorial requires that you have glassfish installed and followed the following previous 2 tutorials:
http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html
http://czetsuya-tech.blogspot.com/2012/06/how-to-send-jms-message-to-jms-queue.html
For simplicity, we will just extend the JMSSender class that we have defined from the previous tutorial and add the ff method:
public void getMessages() throws JMSException {
ObjectMessage objectMessage;
QueueConnection connection = null;
QueueSession session = null;
QueueReceiver queueReceiver = null;
boolean goodByeReceived = false;
try {
connection = openQueueConnection();
session = connection.createQueueSession(transacted,
QueueSession.AUTO_ACKNOWLEDGE);
queueReceiver = session.createReceiver(queue);
connection.start();
while (!goodByeReceived) {
log.debug("[xxx-commons] Waiting for messages...");
objectMessage = (ObjectMessage) queueReceiver.receive();
MessageDTO message = MessageDTOHelper
.deserialize(objectMessage);
if (message != null) {
log.debug("[xxx-commons] Received the following message: ");
log.debug("[xxx-commons] message: " + message.toString()
+ " : " + message.getRequestId());
}
if (message.getRequestId() != null
&& message.getRequestId().equals("7d")) {
goodByeReceived = true;
}
}
log.debug("[xxx-commons] jms receive done");
} finally {
closeQueueReceiver(queueReceiver);
closeSession(session);
closeConnection(connection);
}
}
This method will read from the queue that we have defined. Note that queueReceiver.receive() without parameter will block until there's a jms message in queue.
Tip you can create a generic jms receiver class that you can extend for different DTOs.
-
Javaee Development
JavaEE6How to create a javaee6 web app using jboss maven war archetypeCreate a simple javaee6 web app with maven, glassfish and postgresqlHow to validate a JavaEE6 Bean in a jobHow to add JavaEE 6 archetypes in eclipse keplerHow to create a custom bean...
-
How To Send And Receive Stomp Message In Jboss 7.2
The following code is an example of how we can send and receive a stomp message with JBoss 7.2. package org.meveo.util; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Properties; import javax.naming.InitialContext;...
-
How To Implement Jms Request/response Feature In Glassfish
This tutorial will provide a sample code on how you can send request and receive reply using jms. I've used Glassfish 3.1.1, with its built in JMS Broker for testing set to LOCAL. We need 2 maven projects (1 is for request and 1 for receiving the...
-
How To Receive An Asynchronous Jms Message
This tutorial assumes that you have glassfish installed and followed the 2 previous tutorials: http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html http://czetsuya-tech.blogspot.com/2012/06/how-to-send-jms-message-to-jms-queue.html...
-
How To Setup Set Jms Factory And Queue In Glassfish
This tutorial assumes that you already have glassfish installed and running on your local machine. 1.) To check the default setting of your JMS broker navigate to Configurations->server-config->Java Message Service->JMS Hosts->default_JMS_host....
Tech-Today