How to send jms message to a jms queue
Tech-Today

How to send jms message to a jms queue


This tutorial requires that you have completed my previous tutorial on how to setup jms broker on glassfish: http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html

From the previous tutorial we will use the ff values:
queue factory: ipielPool
the queue: jms/ipielPool

Then we define a JMSSender class as follows:

package com.ipiel.commons.utils;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ipiel.commons.dto.MessageDTO;
import com.ipiel.commons.dto.util.JMSQueueEnum;
import com.ipiel.commons.dto.util.MessageDTOHelper;
import com.ipiel.commons.exceptions.InvalidDTOException;

/**
* @author Edward P. Legaspi
* @since Jun 8, 2012
*/
public class JmsSender {
private Logger log = LoggerFactory.getLogger(JmsSender.class);

private String connectionJndiName;
private QueueConnectionFactory queueConnectionFactory;

private String queueJndiName;
private Queue queue;

private boolean transacted;

public JmsSender() {
super();
}

public JmsSender(String connectionJndiName, String queueJndiName,
boolean transacted, Context jndi) throws NamingException {
this.connectionJndiName = connectionJndiName;
this.queueJndiName = queueJndiName;
this.transacted = transacted;
log.debug("[ipiel-commons] initializing jms factory={}, queue={}",
connectionJndiName, queueJndiName);

queueConnectionFactory = (QueueConnectionFactory) jndi
.lookup(this.connectionJndiName);
queue = (Queue) jndi.lookup(this.queueJndiName);

log.debug("[ipiel-commons] queue.connection.factory."
+ queueConnectionFactory.toString());
log.debug("[ipiel-commons] queue." + queue.toString());
}

public JmsSender(String connectionJndiName, String queueJndiName,
boolean transacted) throws NamingException {
this(connectionJndiName, queueJndiName, transacted,
new InitialContext());
}

public JmsSender(String connectionJndiName, String queueJndiName)
throws NamingException {
this(connectionJndiName, queueJndiName, false);
}

public String send(MessageDTO messageDTO) throws JMSException,
InvalidDTOException, NamingException {
return send(messageDTO, null, null);
}

public String send(MessageDTO messageDTO,
JMSQueueEnum resultNotificationQueue) throws JMSException,
InvalidDTOException, NamingException {
return send(messageDTO, resultNotificationQueue, null);
}

public String send(MessageDTO messageDTO,
JMSQueueEnum resultNotificationQueue, Long parentProcessId)
throws JMSException, InvalidDTOException, NamingException {
return send(messageDTO, resultNotificationQueue, parentProcessId, -1, 0);
}

public String send(MessageDTO messageDTO, int JMSPriority, long timeToLive)
throws JMSException, InvalidDTOException, NamingException {
return send(messageDTO, null, null, JMSPriority, timeToLive);
}

public String send(MessageDTO messageDTO,
JMSQueueEnum resultNotificationQueue, Long parentProcessId,
int JMSPriority, long timeToLive) throws JMSException,
InvalidDTOException, NamingException {

QueueConnection connection = null;
QueueSession session = null;
QueueSender sender = null;

try {
// Process optional parameters
messageDTO.setResultNotificationQueue(resultNotificationQueue);
messageDTO.setParentProcessId(parentProcessId);

// JMS Init
connection = openQueueConnection();
session = connection.createQueueSession(transacted,
QueueSession.AUTO_ACKNOWLEDGE);
sender = session.createSender(queue);

// Wrap MessageDTO in a JMS ObjectMessage
ObjectMessage msg = MessageDTOHelper.serialize(session, messageDTO);

msg.setJMSType(messageDTO.getClass().getSimpleName());

msg.setStringProperty("DTOType", messageDTO.getClass()
.getSimpleName());

log.debug(
"[ipiel-commons] Sending message to queue {} via connection {}, message contents {} (additional parameters : resultNotificationQueue={}, "
+ "parentProcessId={}, JMSPriority={}, TimeToLive={}, JMSCorrelationID={})",
new Object[] { queueJndiName, connectionJndiName,
messageDTO, resultNotificationQueue,
parentProcessId, JMSPriority, timeToLive,
msg.getJMSCorrelationID() });

if (JMSPriority > -1) {
sender.send(msg, DeliveryMode.PERSISTENT, JMSPriority,
timeToLive);
} else {
// Send the JMS message
sender.send(msg);
}
return msg.getJMSMessageID();
} finally {
closeQueueSender(sender);
closeSession(session);
closeConnection(connection);
}
}

/**
* Retrieves a new JMS Connection from the pool
*
* @return a QueueConnection
* @throws JMSException
* if the connection could not be retrieved
*/
private QueueConnection openQueueConnection() throws JMSException {
return queueConnectionFactory.createQueueConnection();
// queueConnection.start(); this is a pool we don't need to start the
// connection
}

/**
* Closes the JMS connection.
*/
private void closeConnection(Connection connection) {
try {
if (connection != null)
connection.close();
} catch (JMSException e) {
log.warn("Could not close JMS connection", e);
}
}

/**
* Closes the JMS session.
*/
private void closeSession(Session session) {
try {
if (session != null)
session.close();
} catch (JMSException e) {
log.warn("Could not close JMS session", e);
}
}

/**
* Closes the JMS session.
*/
private void closeQueueSender(QueueSender queueSender) {
try {
if (queueSender != null)
queueSender.close();
} catch (JMSException e) {
log.warn("Could not close queue sender", e);
}
}

/**
* Closes the JMS session.
*/
private void closeQueueReceiver(QueueReceiver queueReceiver) {
try {
if (queueReceiver != null)
queueReceiver.close();
} catch (JMSException e) {
log.warn("Could not close queue sender", e);
}
}
}
And this is how you will execute a send message call:
JmsSender sender = new JmsSender("ipielPool", "jms/IpielQueue");
String jmsMessageId = sender.send(msg, JMSQueueEnum.MERCHANT_GATEWAY);

After that you can check your queue browser if you already have a jms message in jms/IpielQueue. I suggest you use QBrowser, which you can download from: http://sourceforge.net/projects/qbrowserv2/ The helper class:
public class MessageDTOHelper {
public static MessageDTO deserialize(ObjectMessage m) throws JMSException,
InvalidDTOException {
MessageDTO msgDTO = (MessageDTO) m.getObject();
return msgDTO;
}

public static ObjectMessage serialize(Session session, MessageDTO dto)
throws JMSException, InvalidDTOException {
dto.validate();
ObjectMessage msg = session.createObjectMessage(dto);
return msg;
}
}




- 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 Send An Email In Glassfish Using Javamail Session Injection
This tutorial will teach us how to send an email in Glassfish using JavaMail session injection. It use gmail for sending the email. Steps: 1.) First we need to create a JavaMail session by, opening Glassfish admin: http://localhost:4848, and navigating...

- 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








.