Created
November 18, 2012 18:08
-
-
Save bhdrkn/4106589 to your computer and use it in GitHub Desktop.
HelloWorldProducer ActiveMq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bahadirakin.jms; | |
import javax.jms.Connection; | |
import javax.jms.DeliveryMode; | |
import javax.jms.Destination; | |
import javax.jms.MessageProducer; | |
import javax.jms.Session; | |
import javax.jms.TextMessage; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
/** | |
* @author bahadir.akin | |
* | |
*/ | |
public class HelloWorldProducer implements Runnable { | |
public void run() { | |
try { | |
// Create a ConnectionFactory | |
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( | |
"tcp://localhost:8182"); | |
// Create a Connection | |
Connection connection = connectionFactory.createConnection(); | |
connection.start(); | |
// Create a Session | |
Session session = connection.createSession(false, | |
Session.AUTO_ACKNOWLEDGE); | |
// Create the destination (Topic or Queue) | |
Destination destination = session.createQueue("TEST.FOO"); | |
// Create a MessageProducer from the Session to the Topic or Queue | |
MessageProducer producer = session.createProducer(destination); | |
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); | |
// Create a messages | |
String text = "Hello world! From: " | |
+ Thread.currentThread().getName() + " : " | |
+ this.hashCode(); | |
TextMessage message = session.createTextMessage(text); | |
// Tell the producer to send the message | |
System.out.println("Sent message: " + text); | |
producer.send(message); | |
// Clean up | |
session.close(); | |
connection.close(); | |
} catch (Exception e) { | |
System.out.println("Caught: " + e); | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
new HelloWorldProducer().run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment