My Configuration Class is below:-
@Configuration @EnableRabbit public class NeuwMessagingConfig implements RabbitListenerConfigurer { private Logger logger = Logger.getLogger(this.getClass()); @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost"); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); connectionFactory.setPort(5672); return connectionFactory; } @Bean public AmqpAdmin amqpAdmin() { RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory()); return rabbitAdmin; } @Bean public Queue myQueue() { return new Queue("event", true); } @Bean public DirectExchange directExchange(){ DirectExchange directExchange = new DirectExchange("neuw.direct.exchange",true,false); return directExchange; } @Bean public Binding binding() { Binding binding = BindingBuilder .bind(new Queue("event")) .to(directExchange()).with("type"); return binding; } @Bean() public RabbitListenerContainerFactory listenerFactory(){ SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConnectionFactory(connectionFactory()); factory.setMessageConverter(jsonMessageConverter()); return factory; } @Bean public MappingJackson2MessageConverter consumerJackson2MessageConverter() { return new MappingJackson2MessageConverter(); } @Bean public MessageConverter jsonMessageConverter(){ Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter(); /*DefaultClassMapper classMapper = new DefaultClassMapper(); messageConverter.setClassMapper(classMapper);*/ return messageConverter; } @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); rabbitTemplate.setMessageConverter(jsonMessageConverter()); return rabbitTemplate; } @Bean public MappingJackson2MessageConverter jackson2Converter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setObjectMapper(new ObjectMapper()); return converter; } @Bean public DefaultMessageHandlerMethodFactory messageHandlerMethodFactory() { DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory(); factory.setMessageConverter(jackson2Converter()); return factory; } @Override public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) { registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory()); } }
The DTO is :-
public class TestDto { private String message; private int code; @JsonProperty(value = "co-relation-id") private String coRelationID; public TestDto(String message, int code) { this.message = message; this.code = code; UUID uuid = UUID.randomUUID(); this.coRelationID = uuid.toString(); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getCoRelationID() { return coRelationID; } public void setCoRelationID(String coRelationID) { this.coRelationID = coRelationID; } }
The Snippet for publishing the message is:-
TestDto testDto = new TestDto("test",200); rabbitTemplate.convertAndSend("neuw.direct.exchange","type", testDto);
And the snippet to listen the message is:-
private Logger logger = Logger.getLogger(this.getClass()); @RabbitListener(queues = "event", containerFactory = "listenerFactory") public void Test(Message<TestDto> testDto){ logger.info("TEST info "+testDto.getPayload().getCoRelationID()); logger.error("TEST error "+testDto.getPayload().getCoRelationID()); logger.warn("TEST warn "+testDto.getPayload().getCoRelationID()); logger.debug("TEST debug "+testDto.getPayload().getCoRelationID()); logger.trace("TEST trace "+testDto.getPayload().getCoRelationID()); }
The issue is the logs here are not coming up
but if I try with The snippet below then it works:-
public void Test(Object testDto){ logger.info("TEST info"); logger.error("TEST error"); logger.warn("TEST warn"); logger.debug("TEST debug"); logger.trace("TEST trace"); }
it does goes to the logs but how to get the TestDto in that case? If I am doing something wrong in the configuration or not using the listner correctly? Need some resolution on this issue
Advertisement
Answer
Problem solved with issue with the constructor of the POJO class – no default constructor, now the resolution was either to have a default one or have a constructor with @JsonCreator
Example:-
@JsonCreator public TestDto(@JsonProperty("message") String message, @JsonProperty("code") int code, @JsonProperty(value = "co-relation-id") String coRelationID) { this.message = message; this.code = code; this.coRelationID = coRelationID; }