rabbitmq学习9:使用spring-amqp发送消息及同步接收消息

    通过对spring-amqp看重要类的认识,下面来通过spring-amqp的发送消息及同步接收消息是如何实现的。有兴趣的朋友 可以去spring-amqp官网下载例子。

    先来看看HelloWorldConfiguration类

Java代码  收藏代码
package org.springframework.amqp.helloworld; import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.SingleConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class HelloWorldConfiguration extends AbstractRabbitConfiguration { protected final String helloWorldQueueName = "hello.world.queue"; @Bean
public ConnectionFactory connectionFactory() {
SingleConnectionFactory connectionFactory = new SingleConnectionFactory(
"localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
} @Override
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.helloWorldQueueName);
// // Where we will synchronously receive messages from
template.setQueue(this.helloWorldQueueName);
return template;
} @Bean
public Queue helloWorldQueue() {
return new Queue(this.helloWorldQueueName);
}
}
此类定义了ConnectionFactory 、RabbitTemplate 、Queue 发送消息的程序如下: Java代码 收藏代码
package org.springframework.amqp.helloworld; 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(HelloWorldConfiguration.class);
AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
amqpTemplate.convertAndSend("Hello World");
System.out.println("Sent: Hello World");
} }
同步接收消息如下: Java代码 收藏代码
package org.springframework.amqp.helloworld; 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(HelloWorldConfiguration.class);
AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
System.out.println("Received: " + amqpTemplate.receiveAndConvert());
} } 这个例子是Exchange类型为DirectExchange. routingkey的名称默认为Queue的名称。 对于 HelloWorldConfiguration类的配置,也可以通过SPRING XML文件来配置。例如 rabbitConfiguration.xml Java代码 收藏代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 创建connectionFactory -->
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
<constructor-arg value="localhost" />
<property name="username" value="guest" />
<property name="password" value="guest" />
</bean>
<bean id="rabbitAdmin"
class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>
<bean id="rabbitTemplate"
class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<constructor-arg ref="connectionFactory"></constructor-arg>
<property name="queue" value="hello.world.queue"></property>
<property name="routingKey" value="hello.world.queue"></property>
</bean> <!-- 声明Queue并设定Queue的名称 -->
<bean id="helloWorldQueue"
class="org.springframework.amqp.core.Queue">
<constructor-arg value="hello.world.queue"></constructor-arg>
</bean> </beans>

最新文章

  1. Day20160425
  2. cacti添加主机监控
  3. backbone &amp; django csrf_token的问题
  4. Bellman-Ford算法判负环
  5. Candy Distribution_找规律
  6. Apache CXF实现Web Service(5)—— GZIP使用
  7. 使用JDBC进行数据库的事务操作(1)
  8. SecurityError:Error #2048:安全沙箱冲突
  9. 树链剖分模板(BZOJ3083)
  10. Qzone React Native改造
  11. Linux之使用网络
  12. vue国际化
  13. spring boot+mybatis+mysql
  14. html回顾随笔JS(*^__^*)
  15. :before与::before的区别
  16. SpringMVC初探-HelloWorld
  17. 让java从Mysql返回多个ResultSet
  18. HDU2976 Dropping tests 2017-05-11 18:10 39人阅读 评论(0) 收藏
  19. winform 批量控件取值赋值
  20. IT之路如何走得更远

热门文章

  1. 聚类结果的评估指标及其JAVA实现
  2. 使用Donut Caching和Donut Hole Caching在ASP.NET MVC应用中缓存页面
  3. Django中级篇(上)
  4. 怎样去掉FireFox的导入向导
  5. NOIP2003 传染病防治
  6. No.008 String to Integer (atoi)
  7. No.001 Two Sum
  8. ASPxCallback控件
  9. 简化对象extend拓展
  10. CSS3 伪类选择器 :nth-child()