JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(cf);
jmsTemplate.setPubSubDomain(true);
JmsByteArrayMessageSender messageSender = new JmsByteArrayMessageSender(topicName, jmsTemplate);
CollectingByteArrayMessageReceiver collectingReceiver = new CollectingByteArrayMessageReceiver();
JmsByteArrayMessageDispatcher messageDispatcher = new JmsByteArrayMessageDispatcher(collectingReceiver);
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConnectionFactory(cf);
container.setMessageListener(messageDispatcher);
container.setDestinationName(topicName);
container.setPubSubDomain(true);
container.afterPropertiesSet();
container.start();
Random random = new Random();
byte[] randomBytes = new byte[1024];
random.nextBytes(randomBytes);
while(!container.isRunning()) {
Thread.sleep(10l);
}
//TODO: this is a hack. The context doesn't seem to have always set up the consumer completely yet
Thread.sleep(500l);
messageSender.send(randomBytes);
long startTime = System.currentTimeMillis();
while(collectingReceiver.getMessages().isEmpty()) {
Thread.sleep(10l);
if ((System.currentTimeMillis() - startTime) > TIMEOUT) {
fail("Did not receive a message in " + (TIMEOUT / 1000) + " seconds.");
}
}
s_logger.debug ("topicConduit message received {}ms before timeout limit", TIMEOUT - (System.currentTimeMillis () - startTime));
assertEquals(1, collectingReceiver.getMessages().size());
byte[] receivedBytes = collectingReceiver.getMessages().get(0);
assertEquals(randomBytes.length, receivedBytes.length);
for(int i = 0; i < randomBytes.length; i++) {
assertEquals(randomBytes[i], receivedBytes[i]);
}