Skip to content

Instantly share code, notes, and snippets.

@snicoll
Last active August 29, 2015 13:57
Show Gist options
  • Save snicoll/9506935 to your computer and use it in GitHub Desktop.
Save snicoll/9506935 to your computer and use it in GitHub Desktop.
@Configuration
@Import(BootstrapHornetQConfig.class)
@EnableJms
static class Config {
@Bean
public DemoService echoService() {
return new DemoService();
}
@Bean
public JmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory,
DestinationResolver destinationResolver) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setId("default");
factory.setConnectionFactory(connectionFactory);
factory.setDestinationResolver(destinationResolver);
return factory;
}
@Bean
public Queue testQueue() {
return new HornetQQueue("testQueue");
}
@Bean
public Queue anotherQueue() {
return new HornetQQueue("anotherQueue");
}
@Bean
JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
return new JmsTemplate(connectionFactory);
}
}
public class DemoService {
private final Logger logger = LoggerFactory.getLogger(DemoService.class);
@JmsListener(destination = "testQueue", responseDestination = "anotherQueue")
public String echo(String input) {
logger.info("Sending back: " + input);
return input;
}
@JmsListener(destination = "anotherQueue")
public void log(String input) {
logger.info("received: " + input);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DemoServiceTest {
private final Logger logger = LoggerFactory.getLogger(DemoServiceTest.class);
@Autowired
private JmsTemplate jmsTemplate;
private final Object lock = new Object();
@Test
public void sample() throws JMSException {
sendMessage("testQueue", "Hello World");
// This should hit the first queue and send the response
sleep(5000);
}
private void sendMessage(String destination, final String content) {
logger.info("Sending a simple message to [" + destination + "]");
jmsTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(content);
}
});
}
}
14:29:32,810 [net.nicoll.scratch.spring.jms.DemoServiceTest] - Sending a simple message to [testQueue]
14:29:32,831 [net.nicoll.scratch.spring.jms.DemoService] - Sending back: Hello World
14:29:32,832 [net.nicoll.scratch.spring.jms.DemoService] - received: Hello World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment