博客
关于我
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实现FigurateNumber垛积数算法(附完整源码)
    查看>>
    Objective-C实现finding bridges寻找桥梁算法(附完整源码)
    查看>>
    Objective-C实现first come first served先到先得算法(附完整源码)
    查看>>
    Objective-C实现FIR滤波器(附完整源码)
    查看>>
    Objective-C实现fischer yates shuffle洗牌算法(附完整源码)
    查看>>
    Objective-C实现FisherYates Shuffle洗牌算法(附完整源码)
    查看>>
    Objective-C实现fisherYates洗牌算法(附完整源码)
    查看>>
    Objective-C实现Floyd-Warshall算法(附完整源码)
    查看>>
    Objective-C实现FPmax算法(附完整源码)
    查看>>
    Objective-C实现frequency finder频率探测器算法(附完整源码)
    查看>>
    Objective-C实现FTP上传文件(附完整源码)
    查看>>
    Objective-C实现FTP文件上传(附完整源码)
    查看>>
    Objective-C实现FTP文件下载(附完整源码)
    查看>>
    Objective-C实现fuzzy operations模糊运算算法(附完整源码)
    查看>>
    Objective-C实现Gale-Shapley盖尔-沙普利算法(附完整源码)
    查看>>
    Objective-C实现gamma recursive伽玛递归算法(附完整源码)
    查看>>
    Objective-C实现gamma 伽玛功能算法(附完整源码)
    查看>>
    Objective-C实现gauss easte高斯复活节日期算法(附完整源码)
    查看>>
    Objective-C实现gaussian filter高斯滤波器算法(附完整源码)
    查看>>
    Objective-C实现gaussian naive bayes高斯贝叶斯算法(附完整源码)
    查看>>