Last active
May 24, 2018 20:52
-
-
Save fmbenhassine/16a21a997a7d83dc6432 to your computer and use it in GitHub Desktop.
JMS hello world #lab
This file contains hidden or 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 io.github.benas.labs.javaee.jms.listeners; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.TextMessage; | |
public class SimpleMsgListener implements MessageListener { | |
String name; | |
public SimpleMsgListener(String name) { | |
this.name = name; | |
} | |
public void onMessage(Message msg) { | |
try { | |
if (msg instanceof TextMessage) | |
System.out.println(name + " received : " + ((TextMessage) msg).getText()); | |
} catch (JMSException exc) { | |
System.err.println("Exception in listener: " + exc); | |
} | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueReceiver { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleQueueReceiver.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueReceiver queueReceiver = queueSession.createReceiver(queue); | |
//For asynchronous receiving (PUSH), register a listener and start the queueConnection | |
queueReceiver.setMessageListener(new SimpleMsgListener("simple listener")); | |
queueConnection.start(); | |
System.out.println("queue receiver is listening to incoming messages..."); | |
//For synchronous receiving (PULL), DO NOT register a listener and use the following snippet : | |
/*queueConnection.start(); | |
while (true) { | |
Message m = queueReceiver.receive(5000); | |
if (m != null) { | |
if (m instanceof TextMessage) { | |
TextMessage message = (TextMessage) m; | |
System.out.println("Reading message: " + message.getText()); | |
} else { | |
break; | |
} | |
} | |
}*/ | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueSender { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleQueueSender.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueSender queueSender = queueSession.createSender(queue); | |
TextMessage message = queueSession.createTextMessage(); | |
message.setText("This is a new message from queue sender!"); | |
queueSender.send(message); | |
queueConnection.close(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.io.FileInputStream; | |
import java.util.Properties; | |
public class SimpleTopicPublisher { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleTopicPublisher.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicPublisher topicPublisher = topicSession.createPublisher(topic); | |
TextMessage message = topicSession.createTextMessage(); | |
message.setText("Hi there!"); | |
System.out.println("Publishing message: " + message.getText()); | |
topicPublisher.publish(message); | |
topicConnection.close(); | |
} | |
} | |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.util.Properties; | |
public class SimpleTopicSubscriber { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleTopicSubscriber.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicSubscriber topicSubscriber1 = topicSession.createSubscriber(topic); | |
TopicSubscriber topicSubscriber2 = topicSession.createSubscriber(topic); | |
topicSubscriber1.setMessageListener(new SimpleMsgListener("Subscriber 1 for topic t")); | |
topicSubscriber2.setMessageListener(new SimpleMsgListener("Subscriber 2 for topic t")); | |
topicConnection.start(); | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} | |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.listeners; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.TextMessage; | |
public class SimpleMsgListener implements MessageListener { | |
String name; | |
public SimpleMsgListener(String name) { | |
this.name = name; | |
} | |
public void onMessage(Message msg) { | |
try { | |
if (msg instanceof TextMessage) | |
System.out.println(name + " received : " + ((TextMessage) msg).getText()); | |
} catch (JMSException exc) { | |
System.err.println("Exception in listener: " + exc); | |
} | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueReceiver { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleQueueReceiver.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueReceiver queueReceiver = queueSession.createReceiver(queue); | |
//For asynchronous receiving (PUSH), register a listener and start the queueConnection | |
queueReceiver.setMessageListener(new SimpleMsgListener("simple listener")); | |
queueConnection.start(); | |
System.out.println("queue receiver is listening to incoming messages..."); | |
//For synchronous receiving (PULL), DO NOT register a listener and use the following snippet : | |
/*queueConnection.start(); | |
while (true) { | |
Message m = queueReceiver.receive(5000); | |
if (m != null) { | |
if (m instanceof TextMessage) { | |
TextMessage message = (TextMessage) m; | |
System.out.println("Reading message: " + message.getText()); | |
} else { | |
break; | |
} | |
} | |
}*/ | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueSender { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleQueueSender.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueSender queueSender = queueSession.createSender(queue); | |
TextMessage message = queueSession.createTextMessage(); | |
message.setText("This is a new message from queue sender!"); | |
queueSender.send(message); | |
queueConnection.close(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.io.FileInputStream; | |
import java.util.Properties; | |
public class SimpleTopicPublisher { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleTopicPublisher.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicPublisher topicPublisher = topicSession.createPublisher(topic); | |
TextMessage message = topicSession.createTextMessage(); | |
message.setText("Hi there!"); | |
System.out.println("Publishing message: " + message.getText()); | |
topicPublisher.publish(message); | |
topicConnection.close(); | |
} | |
} | |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.util.Properties; | |
public class SimpleTopicSubscriber { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleTopicSubscriber.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicSubscriber topicSubscriber1 = topicSession.createSubscriber(topic); | |
TopicSubscriber topicSubscriber2 = topicSession.createSubscriber(topic); | |
topicSubscriber1.setMessageListener(new SimpleMsgListener("Subscriber 1 for topic t")); | |
topicSubscriber2.setMessageListener(new SimpleMsgListener("Subscriber 2 for topic t")); | |
topicConnection.start(); | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} | |
This file contains hidden or 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
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory | |
# use the following property to configure the default connector | |
java.naming.provider.url = tcp://localhost:61616 | |
# register some queues in JNDI using the form | |
# queue.[jndiName] = [physicalName] | |
queue.q = q | |
# register some topics in JNDI using the form | |
# topic.[jndiName] = [physicalName] | |
topic.t = t |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.listeners; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.TextMessage; | |
public class SimpleMsgListener implements MessageListener { | |
String name; | |
public SimpleMsgListener(String name) { | |
this.name = name; | |
} | |
public void onMessage(Message msg) { | |
try { | |
if (msg instanceof TextMessage) | |
System.out.println(name + " received : " + ((TextMessage) msg).getText()); | |
} catch (JMSException exc) { | |
System.err.println("Exception in listener: " + exc); | |
} | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.listeners; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.TextMessage; | |
public class SimpleMsgListener implements MessageListener { | |
String name; | |
public SimpleMsgListener(String name) { | |
this.name = name; | |
} | |
public void onMessage(Message msg) { | |
try { | |
if (msg instanceof TextMessage) | |
System.out.println(name + " received : " + ((TextMessage) msg).getText()); | |
} catch (JMSException exc) { | |
System.err.println("Exception in listener: " + exc); | |
} | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueReceiver { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleQueueReceiver.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueReceiver queueReceiver = queueSession.createReceiver(queue); | |
//For asynchronous receiving (PUSH), register a listener and start the queueConnection | |
queueReceiver.setMessageListener(new SimpleMsgListener("simple listener")); | |
queueConnection.start(); | |
System.out.println("queue receiver is listening to incoming messages..."); | |
//For synchronous receiving (PULL), DO NOT register a listener and use the following snippet : | |
/*queueConnection.start(); | |
while (true) { | |
Message m = queueReceiver.receive(5000); | |
if (m != null) { | |
if (m instanceof TextMessage) { | |
TextMessage message = (TextMessage) m; | |
System.out.println("Reading message: " + message.getText()); | |
} else { | |
break; | |
} | |
} | |
}*/ | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueSender { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleQueueSender.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueSender queueSender = queueSession.createSender(queue); | |
TextMessage message = queueSession.createTextMessage(); | |
message.setText("This is a new message from queue sender!"); | |
queueSender.send(message); | |
queueConnection.close(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.io.FileInputStream; | |
import java.util.Properties; | |
public class SimpleTopicPublisher { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleTopicPublisher.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicPublisher topicPublisher = topicSession.createPublisher(topic); | |
TextMessage message = topicSession.createTextMessage(); | |
message.setText("Hi there!"); | |
System.out.println("Publishing message: " + message.getText()); | |
topicPublisher.publish(message); | |
topicConnection.close(); | |
} | |
} | |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.util.Properties; | |
public class SimpleTopicSubscriber { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleTopicSubscriber.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicSubscriber topicSubscriber1 = topicSession.createSubscriber(topic); | |
TopicSubscriber topicSubscriber2 = topicSession.createSubscriber(topic); | |
topicSubscriber1.setMessageListener(new SimpleMsgListener("Subscriber 1 for topic t")); | |
topicSubscriber2.setMessageListener(new SimpleMsgListener("Subscriber 2 for topic t")); | |
topicConnection.start(); | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} | |
This file contains hidden or 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
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory | |
# use the following property to configure the default connector | |
java.naming.provider.url = tcp://localhost:61616 | |
# register some queues in JNDI using the form | |
# queue.[jndiName] = [physicalName] | |
queue.q = q | |
# register some topics in JNDI using the form | |
# topic.[jndiName] = [physicalName] | |
topic.t = t |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueReceiver { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleQueueReceiver.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueReceiver queueReceiver = queueSession.createReceiver(queue); | |
//For asynchronous receiving (PUSH), register a listener and start the queueConnection | |
queueReceiver.setMessageListener(new SimpleMsgListener("simple listener")); | |
queueConnection.start(); | |
System.out.println("queue receiver is listening to incoming messages..."); | |
//For synchronous receiving (PULL), DO NOT register a listener and use the following snippet : | |
/*queueConnection.start(); | |
while (true) { | |
Message m = queueReceiver.receive(5000); | |
if (m != null) { | |
if (m instanceof TextMessage) { | |
TextMessage message = (TextMessage) m; | |
System.out.println("Reading message: " + message.getText()); | |
} else { | |
break; | |
} | |
} | |
}*/ | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueSender { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleQueueSender.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueSender queueSender = queueSession.createSender(queue); | |
TextMessage message = queueSession.createTextMessage(); | |
message.setText("This is a new message from queue sender!"); | |
queueSender.send(message); | |
queueConnection.close(); | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>io.github.benas.labs</groupId> | |
<artifactId>java-labs</artifactId> | |
<version>1.0</version> | |
</parent> | |
<artifactId>jms</artifactId> | |
<version>1.0</version> | |
<name>Java EE JMS lab</name> | |
<packaging>jar</packaging> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.activemq</groupId> | |
<artifactId>activemq-core</artifactId> | |
<version>5.7.0</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.io.FileInputStream; | |
import java.util.Properties; | |
public class SimpleTopicPublisher { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleTopicPublisher.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicPublisher topicPublisher = topicSession.createPublisher(topic); | |
TextMessage message = topicSession.createTextMessage(); | |
message.setText("Hi there!"); | |
System.out.println("Publishing message: " + message.getText()); | |
topicPublisher.publish(message); | |
topicConnection.close(); | |
} | |
} | |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.util.Properties; | |
public class SimpleTopicSubscriber { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleTopicSubscriber.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicSubscriber topicSubscriber1 = topicSession.createSubscriber(topic); | |
TopicSubscriber topicSubscriber2 = topicSession.createSubscriber(topic); | |
topicSubscriber1.setMessageListener(new SimpleMsgListener("Subscriber 1 for topic t")); | |
topicSubscriber2.setMessageListener(new SimpleMsgListener("Subscriber 2 for topic t")); | |
topicConnection.start(); | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} | |
This file contains hidden or 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
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory | |
# use the following property to configure the default connector | |
java.naming.provider.url = tcp://localhost:61616 | |
# register some queues in JNDI using the form | |
# queue.[jndiName] = [physicalName] | |
queue.q = q | |
# register some topics in JNDI using the form | |
# topic.[jndiName] = [physicalName] | |
topic.t = t |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.listeners; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.TextMessage; | |
public class SimpleMsgListener implements MessageListener { | |
String name; | |
public SimpleMsgListener(String name) { | |
this.name = name; | |
} | |
public void onMessage(Message msg) { | |
try { | |
if (msg instanceof TextMessage) | |
System.out.println(name + " received : " + ((TextMessage) msg).getText()); | |
} catch (JMSException exc) { | |
System.err.println("Exception in listener: " + exc); | |
} | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueReceiver { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleQueueReceiver.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueReceiver queueReceiver = queueSession.createReceiver(queue); | |
//For asynchronous receiving (PUSH), register a listener and start the queueConnection | |
queueReceiver.setMessageListener(new SimpleMsgListener("simple listener")); | |
queueConnection.start(); | |
System.out.println("queue receiver is listening to incoming messages..."); | |
//For synchronous receiving (PULL), DO NOT register a listener and use the following snippet : | |
/*queueConnection.start(); | |
while (true) { | |
Message m = queueReceiver.receive(5000); | |
if (m != null) { | |
if (m instanceof TextMessage) { | |
TextMessage message = (TextMessage) m; | |
System.out.println("Reading message: " + message.getText()); | |
} else { | |
break; | |
} | |
} | |
}*/ | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueSender { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleQueueSender.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueSender queueSender = queueSession.createSender(queue); | |
TextMessage message = queueSession.createTextMessage(); | |
message.setText("This is a new message from queue sender!"); | |
queueSender.send(message); | |
queueConnection.close(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.io.FileInputStream; | |
import java.util.Properties; | |
public class SimpleTopicPublisher { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleTopicPublisher.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicPublisher topicPublisher = topicSession.createPublisher(topic); | |
TextMessage message = topicSession.createTextMessage(); | |
message.setText("Hi there!"); | |
System.out.println("Publishing message: " + message.getText()); | |
topicPublisher.publish(message); | |
topicConnection.close(); | |
} | |
} | |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.util.Properties; | |
public class SimpleTopicSubscriber { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleTopicSubscriber.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicSubscriber topicSubscriber1 = topicSession.createSubscriber(topic); | |
TopicSubscriber topicSubscriber2 = topicSession.createSubscriber(topic); | |
topicSubscriber1.setMessageListener(new SimpleMsgListener("Subscriber 1 for topic t")); | |
topicSubscriber2.setMessageListener(new SimpleMsgListener("Subscriber 2 for topic t")); | |
topicConnection.start(); | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} | |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.listeners; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.TextMessage; | |
public class SimpleMsgListener implements MessageListener { | |
String name; | |
public SimpleMsgListener(String name) { | |
this.name = name; | |
} | |
public void onMessage(Message msg) { | |
try { | |
if (msg instanceof TextMessage) | |
System.out.println(name + " received : " + ((TextMessage) msg).getText()); | |
} catch (JMSException exc) { | |
System.err.println("Exception in listener: " + exc); | |
} | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueReceiver { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleQueueReceiver.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueReceiver queueReceiver = queueSession.createReceiver(queue); | |
//For asynchronous receiving (PUSH), register a listener and start the queueConnection | |
queueReceiver.setMessageListener(new SimpleMsgListener("simple listener")); | |
queueConnection.start(); | |
System.out.println("queue receiver is listening to incoming messages..."); | |
//For synchronous receiving (PULL), DO NOT register a listener and use the following snippet : | |
/*queueConnection.start(); | |
while (true) { | |
Message m = queueReceiver.receive(5000); | |
if (m != null) { | |
if (m instanceof TextMessage) { | |
TextMessage message = (TextMessage) m; | |
System.out.println("Reading message: " + message.getText()); | |
} else { | |
break; | |
} | |
} | |
}*/ | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.p2p; | |
import javax.jms.*; | |
import javax.naming.*; | |
import java.util.Properties; | |
public class SimpleQueueSender { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleQueueSender.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); | |
Queue queue = (Queue) jndiContext.lookup("q"); | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | |
QueueSender queueSender = queueSession.createSender(queue); | |
TextMessage message = queueSession.createTextMessage(); | |
message.setText("This is a new message from queue sender!"); | |
queueSender.send(message); | |
queueConnection.close(); | |
} | |
} |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.io.FileInputStream; | |
import java.util.Properties; | |
public class SimpleTopicPublisher { | |
public static void main(String[] args) throws Exception{ | |
Properties p = new Properties(); | |
p.load(SimpleTopicPublisher.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicPublisher topicPublisher = topicSession.createPublisher(topic); | |
TextMessage message = topicSession.createTextMessage(); | |
message.setText("Hi there!"); | |
System.out.println("Publishing message: " + message.getText()); | |
topicPublisher.publish(message); | |
topicConnection.close(); | |
} | |
} | |
This file contains hidden or 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 io.github.benas.labs.javaee.jms.pubsub; | |
import org.apache.activemq.broker.BrokerService; | |
import io.github.benas.labs.javaee.jms.listeners.SimpleMsgListener; | |
import javax.jms.*; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import java.util.Properties; | |
public class SimpleTopicSubscriber { | |
public static void main(String[] args) throws Exception{ | |
startBroker(); | |
Properties p = new Properties(); | |
p.load(SimpleTopicSubscriber.class.getResourceAsStream(("/jndi.properties"))); | |
Context jndiContext = new InitialContext(p); | |
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); | |
Topic topic = (Topic) jndiContext.lookup("t"); | |
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); | |
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); | |
TopicSubscriber topicSubscriber1 = topicSession.createSubscriber(topic); | |
TopicSubscriber topicSubscriber2 = topicSession.createSubscriber(topic); | |
topicSubscriber1.setMessageListener(new SimpleMsgListener("Subscriber 1 for topic t")); | |
topicSubscriber2.setMessageListener(new SimpleMsgListener("Subscriber 2 for topic t")); | |
topicConnection.start(); | |
} | |
private static void startBroker() throws Exception { | |
BrokerService broker = new BrokerService(); | |
broker.addConnector("tcp://localhost:61616"); | |
broker.start(); | |
} | |
} | |
This file contains hidden or 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
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory | |
# use the following property to configure the default connector | |
java.naming.provider.url = tcp://localhost:61616 | |
# register some queues in JNDI using the form | |
# queue.[jndiName] = [physicalName] | |
queue.q = q | |
# register some topics in JNDI using the form | |
# topic.[jndiName] = [physicalName] | |
topic.t = t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment