六、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 broker的URL,默认自动生成.#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.****.net/Mr_yangzc/article/details/80065374