我希望不要求我的客户提供content_type application / json,但只是默认它。我得到了这个工作。
我还尝试结合另一个例子来实现ConditionalRejectingErrorHandler的自定义isFatal(Throwable t)。我可以启动我的自定义错误处理程序,但它似乎再次需要content_type属性。我无法弄清楚如何让它们同时工作。
有什么想法吗?
以下成功地不需要content_type 编辑:以下代码无法正常工作。必须已将具有属性content_type application / json的队列中的旧消息拉入
@EnableRabbit
@Configuration
public class ExampleRabbitConfigurer implements
RabbitListenerConfigurer {
@Value("${spring.rabbitmq.host:'localhost'}")
private String host;
@Value("${spring.rabbitmq.port:5672}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Autowired
private MappingJackson2MessageConverter mappingJackson2MessageConverter;
@Autowired
private DefaultMessageHandlerMethodFactory messageHandlerMethodFactory;
@Bean
public MappingJackson2MessageConverter mappingJackson2MessageConverter() {
return new MappingJackson2MessageConverter();
}
@Bean
public DefaultMessageHandlerMethodFactory messageHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
factory.setMessageConverter(mappingJackson2MessageConverter);
return factory;
}
@Override
public void configureRabbitListeners(final RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory);
}
下面的内容适用于覆盖ConditionalRejectingErrorHandler中的isFatal()。 SimpleRabbitListenerContainerFactory.setMessageConverter()似乎应该与DefaultMessageHandlerMethodFactory.setMessageConverter()起到相同的作用。显然事实并非如此。
@EnableRabbit
@Configuration
public class ExampleRabbitConfigurer {
@Value("${spring.rabbitmq.host:'localhost'}")
private String host;
@Value("${spring.rabbitmq.port:5672}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Autowired
ConnectionFactory connectionFactory;
@Autowired
Jackson2JsonMessageConverter jackson2JsonConverter;
@Autowired
ErrorHandler amqpErrorHandlingExceptionStrategy;
@Bean
public Jackson2JsonMessageConverter jackson2JsonConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public ErrorHandler amqpErrorHandlingExceptionStrategy() {
return new ConditionalRejectingErrorHandler(new AmqpErrorHandlingExceptionStrategy());
}
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(jackson2JsonConverter);
factory.setErrorHandler(amqpErrorHandlingExceptionStrategy);
return factory;
}
public static class AmqpErrorHandlingExceptionStrategy extends ConditionalRejectingErrorHandler.DefaultExceptionStrategy {
private final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(getClass());
@Override
public boolean isFatal(Throwable t) {
if (t instanceof ListenerExecutionFailedException) {
ListenerExecutionFailedException lefe = (ListenerExecutionFailedException) t;
LOGGER.error("Failed to process inbound message from queue "
+ lefe.getFailedMessage().getMessageProperties().getConsumerQueue()
+ "; failed message: " + lefe.getFailedMessage(), t);
}
return super.isFatal(t);
}
}