Spring Boot with JMS - ActiveMQ(3)

本篇介绍ActiveMQ Topics消息方式,直接码代码演示了:

application.properties配置加入:

#spring.jms.pub-sub-domain=false # Specify if the default destination type is topic.
spring.jms.pub-sub-domain=true

消息生产者

package com.spring.boot.jms.producter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Destination;


/**
 * Description: 消息生产者
 * author: 慢慢来了
 * date:  2017/3/24 14:07
 */
@Component
public class Producter {

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    public void sendMessage(Destination destination, final String message) {
        jmsTemplate.convertAndSend(destination, message);
    }


}

消息消费者,写了二个方法监听msg.topics.demo队列

package com.spring.boot.jms.consumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * Description:
 * author: 慢慢来了
 * date:  2017/3/24 17:31
 */
@Component
public class ConsumerTopics {

    @JmsListener(destination = "msg.topics.demo")
    public void processMessage(String content) {

        System.out.println("topics a message :" + content);

    }


    @JmsListener(destination = "msg.topics.demo")
    public void processMessage2(String content) {

        System.out.println("topics a message 2 :" + content);

    }
}

编写单元测试

package com.spring.boot.jms;

import com.spring.boot.jms.producter.Producter;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.jms.Destination;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpirngBootJmsApplicationTests {

    @Autowired
    private Producter producter;

    @Test
    public void contextLoads() {
        Destination p2pMsg = new ActiveMQQueue("msg.p2p.queue");

        producter.sendMessage(p2pMsg , "hello , this is jms msg");

    }

    @Test
    public void topics(){
        Destination topics = new ActiveMQTopic("msg.topics.demo");
        producter.sendMessage(topics , "hello , this topics msg");
    }

}

结果如下:

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,268评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,999评论 6 342
  • ActiveMQ 即时通讯服务 浅析http://www.cnblogs.com/hoojo/p/active_m...
    bboymonk阅读 1,532评论 0 11
  • 今天开始写Spring Boot 整合JMS ActiveMQ,用Spring Boot 来整合也是相当的简单。A...
    慢慢来了阅读 3,694评论 0 8
  • 我很爱喝甜茶,甜茶虽不能让我体验精神上的高潮却也足够令我兴奋,甜茶就想小孩子的柔嫩的躯体。 爱喝甜茶还有一个原因,...
    ECHO最后的倔强阅读 559评论 0 3