六、springboot+ activemq

1.创建工程

idea  链接:https://pan.baidu.com/s/1tTY0JBmmqk-GVwm4Q-uxDg 密码:ozjz

file--> new -->project

spring initializr--> 选择默认的  default  -->  next

填好项目信息 --> next 

到这里项目就搭建完成了。

打开 application.properties 文件 添加  spring.activemq.in-memory=true  spring.activemq.pool.enabled=false

 

spring.activemq.in-memory=true
#是否是内存模式,默认为true.
spring.activemq.pool.enabled=false
#是否创建PooledConnectionFactory,而非ConnectionFactory,默认false

#spring.activemq.broker-url= tcp://localhost:61616

#指定ActiveMQ brokerURL,默认自动生成.#spring.activemq.password#指定broker的密码.#spring.activemq.user#指定broker的用户.

如果使用外部 mq 开启broker-url  将in-memory设置为false

1. 生产者

 

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.MessageNotWriteableException;
import javax.jms.Queue;
import javax.jms.Topic;

/**
 * @author Yang
 * @create 2018-04-23 16:50
 * 描述:
 */
@RestController
public class Producer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;


    @RequestMapping("sendqueue")
    public void sendQueue(String msg) throws MessageNotWriteableException {
        final Queue queue = new ActiveMQQueue("queue");
        jmsMessagingTemplate.convertAndSend(queue, msg);

    }

    @RequestMapping("sendtopic")
    public void sendTopic(String msg) throws MessageNotWriteableException {
        final Topic topic = new ActiveMQTopic("topic");
        jmsMessagingTemplate.convertAndSend(topic, msg);

    }

    @JmsListener(destination = "returnmsg")

    public void returnmsg(String msg) {

        System.out.println("*********" + msg + "*************");
    }


}

2.消费者

@JmsListener(destination="queue)  监听

@SendTo("returnmsg"),该注解的意思是将return回的值,再发送的"returnmsg"队列中

 

import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

/**
 * @author Yang
 * @create 2018-04-23 16:54
 * 描述:
 */
@Component
public class Consumer {

    @JmsListener(destination = "queue")
    @SendTo("returnmsg")
    public String receiveQueue(String msg) {
        System.out.println("==============" + msg + "==================");
        return "老铁我收到了"+msg;
    }

    @JmsListener(destination = "topic")
    @SendTo("returnmsg")
    public String receiveTopic(String msg){
        System.out.println("==topic=====" + msg + "=======topic====");
        return "topic"+msg;
    }

    @JmsListener(destination = "topic")
    public void receiveTopic2(String msg){
        System.out.println("==topic2=====" + msg + "=======topic2====");
    }
}

启动时 添加@EnableJms      @EnableJms会启动jms的注解扫描,相当于<jms:annotation-d riven/>

 

@SpringBootApplication
@EnableJms
public class DemoApplication {


   public static void main(String[] args) {

      SpringApplication.run(DemoApplication.class, args);
   }
}

 

队列(queue):

http://localhost:8080/sendqueue?msg=hello%20mq

测试 topic

需要在  application.properties  添加 

 
spring.jms.pub-sub-domain=true
#如果为True,则是Topic;如果是false或者默认,则是queue。

http://localhost:8080/sendtopic?msg=hello%20mq%20topic

queue 和topic 二者不共存的,需要让 queue与topic共存 请看下篇文章

七、springboot+activemq topic与 queue 并存实现方案

https://blog.csdn.net/Mr_yangzc/article/details/80065374

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值