本文共 7389 字,大约阅读时间需要 24 分钟。
spring.activemq.broker-url=tcp://localhost:61616 # 消息代理路径spring.activemq.user=spring.activemq.password=spring.activemq.in-memory=truespring.activemq.pooled=false
spring.rabbitmq.host=localhost # 服务器地址,默认是 localhostspring.rabbitmq.port=5672 # 端口,默认是 5672spring.rabbitmq.username=adminspring.rabbitmq.password=secret
docker pull cloudesire/activemq
docker run -d -p 61616:61616 -p 8161:8161 cloudesire/activemq
启动容器;其中 61616 是消息代理端口,8161 是管理界面的端口,在用 VirtualBOX 做好端口映射后,可以用浏览器打开 localhost:8161 登录管理界面,默认账号密码为 admin。 org.springframework spring-jms org.apache.activemq activemq-client
logging.file=log.logspring.activemq.broker-url=tcp://localhost:61616
package com.pyc.activemq;import org.springframework.jms.core.MessageCreator;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;public class Msg implements MessageCreator { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage("A message of testing"); }}
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.jms.core.JmsTemplate;@SpringBootApplicationpublic class ActivemqApplication implements CommandLineRunner { // implement CommandLinerRunner and overwrite Run method //so that can execute our code when program start @Autowired JmsTemplate jmsTemplate; @Override public void run(String... args) throws Exception { // 向一个叫 my-destination 的目的端发送消息。 jmsTemplate.send("my-destination", new Msg()); } public static void main(String[] args) { SpringApplication.run(ActivemqApplication.class, args); }}
package com.pyc.activemq;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;@Componentpublic class Receiver { @JmsListener(destination = "my-destination") public void receiverMessage(String message){ System.out.println("接收到:<" + message + ">"); }}
docker pull rabbitmq:3-management
将 RabbitMQ 镜像下载到本地。docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:3-management
启动容器,两个端口中 5672 是消息代理端口,15672 是 Web 管理界面的端口,用 VirtualBox 做好端口映射后,用浏览器访问 localhost:15672 打开管理界面的登录界面,默认账户和密码是 guest,登录成功后在还未进行消息代理时的界面如下: 4.0.0 org.springframework.boot spring-boot-starter-parent 1.3.0.M2 com.pyc rabbitmq 0.0.1-SNAPSHOT rabbitmq RabbitMQ Project 1.8 UTF-8 org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone spring-milestones Spring Milestones https://repo.spring.io/milestone
package com.pyc.rabbitmq;import org.springframework.amqp.core.Queue;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class RabbitmqApplication implements CommandLineRunner { // using rabbit template @Autowired RabbitTemplate template; // define destination also called queue, named my-queue @Bean public Queue pycQueue(){ return new Queue("my-queue"); } @Override public void run(String... args) throws Exception { // using convertAndSend method to send message template.convertAndSend("my-queue", "A greet message come from RabbitMQ"); } public static void main(String[] args) { SpringApplication.run(RabbitmqApplication.class, args); }}
package com.pyc.rabbitmq;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;@Componentpublic class Receiver { @RabbitListener(queues = "my-queue") public void receiverMessage(String message){ System.out.println("Received: <" + message + ">"); }}
转载地址:http://dlqgn.baihongyu.com/