上一篇文章我们使用spring + redis实现消息的订阅发布,但是redis作为消息组件仅适合轻量级的任务处理,例如:秒杀计数器、缓存等,对于重量级,高并发的处理redis就稍显劣势。接下来我们spring+RabbiteMQ的实现。
准备
首先安装ErLang环境,因为RabbiteMQ依赖ErLang,这里我们使用windows环境,因为都有exe。
Erlang下载地址
RabbiteMQ下载地址
编码
首先添加依赖jar包:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>1.5.4.RELEASE </version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId> spring-rabbit</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
接下来同上一篇文章一样添加消息接收类Receiver:
import java.util.concurrent.CountDownLatch;
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;
}
}
最后来看一下消息的发送Application:
package rabbitmq;
import java.util.concurrent.TimeUnit;
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.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
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.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import ch.qos.logback.core.Context;
@SpringBootApplication
public class Application implements CommandLineRunner {
final static String queueName = "spring-boot";
@Autowired
RabbitTemplate rabbitTemplate;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
AnnotationConfigApplicationContext context(){
return new AnnotationConfigApplicationContext();
}
@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
Receiver receiver() {
return new Receiver();
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("Waiting five seconds...");
Thread.sleep(5000);
System.out.println("Sending message...");
rabbitTemplate.convertAndSend(queueName, "Hello from RabbitMQ!");
receiver().getLatch().await(10000, TimeUnit.MILLISECONDS);
}
}
最后一起看一下运行的结果:
Sending message...
Received <Hello from RabbitMQ!>