如何在同一个项目中以不同方式配置两个rabbitlisteners?

问题描述 投票:1回答:1

我有两个队列,每个队列都有消息。队列一有鸟对象,队列二有鸟点对象。我正在使用defaultclassmapper将消息转换回对象。有没有办法让我在我的兔子身上添加不同的配置。

我的听众。

 @Qualifier("bird")
@RabbitListener(queues = "vogels")
public void receiveBird(Bird in)  
    BirdSpotting birdSpotting = new BirdSpotting();
    birdSpotting.setBird(in);
    rabbitTemplate.convertAndSend("vogelspottings",birdSpotting);
}

@Qualifier("birdspotting")
@RabbitListener(queues = "vogelspottingmetlocatie")
public void receiveBirdWithLocation(BirdSpotting birdSpotting){
    service.saveBirdSpotting(birdSpotting);

}

我的配置类。

@Configuration
@EnableRabbit
public class RabbitConf2 implements RabbitListenerConfigurer {
    @Autowired
    DefaultClassMapper mapper;


    @Bean
    public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
        return new MappingJackson2MessageConverter();
    }

    @Bean
    public DefaultMessageHandlerMethodFactory messageHandlerMethodFactory() {
        DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
        factory.setMessageConverter(consumerJackson2MessageConverter());
        return factory;
    }


    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
        registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplateService2(final ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(producerJackson2MessageConverterService2());
        return rabbitTemplate;
    }

    @Bean
    public Jackson2JsonMessageConverter producerJackson2MessageConverterService2() {
        final Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
        jackson2JsonMessageConverter.setClassMapper(mapper);

        return jackson2JsonMessageConverter;
    }

我的两个队列的两个defaultclassmappers:

@Bean(value = "bird")
    public DefaultClassMapper classMapperService2() {
        DefaultClassMapper classMapper = new DefaultClassMapper();
        Map<String, Class<?>> idClassMapping = new HashMap<>();
        idClassMapping.put("be.kdg.birdgeneratorservice.Bird", Bird.class);
        classMapper.setIdClassMapping(idClassMapping);
        return classMapper;
    }

    @Bean(value = "birdspotting")
    public DefaultClassMapper classMapperService3() {
        DefaultClassMapper classMapper = new DefaultClassMapper();
        Map<String, Class<?>> idClassMapping = new HashMap<>();
        idClassMapping.put("be.kdg.locationservice.BirdSpotting", BirdSpotting.class);
        classMapper.setIdClassMapping(idClassMapping);
        return classMapper;
    }
java spring spring-boot rabbitmq spring-amqp
1个回答
1
投票

您需要引入一个具有适当配置的RabbitListenerContainerFactory bean,并使用第二个@RabbitListener中的名称:

/**
 * The bean name of the {@link org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory}
 * to use to create the message listener container responsible to serve this endpoint.
 * <p>If not specified, the default container factory is used, if any.
 * @return the {@link org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory}
 * bean name.
 */
String containerFactory() default "";

通过这种方式,您将区分Spring Boot提供的默认值,并为另一个用例提供自己的自定义。

请参阅文档中的更多信息:https://docs.spring.io/spring-amqp/docs/2.1.4.RELEASE/reference/#async-annotation-driven

© www.soinside.com 2019 - 2024. All rights reserved.