关闭

rabbitmq 路由spring-amqp rabbit标签实现

标签: rabbitmq
451人阅读 评论(0) 收藏 举报
分类:

工程目录:


Recv   消息接收者

Send  消息发送者

MsgHandler  MsgHandlerA     消息处理程序

rabbitmq.properties   rabbitmq 的配置属性

applicationContext-producer.xml    生产者配置

applicationContext-consumer.xml   消费者配置


依赖(spring 依赖未展示):
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-erlang</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.6.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-amqp</artifactId>
            <version>1.6.1.RELEASE</version>
        </dependency>

生产者配置:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
       <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />  
       <property name="ignoreResourceNotFound" value="true" />  
       <property name="locations">  
           <list>  
               <!-- rabbitmq配置 -->  
               <value>classpath*:/rabbitmq.properties</value>  
           </list>  
       </property>  
   </bean>

   <!-- 配置连接 -->
   <rabbit:connection-factory id="connectionFactory"  channel-cache-size="2"
                              host="${rabbitmq.host}" password="${rabbitmq.password}" username="${rabbitmq.username}"
                              virtual-host="${rabbitmq.virtualHost}" port="${rabbitmq.port}"/>

   <!--配置消息模板,指定routing-key,用于发送和接收消息,exchange 会根据routing-key 的配置,发送到指定的队列中 -->
   <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
                    exchange="myExchange" routing-key="foo"/>
   <rabbit:template id="amqpTemplateA" connection-factory="connectionFactory"
                    exchange="myExchange" routing-key="fooA"/>

   <!-- RabbitAdmin 代理,自动声明queue,exchanges和绑定 -->
   <rabbit:admin connection-factory="connectionFactory" />

   <!-- 配置队列,如果不指定出队列名称,则队列名称默认为:"",也是临时队列 -->
   <rabbit:queue id="myQueue" name="${rabbitmq.queue}" auto-delete="${rabbitmq.autoDelete}" durable="${rabbitmq.durable}" exclusive="${rabbitmq.exclusive}" />
   <rabbit:queue id="myQueueA" name="${rabbitmq.queueA}" auto-delete="${rabbitmq.autoDelete}" durable="${rabbitmq.durable}" exclusive="${rabbitmq.exclusive}" />

   <!-- 交换机声明 -->
   <rabbit:direct-exchange name="myExchange" auto-delete="true" durable="false">
       <rabbit:bindings>
           <!-- 指定绑定队列及routing key -->
           <rabbit:binding queue="myQueue"  key="foo"/>
           <rabbit:binding queue="myQueueA" key="fooA"/>
       </rabbit:bindings>
   </rabbit:direct-exchange>

消费者配置:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <!-- rabbitmq配置 -->
            <value>classpath*:/rabbitmq.properties</value>
        </list>
    </property>
</bean>

<!-- 配置连接 -->
<rabbit:connection-factory id="connectionFactory"  channel-cache-size="2"
                           host="${rabbitmq.host}" password="${rabbitmq.password}" username="${rabbitmq.username}"
                           virtual-host="${rabbitmq.virtualHost}" port="${rabbitmq.port}"/>

<!-- RabbitAdmin 代理,自动声明queue,exchanges和绑定 -->
<rabbit:admin connection-factory="connectionFactory" />

<!-- 配置队列,如果不指定出队列名称,则队列名称默认为:"",也是临时队列 -->
<rabbit:queue id="myQueue" name="${rabbitmq.queue}" auto-delete="${rabbitmq.autoDelete}" durable="${rabbitmq.durable}" exclusive="${rabbitmq.exclusive}" />
<rabbit:queue id="myQueueA" name="${rabbitmq.queueA}" auto-delete="${rabbitmq.autoDelete}" durable="${rabbitmq.durable}" exclusive="${rabbitmq.exclusive}" />

<!-- 队列的消息监听器,指定消息处理的类和方法及监听的队列名称
    手动确认,acknowledge="manual"  消息处理要实现ChannelAwareMessageListener 接口实现方法,使用channel 确认消息
-->
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" prefetch="0">
    <rabbit:listener ref="msgHandler" queue-names="${rabbitmq.queue}" />
</rabbit:listener-container>

<!-- 自动确认 -->
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto" prefetch="0">
    <rabbit:listener ref="msgHandlerA" method="handleMassage" queue-names="${rabbitmq.queueA}"  />
</rabbit:listener-container>
<!-- 消息处理类 --><bean id="msgHandler" class="com.convict.rabbitmq.spring.MsgHandler" /><bean id="msgHandlerA" class="com.convict.rabbitmq.spring.MsgHandlerA" />

MsgHandler 中处理接收到的消息:

public class MsgHandler  implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        System.out.println("receive message: "+ new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}


public class MsgHandlerA {
    public void handleMassage(String msg){
        System.out.println("routing key is fooA,receive message :"+msg);
    }
}



Recv 直接加载配置,交给spring 管理bean即可:

new ClassPathXmlApplicationContext("applicationContext-consumer.xml");

Send 使用不同模板发送不同的的routing-key消息,exchange 根据不同的routing-key 分发消息到指定的队列

public static void main(String[] args) throws Exception{

    AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-producer.xml");
    //routing key 不同的两个发送消息模板
    RabbitTemplate template = (RabbitTemplate)ctx.getBean("amqpTemplate");
    RabbitTemplate templateA = (RabbitTemplate)ctx.getBean("amqpTemplateA");
    for(int i=0;i<3;i++){
        //发送消息
         template.convertAndSend("Hello World!"+i);
        templateA.convertAndSend("Hello World!"+i);

        Thread.sleep(1000);
        System.out.println("Hello World!"+i);
    }
    ctx.destroy();
}

完整代码:http://download.csdn.net/detail/convict_eva/9617878






1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37772次
    • 积分:1178
    • 等级:
    • 排名:千里之外
    • 原创:81篇
    • 转载:8篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论