第一步:添加POM.XML依赖
1 <properties> 2 <spring.version>4.2.5.RELEASE</spring.version> 3 </properties> 4 <dependencies> 5 <dependency> 6 <groupId>junit</groupId> 7 <artifactId>junit</artifactId> 8 <version>4.11</version> 9 <scope>test</scope> 10 </dependency> 11 12 <dependency> 13 <groupId>org.springframework</groupId> 14 <artifactId>spring-context</artifactId> 15 <version>${spring.version}</version> 16 </dependency> 17 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-jms</artifactId> 21 <version>${spring.version}</version> 22 </dependency> 23 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-test</artifactId> 27 <version>${spring.version}</version> 28 </dependency> 29 30 <dependency> 31 <groupId>org.apache.activemq</groupId> 32 <artifactId>activemq-core</artifactId> 33 <version>5.7.0</version> 34 <exclusions> 35 <exclusion> 36 <artifactId>spring-context</artifactId> 37 <groupId>org.springframework</groupId> 38 </exclusion> 39 </exclusions> 40 </dependency>
第二步:在resources目录下新建立jmsCommon.xml 文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 6 7 <!--开启Spring的注解机制--> 8 <context:annotation-config></context:annotation-config> 9 10 <!--ActiveMQConnectionFactory为我们提供的ConnectionFactory--> 11 <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 12 <property name="brokerURL" value="tcp://192.168.0.106:61616"></property> 13 </bean> 14 15 <!--Spring jms为我们提供的connectionFactory连接池--> 16 <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> 17 <property name="targetConnectionFactory" ref="targetConnectionFactory"></property> 18 </bean> 19 20 <!--一个队列模式的目的地,点对点的目的--> 21 <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> 22 <constructor-arg value="queue"></constructor-arg> 23 </bean> 24 25 <!--一个主题模式的目的地,发布订阅模式--> 26 <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> 27 <constructor-arg value="topic"></constructor-arg> 28 </bean> 29 30 31 </beans>
第三步:建立生产者
1、在resources目录下新建立producer.xml 文件 即生产者配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> 5 6 <!--共用的jms配置信息引入--> 7 <import resource="jmsCommon.xml"></import> 8 9 <!--配置JmsTemplate,用于发送消息--> 10 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 11 <property name="connectionFactory" ref="connectionFactory"></property> 12 </bean> 13 14 <bean class="com.neusoft.jms.producer.ProducerServiceImpl"></bean> 15 16 </beans>
2、编写接口ProducerService、和接口实现类ProducerServiceImpl
1 package com.neusoft.jms.producer; 2 3 public interface ProducerService { 4 void sendMessage(String message); 5 }
1 package com.neusoft.jms.producer; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.jms.core.JmsTemplate; 5 import org.springframework.jms.core.MessageCreator; 6 7 import javax.annotation.Resource; 8 import javax.jms.*; 9 10 public class ProducerServiceImpl implements ProducerService { 11 12 @Autowired 13 JmsTemplate jmsTemplate; 14 15 @Resource(name = "topicDestination") 16 Destination destination; 17 18 public void sendMessage(final String message) { 19 //使用JmsTemplate发送消息到服务器 20 jmsTemplate.send(destination, new MessageCreator() { 21 //创建一个消息 22 public Message createMessage(Session session) throws JMSException { 23 TextMessage textMessage = session.createTextMessage(message); 24 return textMessage; 25 } 26 }); 27 System.out.println("发送消息:" + message); 28 } 29 }
3、编写消息生产类AppProducer
1 package com.neusoft.jms.producer; 2 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class AppProducer { 6 7 public static void main(String[] args) { 8 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer.xml"); 9 10 ProducerService Service = context.getBean(ProducerService.class); 11 12 for (int i = 0; i < 100 ; i++) { 13 Service.sendMessage("test 消息发送来了" + i); 14 } 15 16 context.close(); 17 } 18 }
第四步:建立消费者
1、在resources目录下新建立consumer.xml文件 即消费者配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!--共用的jms配置信息引入--> 7 <import resource="jmsCommon.xml"></import> 8 9 <!--配置消息监听器--> 10 <bean id="consumerMessageListener" class="com.neusoft.jms.consumer.ConsumerMessageListener"></bean> 11 12 <!--配置消息容器 13 1、需要指定消息容器连接的JMS连接池,由Spring提供 14 2、指定消息容器监听那个位置,什么地方的消息 15 3、指定自己实现的消息监听器实现类 16 --> 17 <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 18 <property name="connectionFactory" ref="connectionFactory"></property> 19 <property name="destination" ref="topicDestination"></property> 20 <property name="messageListener" ref="consumerMessageListener"></property> 21 </bean> 22 23 </beans>
2、编写消息监听器主类ConsumerMessageListener
1 package com.neusoft.jms.consumer; 2 3 import javax.jms.JMSException; 4 import javax.jms.Message; 5 import javax.jms.MessageListener; 6 import javax.jms.TextMessage; 7 8 public class ConsumerMessageListener implements MessageListener{ 9 10 11 public void onMessage(Message message) { 12 13 TextMessage textMessage = (TextMessage) message; 14 15 try { 16 System.out.println("接收消息:" + textMessage.getText()); 17 } catch (JMSException e) { 18 e.printStackTrace(); 19 } 20 21 } 22 }
3、编写消息处理类,接收执行AppConsumer
1 package com.neusoft.jms.consumer; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class AppConsumer { 7 8 public static void main(String[] args) { 9 ApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); 10 11 } 12 }
写在最后:
测试集成结果:
1、先运行AppProducer 消息生产者,
2、再运行AppConsumer 消息消费者进行结果查看。