Spring 以JavaConfig的方式整合RabbitMQ

大体流程:

    1.ConnectionFactory

    2.配置RabbitTemplate

    3.RabbitAdmin

    4.声明Queue and Bind Queue with Exchange and RoutingKey

    5.配置MessageListenerContainer

具体详情:

     1:@Bean
public ConnectionFactory connectionFactory() throws Exception
{
RabbitConnectionFactoryBean rabbitConnectionFactoryBean = new RabbitConnectionFactoryBean();
rabbitConnectionFactoryBean.setHost("localhost");
rabbitConnectionFactoryBean.afterPropertiesSet();
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(
rabbitConnectionFactoryBean.getObject());
return cachingConnectionFactory;

}

 

    2:

        @Bean

public RabbitTemplate rabbitTemplate()
{

RabbitTmeplate rabbitTmeplate=new RabbitTemplate();

                rabbitTemplate.setConenctionFacotry(connectionFactroy());

                //rabbitTemplate.setMessageConverter(...)

                return rabbitTemplate;

}

   3:

     @Bean
public RabbitAdmin rabbitAdmin() throws Exception
{
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
rabbitAdmin.declareExchange(directExchange());
return rabbitAdmin;

}

4:

     @Bean
public Queue test1Queue()
{
return new Queue("test1");
}
@Bean
public Binding test1Binding()
{
return BindingBuilder.bind(test1Queue()).to(directExchange()).with("test1");

}

5:

    @Bean

    public IConsumer consumer1(){

        return new IConsumer(){//TODO};

    }

    @Bean

    public IConsumer consumer2(){

        return new IConsumer(){//TODO};

    }

    @Bean

    public SimpleMessageListenerContainer container(){

    SimpleMessageListenerContainer contaienr =new SimpleMessageListenerContainer ();

    container.setMessageConverter(...)//这里如果设置了消息转换器的话,其他的rabbitMQ设置的转换器要相同

     MessageListenerAdapter adapter=new MessageListener(new FacadedConsumer(),"process");

    adapter.setMessageConverter(...)//同上

    container.setMessageListener(adapter);

    return container;

}

Class AppEvent Extends EventObject{

    private String eventType;

    private Serializable data;

    AppEvent(Stirng type,Serializable data){super(data);//赋值}

}

Classs FacadedConsumer impl IConsuerme{

    private Map<String,Collection<IConsumer>> map=null;

    FacadedConsumer(List<IConsumer>consumers){

        Map<String,Collection<IConsumer>>m=new hashmap<>();

       if(CollectionUtils.isEmpty(consumers)){

            map=Collections.emptyMap();return;

         }

        for(IConsumer con:consumers){

            Collection<IConsumer>cc=m.get(con.queueName());

            if(null==cc){cc=new HashSet<>();m.put(con.queueName(),cc);}

            cc.add(con);

         }

    }

     @Override

    public String queueName(){

    return "";//这个依你具体的需求而定       

    }

    @Override

    public void process(AppEvent appevent){

        //利用for循环进行遍历取appevent对应的IConsumer集合就行了,然后调用Process即可

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值