博客
关于我
Spirng集成ActiveMQ
阅读量:548 次
发布时间:2019-03-09

本文共 4637 字,大约阅读时间需要 15 分钟。

使用Spring集成ActiveMQ的队列模式

配置Spring与AMQ的队列模式

队列模式配置

  • 先导入相关依赖。
  • 在Maven项目中添加以下依赖:

    org.apache.activemq
    activemq-all
    5.7.0
    org.apache.activemq
    activemq-core
    5.7.0
    org.springframework
    spring-jms
    4.2.5.RELEASE
    org.apache.activemq
    activemq-pool
    5.7.0
    org.springframework
    spring-context
    4.2.5.RELEASE
    org.springframework
    spring-test
    4.2.5.RELEASE
    test
    1. Spring与AMQ的配置文件

      tcp://192.168.1.6:61616
    2. 发送者配置

    3. 消息监听器配置

    4. 接口定义

      package com.zzf.spring.jms.service;public interface ProducerService {    void sendMsg(String message);}
    5. 接口实现

      package com.zzf.spring.jms.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Service;import javax.annotation.Resource;import javax.jms.Destination;import javax.jms.Message;@Servicepublic class ProducerServiceImpl implements ProducerService {    @Autowired    private JmsTemplate jmsTemplate;    @Resource(name = "destinationQueue")    private Destination destination;    public void sendMsg(final String message) {        jmsTemplate.send(destination, new MessageCreator() {            public Message createMessage(Session session) throws JMSException {                TextMessage textMessage = session.createTextMessage(message);                return textMessage;            }        });        System.out.println("发送消息: " + message);    }}
    6. 启动类(生产者)

      package com.zzf.spring.jms.service;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppProducer {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-amq-producer.xml");        ProducerService producerService = context.getBean("producerServiceImpl");        for (int i = 0; i < 10; i++) {            producerService.sendMsg("Spring-test=" + i);        }        context.close();    }}
    7. 消费者配置

      package com.zzf.spring.jms.service;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppConsumer {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-amq-consumer.xml");    }}
    8. 此外,对于主题模式,需要在spring-amq-commen.xml中添加主题配置,并修改producer.xml和consumer.xml以使用主题模式。以下是主题配置示例:

      tcp://192.168.1.6:61616

      在producer.xml中,修改相关配置以支持主题模式:

      然后在AppTopicProducer启动类中添加主题发送配置:

      public class AppTopicProducer {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-amq-producer.xml");        ProducerService producerService = context.getBean("producerServiceImpl");        for (int i = 0; i < 10; i++) {            producerService.sendMsgTopic("Spring-topic-test=" + i);        }        context.close();    }}

      消费者启动类同样修改:

      public class AppTopicConsumer {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-amq-topic.xml");    }}

      通过以上配置,您可以实现Spring与ActiveMQ的队列模式和主题模式的消息传输。发布者和订阅者可以分别启动对应的配置文件,从而实现消息的发送和接收。

    转载地址:http://pmmsz.baihongyu.com/

    你可能感兴趣的文章
    python中一个好的持久同步队列
    查看>>
    Python中不准确的对数
    查看>>
    python中什么是实例方法_在Python中创建实例方法对象的不同方法
    查看>>
    Python中使用cutecharts实现简单的手绘风格的图表
    查看>>
    python中使用dxfgrabber,解析dxf文件信息生成obj文件
    查看>>
    Python中使用ncmbot玩转网易云音乐
    查看>>
    Python中使用PIL快速实现二值图
    查看>>
    Python中使用PIL快速实现灰度图
    查看>>
    Python中使用pip install ncmbot时各种错误解决办法
    查看>>
    Python中使用requests和parsel爬取喜马拉雅电台音频
    查看>>
    Python中使用turtle绘制多个同心圆
    查看>>
    Python中使用turtle绘制棋盘详解
    查看>>
    Python中使用wordcloud生成漂亮的中文词云
    查看>>
    Python中使用wxpy定时机器人给微信好友发送每日一文
    查看>>
    Python中使用zip()并行迭代
    查看>>
    Python中使用推导式创建序列
    查看>>
    Python中信号处理程序和atexit处理程序的调用
    查看>>
    Python中关于txt的简单读写模式与操作
    查看>>
    Python中关于类和函数的初体验之"__init__"和"__str__"不是"_init_"和"_str_"
    查看>>
    python中函数接受参数为*args, **kwargs怎么办
    查看>>