From the previous tutorial nosotros volition purpose the ff values:
queue factory: ipielPool
the queue: jms/ipielPool
Then nosotros define a JMSSender flat equally 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 */ populace flat JmsSender { mortal Logger log = LoggerFactory.getLogger(JmsSender.class); mortal String connectionJndiName; mortal QueueConnectionFactory queueConnectionFactory; mortal String queueJndiName; mortal Queue queue; mortal boolean transacted; populace JmsSender() { super(); } populace 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()); } populace JmsSender(String connectionJndiName, String queueJndiName, boolean transacted) throws NamingException { this(connectionJndiName, queueJndiName, transacted, novel InitialContext()); } populace JmsSender(String connectionJndiName, String queueJndiName) throws NamingException { this(connectionJndiName, queueJndiName, false); } populace String send(MessageDTO messageDTO) throws JMSException, InvalidDTOException, NamingException { provide send(messageDTO, null, null); } populace String send(MessageDTO messageDTO, JMSQueueEnum resultNotificationQueue) throws JMSException, InvalidDTOException, NamingException { provide send(messageDTO, resultNotificationQueue, null); } populace String send(MessageDTO messageDTO, JMSQueueEnum resultNotificationQueue, Long parentProcessId) throws JMSException, InvalidDTOException, NamingException { provide send(messageDTO, resultNotificationQueue, parentProcessId, -1, 0); } populace String send(MessageDTO messageDTO, int JMSPriority, long timeToLive) throws JMSException, InvalidDTOException, NamingException { provide send(messageDTO, null, null, JMSPriority, timeToLive); } populace String send(MessageDTO messageDTO, JMSQueueEnum resultNotificationQueue, Long parentProcessId, int JMSPriority, long timeToLive) throws JMSException, InvalidDTOException, NamingException { QueueConnection connexion = null; QueueSession session = null; QueueSender sender = null; seek { // Process optional parameters messageDTO.setResultNotificationQueue(resultNotificationQueue); messageDTO.setParentProcessId(parentProcessId); // JMS Init connexion = openQueueConnection(); session = connection.createQueueSession(transacted, QueueSession.AUTO_ACKNOWLEDGE); sender = session.createSender(queue); // Wrap MessageDTO inwards 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 connexion {}, message contents {} (additional parameters : resultNotificationQueue={}, " + "parentProcessId={}, JMSPriority={}, TimeToLive={}, JMSCorrelationID={})", novel 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); } provide msg.getJMSMessageID(); } live on { closeQueueSender(sender); closeSession(session); closeConnection(connection); } } /** * Retrieves a novel JMS Connection from the puddle * * @return a QueueConnection
* @throws JMSException * if the connexion could non live retrieved */ mortal QueueConnection openQueueConnection() throws JMSException { provide queueConnectionFactory.createQueueConnection(); // queueConnection.start(); this is a puddle nosotros don't need to kickoff the // connexion } /** * Closes the JMS connection. */ mortal void closeConnection(Connection connection) { seek { if (connection != null) connection.close(); } pick out care of (JMSException e) { log.warn("Could non unopen JMS connection", e); } } /** * Closes the JMS session. */ mortal void closeSession(Session session) { seek { if (session != null) session.close(); } pick out care of (JMSException e) { log.warn("Could non unopen JMS session", e); } } /** * Closes the JMS session. */ mortal void closeQueueSender(QueueSender queueSender) { seek { if (queueSender != null) queueSender.close(); } pick out care of (JMSException e) { log.warn("Could non unopen queue sender", e); } } /** * Closes the JMS session. */ mortal void closeQueueReceiver(QueueReceiver queueReceiver) { seek { if (queueReceiver != null) queueReceiver.close(); } pick out care of (JMSException e) { log.warn("Could non unopen queue sender", e); } } }
And this is how you lot volition execute a post message call: JmsSender sender = novel JmsSender("ipielPool", "jms/IpielQueue"); String jmsMessageId = sender.send(msg, JMSQueueEnum.MERCHANT_GATEWAY);
After that you lot tin laissez passer notice depository fiscal establishment jibe your queue browser if you lot already convey a jms message inwards jms/IpielQueue. I propose you lot purpose QBrowser, which you lot tin laissez passer notice download from: http://sourceforge.net/projects/qbrowserv2/ The helper class:
public flat MessageDTOHelper { populace static MessageDTO deserialize(ObjectMessage m) throws JMSException, InvalidDTOException { MessageDTO msgDTO = (MessageDTO) m.getObject(); provide msgDTO; } populace static ObjectMessage serialize(Session session, MessageDTO dto) throws JMSException, InvalidDTOException { dto.validate(); ObjectMessage msg = session.createObjectMessage(dto); provide msg; } }
0 komentar:
Please comment if there are any that need to be asked.