博客
关于我
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+requests+unittest执行自动化接口测试!
    查看>>
    Python+Requests编码识别Bug
    查看>>
    python函数编写_[零基础学python]传说中的函数编写条规
    查看>>
    Python+selenium —— UI自动化之鼠标操作
    查看>>
    python函数注释,参数后面加冒号:,函数后面的箭头→是什么?
    查看>>
    Python+Selenium+Threading进行兼容性测试
    查看>>
    Python+selenium+unittest的GUI自动化框架实现
    查看>>
    python函数拟合不规则曲线_Python计算&绘图——曲线拟合问题(转)
    查看>>
    python函数定义的基本格式_零基础学python-2.19 定义函数、调用函数与默认参数
    查看>>
    Python+Selenium之数据驱动测试的实现
    查看>>
    python函数定义与使用+返回值简解
    查看>>
    Python+Selenium登录
    查看>>
    Python日期时间模块
    查看>>
    Python函数合集:足足68个内置函数请收好!
    查看>>
    Python+Selenium自动化测试项目实战
    查看>>
    Python+Selenium自动化测试项目实战【建议收藏】
    查看>>
    Python+Selenium自动化测试:Page Object模式
    查看>>
    python+selenium进行cnblog的自动化登录测试
    查看>>
    Python函数只返回第一个值而不是数据框
    查看>>
    Python+SQL实战:京东用户行为数据分析案例解析(上)
    查看>>