我有两个队列,每个队列上都有消息。队列1有鸟类对象,队列2有鸟类监视对象。我正在使用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;
}
我两个队列的两个默认类映射器:
@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;
}
答案 0 :(得分: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