Code, JavaEE

Send JMS Message Using ActiveMQ


@Configuration
public class JMSConfig {
 @Bean
 public ActiveMQConnectionFactory activeMQConnectionFactory() {
 ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
 activeMQConnectionFactory.setBrokerURL("failover:(tcp://127.0.0.1:61616)");
 activeMQConnectionFactory.setUserName("admin");
 activeMQConnectionFactory.setPassword("admin");
 return activeMQConnectionFactory;
 }

 

}

 

private void sendJmsMessageToQueue(String message, String queueName) {
 try {
 Connection connection = connectionFactory.createConnection();
 connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination destination = session.createQueue(queueName);

MessageProducer producer = session.createProducer(destination);
 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

TextMessage textMessage = session.createTextMessage(message);

producer.send(textMessage);

session.close();
 connection.close();

System.out.println("JMS Message Sent");

} catch (JMSException e) {
 e.printStackTrace();
 }
 }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s