RabbitMQ入门-10-2(整合spring-发送同步消息注解实现)
2017-12-12 14:26
781 查看
写在前面的话
系列教程都是从网络上收集和本人的理解所编辑而成,仅供广大爱好者学习所用,请尊重本人的劳动成果。欢迎评论指正和转帖!(请保留连接谢谢!)
一、项目结构
![](https://oscdn.geek-share.com/Uploads/Images/Content/201712/24d86e2733862027461d1d67018e9a86)
二、POM.XML
![](https://oscdn.geek-share.com/Uploads/Images/Content/201712/4b885330e601d8e2c47db2c6244731e1)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201712/40a168f719e58e854e774b4276cd6bf5)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201712/810f1ad337f8c0fc25f647ce797cfb8e)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201712/1b289d1dca32a34f1cd5473fc9ff6468)
三、Producer.java
四、Consumer.java
五、AnnotationConfiguration.java
package com.rabbitmq;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.rabbitmq.client.AMQP;
@Configuration
public class AnnotationConfiguration {
//指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
protected String springQueueDemo = "spring-queue-demo";
//创建链接
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.0.117");
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setPort(AMQP.PROTOCOL.PORT);
return connectionFactory;
}
//创建rabbitAdmin 代理类
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
//创建rabbitTemplate 消息模板类
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
//The routing key is set to the name of the queue by the broker for the default exchange.
template.setRoutingKey(this.springQueueDemo);
//Where we will synchronously receive messages from
template.setQueue(this.springQueueDemo);
return template;
}
//
// Every queue is bound to the default direct exchange
public Queue helloWorldQueue() {
return ne
9ea1
w Queue(this.springQueueDemo);
}
/*
@Bean
public Binding binding() {
return declare(new Binding(helloWorldQueue(), defaultDirectExchange()));
}*/
/*
@Bean
public TopicExchange helloExchange() {
return declare(new TopicExchange("hello.world.exchange"));
}*/
/*
public Queue declareUniqueQueue(String namePrefix) {
Queue queue = new Queue(namePrefix + "-" + UUID.randomUUID());
rabbitAdminTemplate().declareQueue(queue);
return queue;
}
// if the default exchange isn't configured to your liking....
@Bean Binding declareP2PBinding(Queue queue, DirectExchange exchange) {
return declare(new Binding(queue, exchange, queue.getName()));
}
@Bean Binding declarePubSubBinding(String queuePrefix, FanoutExchange exchange) {
return declare(new Binding(declareUniqueQueue(queuePrefix), exchange));
}
@Bean Binding declarePubSubBinding(UniqueQueue uniqueQueue, TopicExchange exchange) {
return declare(new Binding(uniqueQueue, exchange));
}
@Bean Binding declarePubSubBinding(String queuePrefix, TopicExchange exchange, String routingKey) {
return declare(new Binding(declareUniqueQueue(queuePrefix), exchange, routingKey));
}*/
}
写在后面的话The worldi is like mirror:Frown at itand it frowns at you;smile,and it smiles too.世界犹如一面镜子:朝它皱眉它就朝你皱眉,朝它微笑它就朝你微笑。
系列教程都是从网络上收集和本人的理解所编辑而成,仅供广大爱好者学习所用,请尊重本人的劳动成果。欢迎评论指正和转帖!(请保留连接谢谢!)
一、项目结构
二、POM.XML
三、Producer.java
package com.rabbitmq; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Producer { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfiguration.class); AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class); amqpTemplate.convertAndSend("Hello World"); System.out.println("Sent: Hello World"); } }
四、Consumer.java
package com.rabbitmq; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Consumer { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfiguration.class); AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class); System.out.println("Received: " + amqpTemplate.receiveAndConvert()); } }
五、AnnotationConfiguration.java
package com.rabbitmq;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.rabbitmq.client.AMQP;
@Configuration
public class AnnotationConfiguration {
//指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
protected String springQueueDemo = "spring-queue-demo";
//创建链接
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.0.117");
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setPort(AMQP.PROTOCOL.PORT);
return connectionFactory;
}
//创建rabbitAdmin 代理类
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
//创建rabbitTemplate 消息模板类
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
//The routing key is set to the name of the queue by the broker for the default exchange.
template.setRoutingKey(this.springQueueDemo);
//Where we will synchronously receive messages from
template.setQueue(this.springQueueDemo);
return template;
}
//
// Every queue is bound to the default direct exchange
public Queue helloWorldQueue() {
return ne
9ea1
w Queue(this.springQueueDemo);
}
/*
@Bean
public Binding binding() {
return declare(new Binding(helloWorldQueue(), defaultDirectExchange()));
}*/
/*
@Bean
public TopicExchange helloExchange() {
return declare(new TopicExchange("hello.world.exchange"));
}*/
/*
public Queue declareUniqueQueue(String namePrefix) {
Queue queue = new Queue(namePrefix + "-" + UUID.randomUUID());
rabbitAdminTemplate().declareQueue(queue);
return queue;
}
// if the default exchange isn't configured to your liking....
@Bean Binding declareP2PBinding(Queue queue, DirectExchange exchange) {
return declare(new Binding(queue, exchange, queue.getName()));
}
@Bean Binding declarePubSubBinding(String queuePrefix, FanoutExchange exchange) {
return declare(new Binding(declareUniqueQueue(queuePrefix), exchange));
}
@Bean Binding declarePubSubBinding(UniqueQueue uniqueQueue, TopicExchange exchange) {
return declare(new Binding(uniqueQueue, exchange));
}
@Bean Binding declarePubSubBinding(String queuePrefix, TopicExchange exchange, String routingKey) {
return declare(new Binding(declareUniqueQueue(queuePrefix), exchange, routingKey));
}*/
}
写在后面的话The worldi is like mirror:Frown at itand it frowns at you;smile,and it smiles too.世界犹如一面镜子:朝它皱眉它就朝你皱眉,朝它微笑它就朝你微笑。
相关文章推荐
- RabbitMQ入门-10-3(整合spring-发送异步消息注解实现)
- RabbitMQ学习之spring整合发送同步消息(注解实现)
- RabbitMQ学习(十)之spring整合发送同步消息(注解实现)
- (转) RabbitMQ学习之spring整合发送同步消息(注解实现)
- RabbitMQ入门-10-1(整合spring-发送同步消息)
- [RabbitMQ]15_RabbitMQ学习之spring整合发送同步消息(注解实现)
- (转)RabbitMQ学习之spring整合发送异步消息(注解实现)
- RabbitMQ入门-10(整合spring-发送异步消息)
- RabbitMQ学习(十二)之spring整合发送异步消息(注解实现)
- RabbitMQ学习之spring整合发送异步消息(注解实现)
- RabbitMQ学习之spring整合发送同步消息
- RabbitMQ学习(九)之spring整合发送同步消息
- (转)RabbitMQ学习之spring整合发送同步消息
- [RabbitMQ]14_RabbitMQ学习之spring整合发送同步消息
- (转) RabbitMQ学习之spring整合发送异步消息
- SpringBoot整合RabbitMQ实现微服务间的异步消息沟通
- spring整合apache activemq实现消息发送的三种方式代码配置实例
- Spring Cloud 入门教程(十):和RabbitMQ的整合 -- 消息总线Spring Cloud Netflix Bus
- spring整合JMS一同步收发消息(基于ActiveMQ的实现)
- spring boot整合spring-kafka实现发送接收消息实例代码