use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class ClusteredTopicExample method main.
public static void main(final String[] args) throws Exception {
Connection connection0 = null;
Connection connection1 = null;
try {
// Step 1. Instantiate topic
Topic topic = ActiveMQJMSClient.createTopic("exampleTopic");
// Step 2. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 4. We create a JMS Connection connection0 which is a connection to server 0
connection0 = cf0.createConnection();
// Step 5. We create a JMS Connection connection1 which is a connection to server 1
connection1 = cf1.createConnection();
// Step 6. We create a JMS Session on server 0
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 7. We create a JMS Session on server 1
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 8. We start the connections to ensure delivery occurs on them
connection0.start();
connection1.start();
// Step 9. We create JMS MessageConsumer objects on server 0 and server 1
MessageConsumer consumer0 = session0.createConsumer(topic);
MessageConsumer consumer1 = session1.createConsumer(topic);
Thread.sleep(1000);
// Step 10. We create a JMS MessageProducer object on server 0
MessageProducer producer = session0.createProducer(topic);
// Step 11. We send some messages to server 0
final int numMessages = 10;
for (int i = 0; i < numMessages; i++) {
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
for (int i = 0; i < numMessages; i++) {
TextMessage message0 = (TextMessage) consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
TextMessage message1 = (TextMessage) consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
}
} finally {
// Step 15. Be sure to close our JMS resources!
if (connection0 != null) {
connection0.close();
}
if (connection1 != null) {
connection1.close();
}
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class QueueMessageRedistributionExample method main.
public static void main(final String[] args) throws Exception {
Connection connection0 = null;
Connection connection1 = null;
try {
// Step 2. Look-up the JMS Queue object from JNDI
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 5. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 6. We create a JMS Connection connection0 which is a connection to server 0
connection0 = cf0.createConnection();
// Step 7. We create a JMS Connection connection1 which is a connection to server 1
connection1 = cf1.createConnection();
// Step 8. We create a JMS Session on server 0, note the session is CLIENT_ACKNOWLEDGE
Session session0 = connection0.createSession(false, Session.CLIENT_ACKNOWLEDGE);
// Step 9. We create a JMS Session on server 1, note the session is CLIENT_ACKNOWLEDGE
Session session1 = connection1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
// Step 10. We start the connections to ensure delivery occurs on them
connection0.start();
connection1.start();
// Step 11. We create JMS MessageConsumer objects on server 0 and server 1
MessageConsumer consumer0 = session0.createConsumer(queue);
MessageConsumer consumer1 = session1.createConsumer(queue);
Thread.sleep(1000);
// Step 12. We create a JMS MessageProducer object on server 0
MessageProducer producer = session0.createProducer(queue);
// Step 13. We send some messages to server 0
final int numMessages = 10;
for (int i = 0; i < numMessages; i++) {
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
// Step 14. We now consume those messages on *both* server 0 and server 1.
// We note the messages have been distributed between servers in a round robin fashion
// JMS Queues implement point-to-point message where each message is only ever consumed by a
// maximum of one consumer
TextMessage message0 = null;
TextMessage message1 = null;
for (int i = 0; i < numMessages; i += 2) {
message0 = (TextMessage) consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
message1 = (TextMessage) consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
}
// Step 15. We acknowledge the messages consumed on node 0. The sessions are CLIENT_ACKNOWLEDGE so
// messages will not get acknowledged until they are explicitly acknowledged.
// Note that we *do not* acknowledge the message consumed on node 1 yet.
message0.acknowledge();
// Step 16. We now close the session and consumer on node 1. (Closing the session automatically closes the
// consumer)
session1.close();
for (int i = 0; i < numMessages; i += 2) {
message0 = (TextMessage) consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
}
// Step 18. We ack the messages.
message0.acknowledge();
} finally {
if (connection0 != null) {
connection0.close();
}
if (connection1 != null) {
connection1.close();
}
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class ApplicationLayerFailoverExample method createJMSObjects.
private static void createJMSObjects(final int server) throws Exception {
// Step 1. Instantiate a JMS Connection Factory object from JNDI on server 1
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:" + (61616 + server));
// Step 2. We create a JMS Connection connection
connection = connectionFactory.createConnection();
// Step 3. We start the connection to ensure delivery occurs
connection.start();
// Step 4. We create a JMS Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 5. Look-up the JMS Queue object from JNDI
Queue queue = session.createQueue("exampleQueue");
// Step 6. We create a JMS MessageConsumer object
consumer = session.createConsumer(queue);
// Step 7. We create a JMS MessageProducer object
producer = session.createProducer(queue);
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class JMSCompletionListenerExample method main.
public static void main(final String[] args) throws Exception {
JMSContext jmsContext = null;
try {
// Step 2. Perfom a lookup on the queue
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 3. Perform a lookup on the Connection Factory
ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616?confirmationWindowSize=10240");
// Step 4.Create a JMS Context
jmsContext = cf.createContext();
// Step 5. Create a message producer.
JMSProducer producer = jmsContext.createProducer();
final CountDownLatch latch = new CountDownLatch(1);
// Step 6. We want to send the message Asynchronously and be notified when the Broker receives it so we set a completion handler
producer.setAsync(new CompletionListener() {
@Override
public void onCompletion(Message message) {
System.out.println("message acknowledged by ActiveMQ");
latch.countDown();
}
@Override
public void onException(Message message, Exception e) {
e.printStackTrace();
}
});
// Step 6. Send the Message
producer.send(queue, "this is a string");
// Step 7. wait for the Completion handler
if (!latch.await(5, TimeUnit.SECONDS)) {
throw new IllegalStateException("Completion listener not called as expected.");
}
} finally {
if (jmsContext != null) {
jmsContext.close();
}
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class JMSContextExample method main.
public static void main(final String[] args) throws Exception {
// Instantiate the queue
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Using closeable interface
try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
JMSContext jmsContext = cf.createContext()) {
// Create a message producer, note that we can chain all this into one statement
jmsContext.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT).send(queue, "this is a string");
// Create a Consumer and receive the payload of the message direct.
String payLoad = jmsContext.createConsumer(queue).receiveBody(String.class);
System.out.println("payLoad = " + payLoad);
}
}
Aggregations