Skip to content

Instantly share code, notes, and snippets.

@emag
Last active January 1, 2016 04:58
Show Gist options
  • Select an option

  • Save emag/8094892 to your computer and use it in GitHub Desktop.

Select an option

Save emag/8094892 to your computer and use it in GitHub Desktop.
package hello;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
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.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 mailboxDestination = "mailbox-destination";
@Bean
Receiver receiver() {
return new Receiver();
}
@Bean
MessageListenerAdapter adapter(Receiver receiver) {
return new MessageListenerAdapter(receiver) {
{
setDefaultListenerMethod("receiveMessage");
}
};
}
@Bean
SimpleMessageListenerContainer container(final MessageListenerAdapter messageListener,
final ConnectionFactory connectionFactory) {
return new SimpleMessageListenerContainer() {
{
setMessageListener(messageListener);
setConnectionFactory(connectionFactory);
setDestinationName(mailboxDestination);
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 messageCreator = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("ping!");
}
};
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
System.out.println("Sending a new message.");
jmsTemplate.send(mailboxDestination, messageCreator);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment