Spring JMS

简介:

1. 创建maven工程

   这个工程的文件结构如下图

  

 

2. pom配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<? xml  version="1.0" encoding="UTF-8"?>
< project  xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     < modelVersion >4.0.0</ modelVersion >
 
     < groupId >com.example.jms</ groupId >
     < artifactId >jms-spring</ artifactId >
     < version >1.0-SNAPSHOT</ version >
 
     < properties >
         < spring.version >4.2.5.RELEASE</ spring.version >
     </ properties >
     < dependencies >
         < dependency >
             < groupId >junit</ groupId >
             < artifactId >junit</ artifactId >
             < version >4.11</ version >
             < scope >test</ scope >
         </ dependency >
 
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-context</ artifactId >
             < version >${spring.version}</ version >
         </ dependency >
 
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-jms</ artifactId >
             < version >${spring.version}</ version >
         </ dependency >
 
         < dependency >
             < groupId >org.springframework</ groupId >
             < artifactId >spring-test</ artifactId >
             < version >${spring.version}</ version >
         </ dependency >
 
         < dependency >
             < groupId >org.apache.activemq</ groupId >
             < artifactId >activemq-core</ artifactId >
             < version >5.7.0</ version >
             < exclusions >
                 < exclusion >
                     < artifactId >spring-content</ artifactId >
                     < groupId >org.springframework</ groupId >
                 </ exclusion >
             </ exclusions >
         </ dependency >
     </ dependencies >
</ project >

  

3. 创建生产者

3.1 创建ProducerServer接口

1
2
3
4
public  interface  ProducerService {
     void  sendMessage(String message);
 
}

3.2 创建ProducerServiceImpl类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public  class  ProducerServiceImpl  implements  ProducerService {
 
     @Autowired
     JmsTemplate jmsTemplate;
 
     @Resource (name= "queueDestination" )
     Destination destination;
 
 
     public  void  sendMessage( final  String message) {
         // 使用JmsTemplate发送消息
         jmsTemplate.send(destination,  new  MessageCreator(){
             //创建一个消息
             public  Message createMessage(Session session)  throws  JMSException {
                 TextMessage textMessage = session.createTextMessage(message);
                 return  textMessage;
             }
         });
 
         System.out.println( "发送消息: "  + message);
     }
}

  

 

  

 3.3 创建配置文件producer.xml

配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<? 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"
        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">
 
     < context:annotation-config ></ context:annotation-config >
 
     <!--ActiveMQ为我们提供的ConnectinFactory -->
     < bean  id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
         < property  name="brokerURL" value="tcp://192.168.2.121:61616"></ property >
     </ bean >
     <!--spring jms为我们提供的连接池-->
     < bean  id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
         < property  name="targetConnectionFactory" ref="targetConnectionFactory"></ property >
     </ bean >
 
     <!--一个队列目的地,点对点的-->
     < bean  id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
         < constructor-arg  value="queue" />
     </ bean >
 
     <!--配置JmsTemplate, 用于发送消息-->
     < bean  id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
         < property  name="connectionFactory" ref="connectionFactory"></ property >
     </ bean >
 
     < bean  class="com.example.jms.producer.ProducerServiceImpl">
 
     </ bean >
</ beans >

 

3.4 创建启动类

1
2
3
4
5
6
7
8
9
10
11
12
public  class  AppProducer {
     public  static  void  main(String[] args) {
         ClassPathXmlApplicationContext context =  new  ClassPathXmlApplicationContext( "producer.xml" );
         ProducerService service = context.getBean(ProducerService. class );
         for  ( int  i =  0 ; i <  100 ; i++) {
             service.sendMessage( "text"  + i);
         }
         context.close();
 
     }
 
}

  

 

4. 创建接收者

4.1 创建监听器

1
2
3
4
5
6
7
8
9
10
11
public  class  ConsumerMessageListener  implements  MessageListener{
     public  void  onMessage(Message message) {
         TextMessage textMessage = (TextMessage)message;
         try  {
             System.out.println( "接收消息"  + textMessage.getText());
         catch  (JMSException e) {
             e.printStackTrace();
         }
 
     }
}

  

4.2 创建配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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"
        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" >
 
     <context:annotation-config></context:annotation-config>
 
     <!--ActiveMQ为我们提供的ConnectinFactory -->
     <bean id= "targetConnectionFactory"  class = "org.apache.activemq.ActiveMQConnectionFactory" >
         <property name= "brokerURL"  value= "tcp://192.168.2.121:61616" ></property>
     </bean>
     <!--spring jms为我们提供的连接池-->
     <bean id= "connectionFactory"  class = "org.springframework.jms.connection.SingleConnectionFactory" >
         <property name= "targetConnectionFactory"  ref= "targetConnectionFactory" ></property>
     </bean>
 
     <!--一个队列目的地,点对点的-->
     <bean id= "queueDestination"  class = "org.apache.activemq.command.ActiveMQQueue" >
         <constructor-arg value= "queue"  />
     </bean>
 
     <!--配置消息监听器-->
     <bean id= "consumerMessageListener"  class = "com.example.jms.consumer.ConsumerMessageListener"  />
     <!--配置消息监听容器-->
     <bean id= "jmsContainer"  class = "org.springframework.jms.listener.DefaultMessageListenerContainer" >
         <property name= "connectionFactory"  ref= "connectionFactory"  />
         <property name= "destination"  ref= "queueDestination"  />
         <property name= "messageListener"  ref= "consumerMessageListener"  />
     </bean>
</beans>

  

4.3 创建启动类

1
2
3
4
5
6
public  class  APPConsumer {
     public  static  void  main(String[] args) {
         ApplicationContext context =  new  ClassPathXmlApplicationContext( "consumer.xml" );
 
     }
}

  

5. 主题模式

5.1 配置消费者

修改consumer.xml文件

 

5.2 配置生产者

 

5.3 修改生产者



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/8047897.html,如需转载请自行联系原作者

目录
相关文章
|
消息中间件 Java Spring
spring ehcache jms activemq 分布式实现方案
spring ehcache jms activemq 分布式实现方案
116 0
|
消息中间件 存储 网络协议
Spring Boot与消息(JMS、AMQP、RabbitMQ)
1.概述。 大多应用中,可通过消息服务中间件来提升系统异步通信、扩展解耦能力。 消息服务中两个重要概念: 消息代理(message broker)和目的地(destination)。 当消息发送者发送
243 0
|
Java 数据库连接 应用服务中间件
分布式事务之Spring/JPA/JMS事务(二)
分布式事务之Spring/JPA/JMS事务
分布式事务之Spring/JPA/JMS事务(二)
|
消息中间件 搜索推荐 Java
消息中间件JMS介绍、入门demo与spring整合
消息中间件JMS介绍、入门demo与spring整合
203 0
消息中间件JMS介绍、入门demo与spring整合
|
消息中间件 Java Spring
Spring消息之JMS.
一、概念 异步消息简介     与远程调用机制以及REST接口类似,异步消息也是用于应用程序之间通信的。     RMI、Hessian、Burlap、HTTP invoker和Web服务在应用程序之间的通信机制是同步的,即客户端应用程序直接与远程服务相交互,并且一直等到远程过程完成后才继续执行。
1207 0
|
Spring Java 网络协议