博客
关于我
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.xmlconsumer.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/

    你可能感兴趣的文章
    Objective-C实现haversine distance斜距算法(附完整源码)
    查看>>
    Objective-C实现heap sort堆排序算法(附完整源码)
    查看>>
    Objective-C实现heaps algorithm堆算法(附完整源码)
    查看>>
    Objective-C实现heap堆算法(附完整源码)
    查看>>
    Objective-C实现Heap堆算法(附完整源码)
    查看>>
    Objective-C实现hexagonal numbers六边形数算法(附完整源码)
    查看>>
    Objective-C实现hidden layers neural network浅层神经网络算法(附完整源码)
    查看>>
    Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
    查看>>
    Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
    查看>>
    Objective-C实现Hill密码加解密算法(附完整源码)
    查看>>
    Objective-C实现histogram stretch直方图拉伸算法(附完整源码)
    查看>>
    Objective-C实现Hopcroft算法(附完整源码)
    查看>>
    Objective-C实现horizontal projectile motion平抛运动算法(附完整源码)
    查看>>
    Objective-C实现hornerMethod霍纳法算法(附完整源码)
    查看>>
    Objective-C实现Horn–Schunck光流算法(附完整源码)
    查看>>
    Objective-C实现Http Post请求(附完整源码)
    查看>>
    Objective-C实现http下载文件 (附完整源码)
    查看>>
    Objective-C实现Http协议下载文件(附完整源码)
    查看>>
    Objective-C实现huffman哈夫曼编码算法(附完整源码)
    查看>>
    Objective-C实现ID3贪心算法(附完整源码)
    查看>>