Skip to content

Instantly share code, notes, and snippets.

@balamuru
Created April 2, 2014 05:13
Show Gist options
  • Save balamuru/9928308 to your computer and use it in GitHub Desktop.
Save balamuru/9928308 to your computer and use it in GitHub Desktop.
package hello;
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.listener.SimpleMessageListenerContainer;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.util.FileSystemUtils;
import java.io.File;
@Configuration
@EnableAutoConfiguration
public class Application {
static String destinationTopic = "greetings";
@Bean
Receiver receiver() {
return new Receiver();
}
@Bean
ConnectionFactory connectionFactory() {
return new CachingConnectionFactory(
new ActiveMQConnectionFactory("tcp://localhost:61616")
);
}
@Bean
MessageListenerAdapter adapter(Receiver receiver) {
return new MessageListenerAdapter(receiver) {
{
//setDefaultListenerMethod("receiveMessage");
//setDefaultListenerMethod("receiveObject");
//Note: use default listener method: handleMessage()
}
};
}
@Bean
SimpleMessageListenerContainer container(final MessageListenerAdapter messageListener,
final ConnectionFactory connectionFactory) {
return new SimpleMessageListenerContainer() {
{
setMessageListener(messageListener);
setConnectionFactory(connectionFactory);
setDestinationName(destinationTopic);
setPubSubDomain(true);
}
};
}
public static void main(String[] args) {
// Clean out any ActiveMQ data from a previous run
FileSystemUtils.deleteRecursively(new File("activemq-data"));
// Launch the application
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
// Send a message
MessageCreator stringMessageCreator = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("ping");
}
};
MessageCreator objectMessageCreator2 = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(new Person("john", "123 main street"));
}
};
MessageCreator mapMessageCreator3 = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage= session.createMapMessage();
mapMessage.setInt("myInt", 1);
mapMessage.setLong("myLong", 10L);
mapMessage.setObject("myString", "this is a string");
return mapMessage;
}
};
MessageCreator terminateMessageCreatorN = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("close");
}
};
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
System.out.println("Sending a new string message");
jmsTemplate.send(destinationTopic, stringMessageCreator);
System.out.println("Sending a new object message");
jmsTemplate.send(destinationTopic, objectMessageCreator2);
System.out.println("Sending a new map message");
jmsTemplate.send(destinationTopic, mapMessageCreator3);
System.out.println("Sending a new terminate message");
jmsTemplate.send(destinationTopic, terminateMessageCreatorN);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment