电子商务社交平台源码请加企鹅求求:叁五叁六贰四柒贰五九。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();
}
}复制代码
电子商务社交平台源码请加企鹅求求:叁五叁六贰四柒贰五九