電子商務社交平臺源碼請加企鵝求求:叄五叄六貳四柒貳五九。html
這篇文章帶你瞭解怎麼整合RabbitMQ服務器,而且經過它怎麼去發送和接收消息。我將構建一個springboot工程,經過RabbitTemplate去經過MessageListenerAdapter去訂閱一個POJO類型的消息。程序員
在開始構建項目以前,機器須要安裝rabbitmq,你能夠去官網下載,www.rabbitmq.com/download.ht… ,若是你是用的Mac(程序員都應該用mac吧),你能夠這樣下載:spring
brew install rabbitmq複製代碼
安裝完成後開啓服務器:springboot
rabbitmq-server複製代碼
開啓服務器成功,你能夠看到如下信息:bash
RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc.
## ## Licensed under the MPL. See http://www.rabbitmq.com/
## ##
########## Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log
###### ## /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log
##########
Starting broker... completed with 6 plugins.複製代碼
構架一個SpringBoot工程,其pom文件依賴加上spring-boot-starter-amqp的起步依賴:服務器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>複製代碼
在任何的消息隊列程序中,你須要建立一個消息接收者,用於響應發送的消息。併發
@Component
public class Receiver {
private CountDownLatch latch = new CountDownLatch(1);
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}複製代碼
消息接收者是一個簡單的POJO類,它定義了一個方法去接收消息,當你註冊它去接收消息,你能夠給它取任何的名字。其中,它有CountDownLatch這樣的一個類,它是用於告訴發送者消息已經收到了,你不須要在應用程序中具體實現它,只須要latch.countDown()就好了。maven
在spring程序中,RabbitTemplate提供了發送消息和接收消息的全部方法。你只需簡單的配置下就好了:ide
代碼清單以下:spring-boot
package com.forezp;
import com.forezp.message.Receiver;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringbootRabbitmqApplication {
final static String queueName = "spring-boot";
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange");
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
public static void main(String[] args) {
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
}
}複製代碼
建立一個測試方法:
@Component
public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
private final ConfigurableApplicationContext context;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
ConfigurableApplicationContext context) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
this.context = context;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Sending message...");
rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
context.close();
}
}複製代碼
電子商務社交平臺源碼請加企鵝求求:叄五叄六貳四柒貳五九