RabbitMQ:Spring + RabbitMQ整合发送同步消息
以下实现使用Exchange类型为DirectExchange【模板默认转发器】. routingkey【模板默认选择键】的名称默认为Queue的名称,运行生产者向队列中发送一条消息,再运行消费者消费消息。
注意:事先要在RabbitMQ中增添QUEUE_NAME 队列名称
1.【maven依赖jar包】:
UTF-8
4.1.0.RELEASE
1.1.1.RELEASE
3.2.4
junit
junit
4.10
com.dangdang
sharding-jdbc-core
1.0.0
org.springframework
spring-orm
${spring.version}
commons-dbcp
commons-dbcp
1.4
org.mybatis
mybatis-spring
1.2.2
org.mybatis
mybatis
${mybatis.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-tx
${spring.version}
mysql
mysql-connector-java
5.1.28
log4j
log4j
1.2.16
org.slf4j
slf4j-log4j12
1.7.5
org.elasticsearch.client
transport
5.6.3
com.google.code.gson
gson
2.8.0
org.apache.logging.log4j
log4j-core
2.8.2
com.rabbitmq
amqp-client
3.6.5
org.springframework.amqp
spring-rabbit
1.7.5.RELEASE
org.springframework
spring-core
${spring.version}
commons-logging
commons-logging
org.springframework.amqp
spring-amqp
${spring.amqp.version}
sources
compile
commons-lang
commons-lang
2.6
org.slf4j
slf4j-api
1.5.10
org.slf4j
jcl-over-slf4j
1.5.10
runtime
org.slf4j
slf4j-log4j12
1.5.10
runtime
log4j
log4j
1.2.14
runtime
org.aspectj
aspectjweaver
1.6.9
2. 【配置文件】:
application.properties:
#============== rabbitmq config ====================
rabbit.hosts=127.0.0.1
rabbit.username=
rabbit.password=
rabbit.virtualHost=/
rabbit.queue=spring.queue.sync
rabbit.routingKey=spring.queue.sync
applicationContext.xml:
Spring公共配置
classpath*:application.properties
applicationContext-rabbitmq-sync.xml :
3.【生产者和消费者】:
生产者Producer:
package com.caox.rabbitmq.demo._08_spring_rabbitmq_sync;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by nazi on 2018/7/27.
*/
public class Producer {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
amqpTemplate.convertAndSend("test spring sync");
}
}
消费者Consumer:
package com.caox.rabbitmq.demo._08_spring_rabbitmq_sync;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by nazi on 2018/7/27.
* 消费者
*/
public class Consumer {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
System.out.println("Received: " + amqpTemplate.receiveAndConvert());
}
}
1.【注解配置】:
AnnotationConfiguration:
package com.caox.rabbitmq.demo._08_spring_rabbitmq_sync_annotation;
import com.rabbitmq.client.AMQP;
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;
/**
* Created by nazi on 2018/7/27.
*/
@Configuration
public class AnnotationConfiguration {
//指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
protected String springQueueDemo = "create.world.queue";
//创建链接
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1");
connectionFactory.setUsername("caoxia");
connectionFactory.setPassword("caoxia123456");
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 new 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));
}*/
}
2【生产者Producer】:
package com.caox.rabbitmq.demo._08_spring_rabbitmq_sync_annotation;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by nazi on 2018/7/27.
*/
public class Producer {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
// 声明创建队列
AmqpAdmin amqpAdmin = context.getBean(AmqpAdmin.class);
Queue helloWorldQueue = new Queue("create.world.queue");
amqpAdmin.declareQueue(helloWorldQueue);
AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
amqpTemplate.convertAndSend("Hello World");
System.out.println("Sent: Hello World");
}
}
3.【消费者 Consumer】:
package com.caox.rabbitmq.demo._08_spring_rabbitmq_sync_annotation;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by nazi on 2018/7/27.
*/
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());
}
}