RabbitMRabbit 消息中心_Spring Rabbitmq

来源:互联网 发布:淘宝鹰眼 编辑:程序博客网 时间:2024/04/30 06:36

Spring RabbitMQ集成

 

一、Spring AMQP

Spring AMQP项目将Spring的核心思想应用于基于AMQP的消息解决方案的开发上。它提供了template这个高度抽象来发送和接收信息。它同样提供了消息驱动的实体,这些实体存在于listener container容器中。这些库不但使得AMQP资源的管理变得容易,与此同时促进了依赖注入和声明式配置的使用。在所有的情况下,你将看到许多Spring框架提供的类似于JMS的便利。

这个项目包含两部分:

1spring-amqp的基础抽象;

2spring-rabbit这个RabbitMQ的实现

特点:

1、异步处理没有绑定消息的监听容器;

2RabbitTemplate发送和接收消息;

3RabbitAdmin自动声明队列,交换和绑定。

 

Spring AMQP使用

 

1Maven管理:

<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());

}

}

 

 

 

 

三、

...

0 0