1

Update: I switched to another queue and I can see my messages, as expected. So it means there is something different about the initial queue I tried. It does not have any consumers. What could cause the queue to not show any messages in the web console?

I am trying to publish my Email object to my ActiveMQ queue. My main code successfully runs without errors, and I get the "successfully connected to broker" output. However, I check the queue on my ActiveMQ website instance and there are never any messages. There are no consumers for the queue.

Email does not implement Serializable, but that should not be a problem since I supply a Jackson JSON MessageConverter (and even if there was a serializing problem, I think there would be an error). I even tried publishing a simple "test msg" String, but still don't see any messages.

Why can't I see messages in the queue on the website after I publish?

@Configuration
@EnableJms
public class EmailMessagingConfiguration {
    @Value(ConfigConstants.ACTIVEMQ_EMAIL_BROKER_URL_KEY)
    private String brokerUrl;

    @Value(ConfigConstants.ACTIVEMQ_EMAIL_MAIN_MESSAGE_QUEUE_KEY)
    private String queueName;

    @Value(ConfigConstants.ACTIVEMQ_EMAIL_USERNAME_KEY)
    private String userName;

    @Value(ConfigConstants.ACTIVEMQ_EMAIL_PASSWORD_KEY)
    private String password;


    @Bean
    public ConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(brokerUrl);
        connectionFactory.setUserName(userName);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @Bean // Serialize message content to json using TextMessage
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        return converter;
    }

    @Bean
    public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(connectionFactory);
        jmsTemplate.setMessageConverter(messageConverter);
        jmsTemplate.setDefaultDestinationName(queueName);
        return jmsTemplate;
    }

}

Publisher

@Component
public class EmailPublisher {

    @Autowired
    JmsTemplate jmsTemplate;

    public void publish(final Email email) {
        jmsTemplate.convertAndSend(email);
        jmsTemplate.convertAndSend("test msg");
    }
}

Main method (test)

@SpringBootTest
@Import({EmailMessagingConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfiguration.class)
public class EmailPublisherTest {


    @Autowired
    private EmailPublisher emailPublisher;

    @Test
    public void testPublishEmail() {
        Email email = new Email();
        email.setSubject("foo");
        email.setSender(new User("a@gmail.com"));

        emailPublisher.publish(email);
    }
}
onepiece
  • 3,279
  • 8
  • 44
  • 63
  • You are right of not needing Serializable as long as you have MessageConverter there. I tried exactly with your configurations and it works on my end. Could you please provide an example of your ConfigConstants. There is no consumer because there is no JmsListener listening to your queue, at least I don't see from these configurations provided – Chris May 18 '18 at 06:40
  • @Chris I said there is no consumer, so I should be able to see any messages sent to the queue on the web console. I don't think there is anything wrong with ConfigConstants. Terminally says I successfully connected to the broker, and there is no more output after. – onepiece May 18 '18 at 14:46
  • My bad for misunderstanding your consumer part. Anyway, have you tried running it without using junit test? I copied your entire code and change the queue configurations accordingly and it does manage to send (checked at queue console:8161), so I don't see any issue with the above code. – Chris May 21 '18 at 01:56

0 Answers0