博客
关于我
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基础——MySQL数据库基本操作
    查看>>
    Python基础_列表 list
    查看>>
    python基础---->常用模块
    查看>>
    Python基础(2):控制结构
    查看>>
    Python基础 | 关于“循环”那些事
    查看>>
    python系列【仅供参考】:python 远程rdp
    查看>>
    python基于flask搭建http服务(四)—— Docker容器化部署
    查看>>
    Python块键盘/鼠标输入
    查看>>
    python在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
    查看>>
    python在gpu上运行_【python】python开启GPU加速
    查看>>
    Python在def函数中使用for循环
    查看>>
    Python在def函数中使用for循环
    查看>>
    Python在Conda环境中,但在Windows虚拟环境中没有激活
    查看>>
    python系列【仅供参考】:python pip 错误 ModuleNotFoundError: No module named pip._internal 解决办法
    查看>>
    python图像条状状噪声,使用PYTHON PIL从验证码图像中删除背景嘈杂的线条
    查看>>
    Python图像处理:从内存加载jpeg
    查看>>
    python固定后缀(名物化词汇)词频统计:抽取+统计+可视化
    查看>>
    python商品评论数据采集与分析可视化系统 Flask框架 requests爬虫 NLP情感分析 毕业设计 源码
    查看>>
    python商品数据分析可视化系统(带爬虫)京东销售数据分析 计算机毕业设计 源码下载
    查看>>
    python商品库存管理系统 django框架 商品网站 MySQL数据库 源码下载 计算机毕业设计
    查看>>