English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To integrate Spring with JMS, you need to create two applications.
JMS receiver application JMS Sender application
To create a JMS application with Spring, we use Apache's Active MQ Server Create a queue.
Let's see the simple steps to integrate a Spring application. Use JMS:
1, you need to add spring core , spring misc , spring aop , spring j2ee and Spring persistence core jar files.
Download all jar files of Spring, including aop, mvc, j2ee, remoting, oxm, etc.
2, add activemqall5. 9.jar The file is located in the activemq directory.
Download Active MQ server Download Active MQ
Double-click activemq.bat The file is located in the apache-activemq-5.9.1-bin \ apache-activemq-5.9.1 \ bin \ win64Or win32Directory.
Now the ActiveMQ server console will open.
Through http://localhost:8161/admin/Visit the management console of the ActiveMQ server by URL.
Now, click Queue link, and write in the text field myqueue , then click the Create button
Let's take a look at the simple steps to integrate a Spring application with JMS:
MyMessageListener.java TestListener.java applicationContext.xml
1)MyMessageListener.java
package com.w3codebox; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class MyMessageListener implements MessageListener { @Override public void onMessage(Message m) { TextMessage message = (TextMessage)m; try{ System.out.println(message.getText()); }catch (Exception e) {e.printStackTrace();} } }
2)TestListener.java
package com.w3codebox; import org.springframework.context.support.GenericXmlApplicationContext; public class TestListener { public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:applicationContext.xml"); ctx.refresh(); while(true){} } }
3)applicationContext.xml
<?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:jms="http://www.springframework.org/schema/jms" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> p:brokerURL="tcp://localhost:61616" /> <bean id="listener" class="com.w3codebox.MyMessageListener">/bean> <jms:listener-container container-type="default" connection-factory="connectionFactory" acknowledge="auto"> <jms:listener destination="myqueue" ref="listener" method="onMessage">/jms:listener> </jms:listener-container> </beans>
Let's take a look at the file for creating the JMS Sender application:
MyMessageSender.java TestJmsSender.java applicationContext.xml
1)MyMessageListener.java
package com.w3codebox; import javax.jms.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Component; @Component("messageSender") public class MyMessageSender { @Autowired private JmsTemplate jmsTemplate; public void sendMessage(final String message) { jmsTemplate.send(new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } }
2)TestJmsSender.java
package com.w3codebox; import org.springframework.context.support.GenericXmlApplicationContext; public class TestJmsSender { public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:applicationContext.xml"); ctx.refresh(); MyMessageSender sender = ctx.getBean("messageSender", MyMessageSender.class); sender.sendMessage("hello jms3"); } }
3)applicationContext.xml
<?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:jms="http://www.springframework.org/schema/jms" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> p:brokerURL="tcp://localhost:61616" /> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <constructor-arg name="connectionFactory" ref="connectionFactory"></constructor-arg> <property name="defaultDestinationName" value="myqueue"></property> </bean> context:component-scan base-package="com.w3codebox"></context:component-scan> </beans>