First you have to configure your Application Context:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
xmlns:jms="http://www.springframework.org/schema/jms" | |
xmlns:util="http://www.springframework.org/schema/util" | |
xmlns:int="http://www.springframework.org/schema/integration" | |
xmlns:int-jms="http://www.springframework.org/schema/integration/jms" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd | |
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd | |
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd | |
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd | |
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd | |
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd | |
http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd"> | |
<beans profile="DEV,TEST,PROD"> | |
<util:properties id="jndi_prop" location="file:///system/config/app-jndi.properties"/> | |
<context:property-placeholder properties-ref="jndi_prop"/> | |
<context:annotation-config/> | |
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> | |
<property name="connectionFactory" ref="connectionFactory" /> | |
<property name="destinationResolver" ref="jmsDestinationResolver" /> | |
<property name="pubSubNoLocal" value="true" /> | |
<property name="pubSubDomain" value="false" /> | |
<property name="defaultDestination" ref="xmlMarshallerRequestQueue" /> | |
<property name="explicitQosEnabled" value="true" /> | |
</bean> | |
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> | |
<property name="environment"> | |
<props> | |
<prop key="java.naming.factory.initial">#{jndi_prop['java.naming.factory.initial']}</prop> | |
<prop key="java.naming.factory.url.pkgs">#{jndi_prop['java.naming.factory.url.pkgs']}</prop> | |
<prop key="java.naming.provider.url">#{jndi_prop['java.naming.provider.url']}</prop> | |
<prop key="java.naming.security.principal">#{jndi_prop['java.naming.security.principal']}</prop> | |
<prop key="java.naming.security.credentials">#{jndi_prop['java.naming.security.credentials']}</prop> | |
</props> | |
</property> | |
</bean> | |
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> | |
<property name="jndiTemplate"> | |
<ref bean="jndiTemplate" /> | |
</property> | |
<property name="jndiName" value="#{jms_prop['jms.connection.factory.jndi.name']}" /> | |
</bean> | |
<bean id="cachingConnectionFactory" | |
class="org.springframework.jms.connection.CachingConnectionFactory"> | |
<property name="targetConnectionFactory" ref="connectionFactory" /> | |
<property name="sessionCacheSize" value="10" /> | |
</bean> | |
<bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager"> | |
<property name="connectionFactory" ref="cachingConnectionFactory" /> | |
</bean> | |
<bean id="jmsDestinationResolver" | |
class="org.springframework.jms.support.destination.JndiDestinationResolver"> | |
<property name="jndiTemplate"> | |
<ref bean="jndiTemplate" /> | |
</property> | |
<property name="cache" value="true" /> | |
<property name="fallbackToDynamicDestination" value="false" /> | |
</bean> | |
<bean id="lineBusinessReplyQueue" class="org.springframework.jndi.JndiObjectFactoryBean"> | |
<property name="jndiTemplate"> | |
<ref bean="jndiTemplate" /> | |
</property> | |
<property name="jndiName" value="#{jms_prop['jms.queue.jndi.name.lineBusinessreply']}" /> | |
</bean> | |
<bean id="lineBusinessRequestQueue" class="org.springframework.jndi.JndiObjectFactoryBean"> | |
<property name="jndiTemplate"> | |
<ref bean="jndiTemplate" /> | |
</property> | |
<property name="jndiName" value="#{jms_prop['jms.queue.jndi.name.lineBusinessrequest']}" /> | |
</bean> | |
<bean id="lineBusinessErrorQueue" class="org.springframework.jndi.JndiObjectFactoryBean"> | |
<property name="jndiTemplate"> | |
<ref bean="jndiTemplate" /> | |
</property> | |
<property name="jndiName" value="#{jms_prop['jms.queue.jndi.name.lineBusinesserror']}" /> | |
</bean> | |
<bean id="lineBusinessMessageErrorBrowser" class="mx.com.company.jms.ErrorMessageBrowser"> | |
<property name="queue" ref="lineBusinessErrorQueue" /> | |
<property name="jmsTemplate" ref="jmsTemplate" /> | |
<property name="transactionManager" ref="jmsTransactionManager" /> | |
</bean> | |
</beans> | |
</beans> |
After configurate your xml file you will have to inject a browser of Message Queue:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* */ package mx.com.company.jms; | |
/* */ | |
/* */ import java.io.PrintStream; | |
/* */ import java.util.ArrayList; | |
/* */ import java.util.Enumeration; | |
/* */ import java.util.List; | |
/* */ import javax.jms.JMSException; | |
/* */ import javax.jms.Message; | |
/* */ import javax.jms.ObjectMessage; | |
/* */ import javax.jms.Queue; | |
/* */ import javax.jms.QueueBrowser; | |
/* */ import javax.jms.Session; | |
/* */ import javax.jms.TextMessage; | |
/* */ import org.springframework.jms.connection.JmsTransactionManager; | |
/* */ import org.springframework.jms.core.BrowserCallback; | |
/* */ import org.springframework.jms.core.JmsTemplate; | |
/* */ import org.springframework.transaction.annotation.Isolation; | |
/* */ import org.springframework.transaction.annotation.Propagation; | |
/* */ import org.springframework.transaction.annotation.Transactional; | |
/* */ | |
/* */ @Transactional | |
/* */ public class ErrorMessageBrowser | |
/* */ { | |
/* */ private JmsTemplate jmsTemplate; | |
/* */ private Queue queue; | |
/* */ private JmsTransactionManager transactionManager; | |
/* */ | |
/* */ @Transactional(isolation=Isolation.DEFAULT, propagation=Propagation.REQUIRED, rollbackFor={JMSException.class}) | |
/* */ public int contarMensajes() | |
/* */ throws JMSException | |
/* */ { | |
/* 39 */ int count = ((Integer)this.jmsTemplate.browse(this.queue, new BrowserCallback() | |
/* */ { | |
/* */ public Integer doInJms(Session session, QueueBrowser browser) throws JMSException { | |
/* 42 */ Enumeration enumeration = browser.getEnumeration(); | |
/* 43 */ int counter = 0; | |
/* 44 */ while (enumeration.hasMoreElements()) { | |
/* 45 */ Message msg = (Message)enumeration.nextElement(); | |
/* 46 */ System.out.println(String.format("\tFound : %s", new Object[] { msg })); | |
/* 47 */ counter++; | |
/* */ } | |
/* 49 */ return Integer.valueOf(counter); | |
/* */ } | |
/* */ })).intValue(); | |
/* */ | |
/* 52 */ System.out.println(String.format("\tThere are %s messages", new Object[] { Integer.valueOf(count) })); | |
/* 53 */ return count; | |
/* */ } | |
/* */ @Transactional(isolation=Isolation.DEFAULT, propagation=Propagation.REQUIRED, rollbackFor={JMSException.class}) | |
/* */ public List<String> obtenerTextMessages() throws JMSException { | |
/* 57 */ List count = (List)this.jmsTemplate.browse(this.queue, new BrowserCallback() | |
/* */ { | |
/* */ public List<String> doInJms(Session session, QueueBrowser browser) throws JMSException { | |
/* 60 */ Enumeration enumeration = browser.getEnumeration(); | |
/* 61 */ List lstText = new ArrayList(); | |
/* 62 */ while (enumeration.hasMoreElements()) { | |
/* 63 */ Message msg = (Message)enumeration.nextElement(); | |
/* 64 */ if ((msg instanceof TextMessage)) | |
/* */ { | |
/* 66 */ TextMessage tm = (TextMessage)msg; | |
/* 67 */ String mensaje = tm.getText(); | |
/* 68 */ lstText.add(mensaje); | |
/* */ } | |
/* 70 */ System.out.println(String.format("\tFound : %s", new Object[] { msg })); | |
/* */ } | |
/* 72 */ return lstText; | |
/* */ } | |
/* */ }); | |
/* 75 */ System.out.println(String.format("\tThere are %s messages", new Object[] { count })); | |
/* 76 */ return count; | |
/* */ } | |
/* */ | |
/* */ public List<Message> obtenerMessageException() throws JMSException { | |
/* 80 */ List count = (List)this.jmsTemplate.browse(this.queue, new BrowserCallback() | |
/* */ { | |
/* */ public List<Message> doInJms(Session session, QueueBrowser browser) throws JMSException { | |
/* 83 */ Enumeration enumeration = browser.getEnumeration(); | |
/* 84 */ List lstText = new ArrayList(); | |
/* 85 */ while (enumeration.hasMoreElements()) { | |
/* 86 */ Message msg = (Message)enumeration.nextElement(); | |
/* */ | |
/* 88 */ if ((msg instanceof ObjectMessage)) | |
/* */ { | |
/* 90 */ ObjectMessage om = (ObjectMessage)msg; | |
/* 91 */ Object objeto = om.getObject(); | |
/* 92 */ if ((objeto instanceof MessageException)) | |
/* 93 */ lstText.add(msg); | |
/* */ } | |
/* 95 */ System.out.println(String.format("\tFound : %s", new Object[] { msg })); | |
/* */ } | |
/* 97 */ return lstText; | |
/* */ } | |
/* */ }); | |
/* 100 */ System.out.println(String.format("\tThere are %s messages", new Object[] { count })); | |
/* 101 */ return count; | |
/* */ } | |
/* */ | |
/* */ public void setJmsTemplate(JmsTemplate jmsTemplate) | |
/* */ { | |
/* 108 */ this.jmsTemplate = jmsTemplate; | |
/* */ } | |
/* */ | |
/* */ public JmsTemplate getJmsTemplate() { | |
/* 112 */ return this.jmsTemplate; | |
/* */ } | |
/* */ | |
/* */ public void setQueue(Queue queue) { | |
/* 116 */ this.queue = queue; | |
/* */ } | |
/* */ | |
/* */ public Queue getQueue() { | |
/* 120 */ return this.queue; | |
/* */ } | |
/* */ | |
/* */ public void setTransactionManager(JmsTransactionManager transactionManager) { | |
/* 124 */ this.transactionManager = transactionManager; | |
/* */ } | |
/* */ | |
/* */ public JmsTransactionManager getTransactionManager() { | |
/* 128 */ return this.transactionManager; | |
/* */ } | |
/* */ } | |
Now you can Inject the Request/Response in any Service, this had getting easier a MQ Implementation.
Best Regards,