zoukankan      html  css  js  c++  java
  • jms activeMQ与spring的集成(转载)

     这几天刚学习了一下消息队列,一直苦于找不到很好很简单的上手资料,苦找几天的资料,自己终于把这一块搞的差不多理解了,下面是自己的学习小demo,仅供上手,大致理解怎么跑,方便那些和我一样苦苦找寻资料的用户吧,废话不多说,直接上代码:

     

    1、首先引入activeMQ和spring的jar包 ,直接上图

    上面的jar包第一个是activeMQ的,还有spring的commos-logging,spring综合包,里面包括了spring-jms,另外还要引入slf4j的两个包  (jar包见管理--文件中的  activeMQ所需jar包 如果不能看的,要么留下联系方式我看到了发给你们,要么网上找也好找 -.-  )

     

    2、spring配置文件

    复制代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <beans xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans  
     7         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
     8         http://www.springframework.org/schema/context  
     9         http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    10 
    11 
    12     <!-- 配置JMS连接工厂 -->
    13 
    14     <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
    15         <property name="brokerURL" value="tcp://localhost:61616" />
    16     </bean>
    17 
    18     <!-- 配置JMS模版 -->
    19     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    20         <property name="connectionFactory" ref="connectionFactory" />
    21     </bean>
    22 
    23     <!-- 发送消息的目的地(一个队列) -->
    24     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
    25         <!-- 设置消息队列的名字 -->
    26         <constructor-arg index="0" value="activeMQQueue" />
    27     </bean>
    28 </beans>
    复制代码

     

    2、java代码   发送者

     

    复制代码
     1 package com.pis.activeMQ.test;
     2 
     3 import org.junit.Test;
     4 import org.springframework.context.ApplicationContext;
     5 
     6 import org.springframework.context.support.ClassPathXmlApplicationContext;
     7 
     8 import org.springframework.jms.core.JmsTemplate;
     9 
    10 import org.springframework.jms.core.MessageCreator;
    11 
    12 import javax.jms.Destination;
    13 
    14 import javax.jms.JMSException;
    15 
    16 import javax.jms.Message;
    17 
    18 import javax.jms.Session;
    19 
    20 public class MessageSender implements MessageCreator{
    21     
    22     //这里用的是单元测试来写的,执行时需要引入junit4的jar包,如果嫌麻烦,直接改成main方法也行
    23     @Test
    24     public void send(){
    25         ApplicationContext ctx = new ClassPathXmlApplicationContext("/spring/spring-jms.xml");
    26     
    27         JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
    28     
    29         Destination destination = (Destination) ctx.getBean("destination");
    30     
    31         template.send(destination, this);//此处的this是一个MessageCreator的类型,实质上调用的是MessageCreator的createMessage的方法
    32         //也可以用内部类来写,会更清晰,就不需要实现MessageCreator
    33         
    34         /*
    35         template.send(destination, new MessageCreator(){
    36 
    37             @Override
    38             public Message createMessage(Session session) throws JMSException {
    39                 return session.createTextMessage("sender发送消息!");
    40             }
    41             
    42         });*/
    43         System.out.println("发送JMS消息成功");
    44     }
    45 
    46     @Override
    47     public Message createMessage(Session session) throws JMSException {
    48         
    49         return session.createTextMessage("sender发送消息!");
    50     }
    51     
    52     
    53     
    54     
    55 }
    复制代码

     

    3、java代码   消费者

    复制代码
     1 package com.pis.activeMQ.test;
     2 
     3 import org.junit.Test;
     4 import org.springframework.context.ApplicationContext;  
     5 
     6 import org.springframework.context.support.ClassPathXmlApplicationContext;  
     7 
     8 import org.springframework.jms.core.JmsTemplate;  
     9 
    10  
    11 
    12 import javax.jms.Destination;  
    13 
    14 import javax.jms.JMSException;  
    15 
    16 import javax.jms.TextMessage; 
    17 
    18 public class MessageReceiver {
    19     
    20     
    21     //这里用的是单元测试来写的,执行时需要引入junit4的jar包,如果嫌麻烦,直接改成main方法也行
    22     @Test
    23     public void Receive() throws JMSException {  
    24         ApplicationContext ctx = new ClassPathXmlApplicationContext("/spring/spring-jms.xml");  
    25         JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
    26         Destination destination = (Destination) ctx.getBean("destination");  
    27         while (true) {
    28             TextMessage msg = (TextMessage) template.receive(destination);
    29             if (null != msg)
    30                 System.out.println("收到消息内容为: " + msg.getText());
    31             else
    32                 break;
    33 
    34         }  
    35 }  
    36     
    37 
    38     
    39 }
    复制代码

     

    启动activeMQ服务,连续运行两次发送者,然后再运行一次接受者,可以从http://localhost:8161/admin/queues.jsp去查看消息队列的消息发送接受情况,如下图所示:

     

    运行MessageSender 两次

     

    查看activeMQ管理界面:可以看到有2条消息

     

    运行MessageReceiver,接受两条消息

     

    再次查看activeMQ管理界面:可以看到2条消息已经接收

     

    OK! 上面就是最简单的activeMQ的配置了,绝对可以跑起来,跑起来的代码才是王道,大家一起学习了!

  • 相关阅读:
    NO29 用户提权sudo配置文件详解实践--志行为审计
    NO28 第四关考试题
    NO27 定时任务
    NO26 Linux的文件权限--chmod--Linux删除文件说明--suid--sgid
    NO25 三剑客之SED行天下
    NO24 第三关--企业面试题
    gcc编译错误表
    C++的精髓——虚函数
    gcc 优化选项 -O1 -O2 -O3 -Os 优先级,-fomit-frame-pointer
    正确使用#include和前置声明(forward declaration)
  • 原文地址:https://www.cnblogs.com/chenying99/p/2744108.html
Copyright © 2011-2022 走看看