RabbitMQ与spring集成,配置完整的生产者和消费者

waitzkj 2019-11-03

RabbitMQ与AMQP协议详解可以看看这个 http://www.cnblogs.com/franky...

下面是rabbitMQ和spring集成的配置,我配置了二种ExCahange: topicExchange和directExChange

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
       http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">  
  
    <description>Spring公共配置 </description>  
  
    <!--配置connection-factory,指定连接rabbit server参数 -->  
    <rabbit:connection-factory id="connectionFactory"  
        host="${rabbit.connect.host}" port="${rabbit.connect.port}" username="${rabbit.connect.username}"  
        password="${rabbit.connect.password}" channel-cache-size="${rabbit.connect.channelCacheSize}"  
        publisher-returns="true" publisher-confirms="true" />  
  
    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->  
    <rabbit:admin connection-factory="connectionFactory" />  
    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />  
  
    <!--定义queue  说明:durable:是否持久化 exclusive: 仅创建者可以使用的私有队列,断开后自动删除 auto_delete: 当所有消费客户端连接断开后,是否自动删除队列-->  
    <rabbit:queue name="mq.asdf" durable="true" auto-delete="false" exclusive="false" />  
    <rabbit:queue name="mq.asdf2" durable="true" auto-delete="false" exclusive="false" />  
    <rabbit:queue name="mq.qwer" durable="true" auto-delete="false" exclusive="false" />  
    <!--定义topic-exchange -->  
    <rabbit:topic-exchange name="mq.asdfExChange"  
        durable="true" auto-delete="false">  
        <rabbit:bindings>  
            <rabbit:binding queue="mq.asdf" pattern="mq.asdf.send"></rabbit:binding>  
            <rabbit:binding queue="mq.asdf2" pattern="mq.asdf2.send"></rabbit:binding>  
            <rabbit:binding queue="mq.asdf2" pattern="mq.asdf.send"></rabbit:binding>  
        </rabbit:bindings>  
    </rabbit:topic-exchange>   
      
    <!--定义direct-exchange -->  
    <rabbit:direct-exchange name="mq.qwerExChange" durable="true" auto-delete="false">  
       <rabbit:bindings>  
           <rabbit:binding queue="mq.qwer" key="mq.qwer.send" ></rabbit:binding>  
        </rabbit:bindings>  
      
    </rabbit:direct-exchange>  
  
    <!-- 消息接收者 -->  
      
    <bean id="asdfConsumer" class="com.demo.action.AsdfConsumer"></bean>  
    <bean id="asdfConsumer2" class="com.demo.action.AsdfConsumer2"></bean>  
    <bean id="qwerConsumer" class="com.demo.action.QwerConsumer"></bean>  
    <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->  
    <rabbit:listener-container connection-factory="connectionFactory" >  
         <rabbit:listener  queues="mq.asdf"  ref="asdfConsumer"/>  
    </rabbit:listener-container>  
      
    <rabbit:listener-container connection-factory="connectionFactory" >  
         <rabbit:listener  queues="mq.asdf2"  ref="asdfConsumer2"/>  
    </rabbit:listener-container>  
    <rabbit:listener-container connection-factory="connectionFactory" >  
         <rabbit:listener  queues="mq.qwer"  ref="qwerConsumer"/>  
    </rabbit:listener-container>  
  
  
</beans>

生成者demo例子 :

@Controller  
@RequestMapping("/amqpTest")  
public class AmqpTestController {  
      
    @Autowired  
    private AmqpTemplate amqpTemplate;  

    @RequestMapping("/sendMsg")  
    @ResponseBody  
    public String sendAmqbMsg(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){  
        if(model!=null&&!"".equals(msg)){  
            amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf.send", msg);  
        }else{  
            amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf.send", "hello world");  
        }  
        return "success";  
    }  
    @RequestMapping("/sendMsg2")  
    @ResponseBody  
    public String sendAmqbMsg2(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){  
        if(model!=null&&!"".equals(msg)){  
            amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf2.send", "这个世界很奇妙!!!");  
        }else{  
            amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf2.send", "这个世界很奇妙");  
        }  
        return "success";  
    }  
    @RequestMapping("/sendMsg3")  
    @ResponseBody  
    public String sendAmqbMsg3(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){  
        if(model!=null&&!"".equals(msg)){  
            amqpTemplate.convertAndSend("mq.qwerExChange", "mq.qwer.send", "神奇的世界!!!");  
        }else{  
            amqpTemplate.convertAndSend("mq.qwerExChange", "mq.qwer.send", "神奇的世界");  
        }  
        return "success";  
    }  
  
}

消费者:

这里我按照配置列出来一个Demo

<bean id="asdfConsumer" class="com.demo.action.AsdfConsumer">
</bean>

消费者

路径匹配上就没有什么问题.public class AsdfConsumer implements MessageListener{

static{  
        System.out.println("已经依赖成功 ");  
    }  
    public void onMessage(Message message) {  
        MessageProperties  m=message.getMessageProperties();  
//      System.out.println(m);  
        String msg=  new String (message.getBody());  
          
        System.out.println("消费掉了:"+msg+"------->>>>>");  
          
    }  
  
}

相关推荐