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

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

  
  
  
    Spring公共配置   
  
      
      
  
      
      
      
  
  
  
  
      
      
      
      
      
      
          
              
              
              
          
       
      
      
      
         
             
          
      
      
  
  
  
      
      
      
      
      
      
      
           
      
      
      
           
      
      
           
      
  
  
  

生成者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 消费者

路径匹配上就没有什么问题.

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+"------->>>>>");  
          
          
    }  
  
}  

这种配置的拓展行非常好,个人认为,是比较好的xml配置了
原文:http://blog.csdn.net/xun573017588/article/details/77099535

你可能感兴趣的:(RabbitMQ与spring集成,配置完整的生产者和消费者)