Code, JavaEE

JMS Util


public class JMSUtil {

public static void sendJmsMessageToQueue(ConnectionFactory connectionFactory, String message, String queueName) {
 try {
 Connection connection = connectionFactory.createConnection();
 connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination destination = session.createQueue(queueName);

MessageProducer producer = session.createProducer(destination);
 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

TextMessage textMessage = session.createTextMessage(message);

producer.send(textMessage);

session.close();
 connection.close();

System.out.printf("JMS Message Sent: %s%n", textMessage);

} catch (JMSException e) {
 e.printStackTrace();
 }
 }

public static String retrieveJmsMessageFromQueue(ConnectionFactory connectionFactory, String queueName) {
 String xml = null;
 try {
 Connection connection = connectionFactory.createConnection();
 connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination destination = session.createQueue(queueName);

MessageConsumer consumer = session.createConsumer(destination);

Message message = consumer.receive(3000);

if (message instanceof TextMessage) {
 TextMessage textMessage = (TextMessage)message;
 xml = textMessage.getText();
 }
 } catch (Exception e) {
 e.printStackTrace();
 }

return xml;
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s