RabbitMRabbit 消息中心_Spring Rabbitmq
来源:互联网 发布:淘宝鹰眼 编辑:程序博客网 时间:2024/04/30 06:36
Spring RabbitMQ集成
一、Spring AMQP
Spring AMQP项目将Spring的核心思想应用于基于AMQP的消息解决方案的开发上。它提供了“template”这个高度抽象来发送和接收信息。它同样提供了消息驱动的实体,这些实体存在于“listener container”容器中。这些库不但使得AMQP资源的管理变得容易,与此同时促进了依赖注入和声明式配置的使用。在所有的情况下,你将看到许多Spring框架提供的类似于JMS的便利。
这个项目包含两部分:
1、spring-amqp的基础抽象;
2、spring-rabbit这个RabbitMQ的实现
特点:
1、异步处理没有绑定消息的监听容器;
2、RabbitTemplate发送和接收消息;
3、RabbitAdmin自动声明队列,交换和绑定。
Spring AMQP使用
1)Maven管理:
<dependencies>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
</dependencies>
2)Java编码的方式:
public static void main(final String... args) throws Exception {
ConnectionFactory cf = new CachingConnectionFactory();
// set up the queue, exchange, binding on the broker
RabbitAdmin admin = new RabbitAdmin(cf);
Queue queue = new Queue("myQueue");
admin.declareQueue(queue);
TopicExchange exchange = new TopicExchange("myExchange");
admin.declareExchange(exchange);
admin.declareBinding(
BindingBuilder.bind(queue).to(exchange).with("foo.*"));
// set up the listener and container
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer(cf);
Object listener = new Object() {
public void handleMessage(String foo) {
System.out.println(foo);
}
};
MessageListenerAdapter adapter = new MessageListenerAdapter(listener);
container.setMessageListener(adapter);
container.setQueueNames("myQueue");
container.start();
//发送消息
RabbitTemplate template = new RabbitTemplate(cf);
template.convertAndSend("myExchange", "foo.bar", "Hello, world!");
Thread.sleep(1000);
container.stop();
}
3)Spring集成:
public static void main(final String... args) throws Exception {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext("context.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend("Hello, world!");
Thread.sleep(1000);
ctx.destroy();
}
public class Foo {
public void listen(String foo) {
System.out.println(foo);
}
}
<!--context.xml -->
<rabbit:connection-factory id="connectionFactory" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="myExchange" routing-key="foo.bar"/>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="myQueue" />
<rabbit:topic-exchange name="myExchange">
<rabbit:bindings>
<rabbit:binding queue="myQueue" pattern="foo.*" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="foo" method="listen" queue-names="myQueue" />
</rabbit:listener-container>
<bean id="foo" class="foo.Foo" />
二、常规配置方式
一般使用RabbitTemplate 和AmqpTemplate创建链接,他们的的关系如下:RabbitTemplate implements RabbitOperations
RabbitOperations extends AmqpTemplate
2.1 使用spring创建 connectFactory
1) 直接使用bean进行创建:
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<constructor-arg value="somehost"/>
<property name="username" value="guest"/>
<property name="password" value="guest"/>
<property name="channelCacheSize" value="25"/>
</bean>
channelCacheSize:为每一个链接创建channel个数。
Somehost:为用户从vHost的名称。
2) 使用命名空间进行创建:
<rabbit:connection-factory
id="connectionFactory" host="somehost" port="5672" channel-cache-size="25"/>
3) 使用底层抽象进行创建:
CachingConnectionFactory使用Rabbit客户端ConnectionFactory实例,一系列配置属性通过CachingConnectionFactory的相应属性设置。为了设置其他属性,可以定义一个rabbit factory实例,然后通过CachingConnectionFactory的构造函数引用这个实例。当使用上面部分所描述的命名空间时,可以配置connection-factory属性。
<rabbit:connection-factory
id="connectionFactory" connection-factory="rabbitConnectionFactory"/>
2.2 使用spring创建RoutingConnectionFactory
SimpleRoutingConnectionFactory extends AbstractRoutingConnectionFactory
AbstractRoutingConnectionFactory implements RoutingConnectionFactory
从1.3版本开始,AbstractRoutingConnectionFactory就被引入。它提供了一种机制来配置映射多个连接工厂最终通过运行时的lookupKey来决定目标连接工厂。典型的情况是通过检验线程域内的环境进行检测。为了方面,Spring AMQP提供了SimpleRoutingConnectionFactory,它会从SimpleResourceHolder中获得当前线程的lookupKey.
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.SimpleRoutingConnectionFactory>
<property name="targetConnectionFactories">
<map>
<entry key="#{connectionFactory1.virtualHost}" ref="connectionFactory1"/>
<entry key="#{connectionFactory2.virtualHost}" ref="connectionFactory2"/>
</map>
</property>
</bean>
<rabbit:template id="template" connection-factory="connectionFactory"/>
public classMyService {
@Autowired
privateRabbitTemplate rabbitTemplate;
public voidservice(String vHost, String payload) {
SimpleResourceHolder.bind(rabbitTemplate.getConnectionFactory(), vHost);
rabbitTemplate.convertAndSend(payload);
SimpleResourceHolder.unbind(rabbitTemplate.getConnectionFactory());
}
}
三、
...
- RabbitMRabbit 消息中心_Spring Rabbitmq
- Rabbitmq消息中心_消息中心总体方案
- RabbitMq消息中心_延迟发送消息
- Rabbitmq消息中心_消息追踪
- Spring Cloud刷新配置中心配置——消息总线RabbitMQ
- 消息队列RabbitMQ RabbitMQ RabbitMQ等区别
- RabbitMqx消息中心_消息中心一致性
- Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)
- 消息中心上线了
- 消息中心 NSNotificationCenter
- 消息中心的使用
- EAS消息中心
- 消息中心NSNotificationCenter理解
- 消息中心 NSNotificationCenter
- 消息中心 NSNotificationCenter
- 消息中心 NSNotificationCenter
- NSNotificationCenter消息中心
- IOS 消息中心机制
- 记录一次完整的Android Library到jcenter的过程
- 利用java反射创建通用的数据库查询方法
- 实例探索Java模式之路——抽象工厂模式
- JavaScript---对象学习(一)object对象、string对象、javaScript的constructor属性和prototype属性
- 学习笔记:正则表达式
- RabbitMRabbit 消息中心_Spring Rabbitmq
- C语言编程练习——对N行N列二维数组的每一行排序,偶数行(0当作偶数)由小到大排序,奇数行由大到小排序
- deepin下安装curl 附 CURL常用命令(转载)
- git之工作应用
- HDOJ 3480 Division(斜率优化DP)
- 【Linux】CentOS7中添加windows引导
- Mysql 5.5.49 多实例安装
- 链表中环的入口结点
- 【C编程】找出符合以下条件的Troitsky数,将该数的首位数字移动到末位数字之后得到的数是原数的整数倍