Last active
January 16, 2016 21:09
-
-
Save AndrejGajdos/8402d219a9200bf78aef to your computer and use it in GitHub Desktop.
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
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
import javax.jms.Connection; | |
import javax.jms.DeliveryMode; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.Session; | |
import javax.jms.Destination; | |
import javax.jms.TextMessage; | |
public class App { | |
public static void main(final String arg[]) throws Exception { | |
try { | |
javax.naming.Context context = new javax.naming.InitialContext(); | |
// Create a ConnectionFactory | |
ActiveMQConnectionFactory connectionFactory = | |
(org.apache.activemq.ActiveMQConnectionFactory) context.lookup("ConnectionFactory"); | |
// Create a Connection | |
Connection connection = connectionFactory.createConnection(); | |
connection.start(); | |
// Create a session | |
Session session = connection.createSession(false, | |
Session.AUTO_ACKNOWLEDGE); | |
// Create a destination | |
Destination topic = (javax.jms.Topic) context.lookup("MyTopic"); | |
// Create first consumer | |
javax.jms.MessageConsumer consumer1 = session.createConsumer(topic); | |
consumer1.setMessageListener(new MessageListener() { | |
public void onMessage(Message message) { | |
try { | |
if (message instanceof TextMessage) { | |
TextMessage textMessage = (TextMessage) message; | |
System.out.println("Consumer 1 received message " | |
+ textMessage.getText()); | |
} | |
} catch (JMSException e) { | |
System.out.println("Caught:" + e); | |
e.printStackTrace(); | |
} | |
} | |
}); | |
// Create second consumer | |
javax.jms.MessageConsumer consumer2 = session.createConsumer(topic); | |
consumer2.setMessageListener(new MessageListener() { | |
public void onMessage(Message message) { | |
try { | |
if (message instanceof TextMessage) { | |
TextMessage textMessage = (TextMessage) message; | |
System.out.println("Consumer 2 received message " | |
+ textMessage.getText()); | |
} | |
} catch (JMSException e) { | |
System.out.println("Caught:" + e); | |
e.printStackTrace(); | |
} | |
} | |
}); | |
// Create a producer | |
javax.jms.MessageProducer producer = session.createProducer(topic); | |
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); | |
// Send some messages | |
TextMessage message = session.createTextMessage(""); | |
for (int i = 0; i < 10; i++) { | |
try { | |
Date date = new Date(); | |
String formattedDate = new SimpleDateFormat( | |
"MM/dd/yyyy HH:mm:ss").format(date); | |
message = session.createTextMessage(formattedDate | |
+ " number of iteration " + i); | |
producer.send(message); | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
connection.close(); | |
context.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment