安裝RabbitMQ server
瀏覽器訪問(wèn)https://www.rabbitmq.com/install-standalone-mac.html仆葡,下載RabbitMQ server
-
解壓下載的文件
tar -zxvf rabbitmq-server-mac-standalone-3.6.6.tar.xz
進(jìn)入解壓得到的文件夾中的sbin目錄
執(zhí)行 ./rabbitmq-server
按CRTL+C關(guān)閉啟動(dòng)的server
./rabbitmq-plugins enable rabbitmq_management
./rabbitmq-server
通過(guò)瀏覽器打開(kāi)[http://localhost:15672/](http://localhost:15672/)
用戶名和密碼 是 guest/guest
安裝RabbtMQ Server操作詳細(xì)見(jiàn)http://blog.csdn.net/mrzhangxl/article/details/53114616
編寫(xiě)代碼
- 先看一下項(xiàng)目目錄
image.png
編寫(xiě)amqp-sender
-
在pom.xml中引入amqp依賴包
<dependencies> <!-- ampq 依賴包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
-
配置application.yml
spring: rabbitmq: host: localhost port: 5672 username: guest password: guest
-
編寫(xiě)amqp-sender 配置類
@Configuration public class RabbitMQConfig { public static final String QUEUE_NAME = "spring-boot"; public static final String QUEUE_EXCHANGE_NAME = "spring-boot-exchange"; @Bean public Queue queue() { // 是否持久化 boolean durable = true; // 僅創(chuàng)建者可以使用的私有隊(duì)列,斷開(kāi)后自動(dòng)刪除 boolean exclusive = false; // 當(dāng)所有消費(fèi)客戶端連接斷開(kāi)后研乒,是否自動(dòng)刪除隊(duì)列 boolean autoDelete = false; return new Queue(QUEUE_NAME, durable, exclusive, autoDelete); } @Bean public TopicExchange exchange() { // 是否持久化 boolean durable = true; // 當(dāng)所有消費(fèi)客戶端連接斷開(kāi)后乏盐,是否自動(dòng)刪除隊(duì)列 boolean autoDelete = false; return new TopicExchange(QUEUE_EXCHANGE_NAME, durable, autoDelete); } @Bean public Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME); } }
-
編寫(xiě)sender消息發(fā)送功能類
@Service public class Sender { @Autowired private AmqpTemplate rabbitTemplate; public void send() { System.out.println("ELSE 發(fā)送消息..."); rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, "你好佳窑, ELSE!"); } }
-
編寫(xiě)amqp-sender測(cè)試類
@RunWith(SpringRunner.class) @SpringBootTest public class AmqpSenderApplicationTests { @Autowired private Sender sender; @Test public void send() throws Exception { sender.send(); } }
編寫(xiě)amqp-receiver
-
配置application.yml
server: port: 8081 tomcat: uri-encoding: UTF-8 spring: rabbitmq: host: localhost port: 5672 username: guest password: guest
-
編寫(xiě)amqp-receiver 配置類
@Configuration public class RabbitMQConfig { public static final String QUEUE_NAME = "spring-boot"; @Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(QUEUE_NAME); container.setMessageListener(listenerAdapter); return container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); } }
-
編寫(xiě)receiver消息接收功能類
@Service public class Receiver { @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME) public void receiveMessage(String message) { System.out.println("Received <" + message + ">"); } }
代碼地址:https://git.oschina.net/dream-maker/amqp-demo
更多相關(guān)文章:http://www.tuicool.com/articles/vYNVV3I