package com.ipiel.commons.utils;And this is how you will execute a send message call:
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 aQueueConnection
* @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);
}
}
}
JmsSender sender = new JmsSender("ipielPool", "jms/IpielQueue");
String jmsMessageId = sender.send(msg, JMSQueueEnum.MERCHANT_GATEWAY);
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;
}
}