use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class JMSServerStartStopTest method createConnectionFactory.
/**
* @return
*/
private ActiveMQConnectionFactory createConnectionFactory() {
ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(NETTY_CONNECTOR_FACTORY));
connectionFactories.add(cf);
return cf;
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class HAPolicyAutoBackupExample method main.
public static void main(final String[] args) throws Exception {
Connection connection0 = null;
Connection connection1 = null;
try {
server0 = ServerUtil.startServer(args[0], HAPolicyAutoBackupExample.class.getSimpleName() + "0", 0, 5000);
server1 = ServerUtil.startServer(args[1], HAPolicyAutoBackupExample.class.getSimpleName() + "1", 1, 5000);
// Step 2. Look-up the JMS Queue object from JNDI
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 3. new connection factories towards server 0 and 1
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1");
ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1");
// Step 6. We create JMS Connections to server 0 and 1
connection0 = cf0.createConnection();
connection1 = cf1.createConnection();
// step 7. wait for the backups to start replication
waitForBackups(cf0, 2);
// Step 8. We create JMS Sessions on server 0 and 1
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 9. We start the connections to ensure delivery occurs on them
connection0.start();
connection1.start();
// Step 10. We create JMS MessageConsumer objects on server 0 and server 1
MessageConsumer consumer0 = session0.createConsumer(queue);
MessageConsumer consumer1 = session1.createConsumer(queue);
// Step 11. We create a JMS MessageProducer object on server 0
MessageProducer producer = session0.createProducer(queue);
// Step 12. 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());
}
// note that the other half of the messages will have been sent to server1 for consumer1
for (int i = 0; i < numMessages / 2; i++) {
TextMessage message0 = (TextMessage) consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
}
// Step 14.close the consumer so it doesn't get any messages
consumer1.close();
// Step 15.now kill server1, messages will be scaled down to server0
ServerUtil.killServer(server1);
Thread.sleep(5000);
// Step 16. we now receive the messages that were on server1 but were scaled down to server0
for (int i = 0; i < numMessages / 2; i++) {
TextMessage message0 = (TextMessage) consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 1");
}
} finally {
if (connection0 != null) {
connection0.close();
}
if (connection1 != null) {
connection1.close();
}
ServerUtil.killServer(server0);
ServerUtil.killServer(server1);
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class DBExample method main.
public static void main(final String[] args) throws Exception {
InitialContext initialContext = null;
ConnectionFactory cf = new ActiveMQConnectionFactory();
try (Connection connection = cf.createConnection()) {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("queue1");
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage("this is a text message");
producer.send(message);
System.out.println("Sent message to " + queue.getQueueName() + ": " + message.getText());
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
System.out.println("Received message from " + queue.getQueueName() + ": " + messageReceived);
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class DivertExample method main.
public static void main(final String[] args) throws Exception {
Connection connectionLondon = null;
Connection connectionNewYork = null;
try {
// Step 2. Look-up the queue orderQueue on the London server - this is the queue any orders are sent to
Queue orderQueue = ActiveMQJMSClient.createQueue("orders");
// Step 3. Look-up the topic priceUpdates on the London server- this is the topic that any price updates are
// sent to
Topic priceUpdates = ActiveMQJMSClient.createTopic("priceUpdates");
// Step 4. Look-up the spy topic on the London server- this is what we will use to snoop on any orders
Topic spyTopic = ActiveMQJMSClient.createTopic("spyTopic");
// Step 7. Look-up the topic newYorkPriceUpdates on the New York server - any price updates sent to
// priceUpdates on the London server will
// be diverted to the queue priceForward on the London server, and a bridge will consume from that queue and
// forward
// them to the address newYorkPriceUpdates on the New York server where they will be distributed to the topic
// subscribers on
// the New York server
Topic newYorkPriceUpdates = ActiveMQJMSClient.createTopic("newYorkPriceUpdates");
// Step 8. Perform a lookup on the Connection Factory on the London server
ConnectionFactory cfLondon = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 9. Perform a lookup on the Connection Factory on the New York server
ConnectionFactory cfNewYork = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 10. Create a JMS Connection on the London server
connectionLondon = cfLondon.createConnection();
// Step 11. Create a JMS Connection on the New York server
connectionNewYork = cfNewYork.createConnection();
// Step 12. Create a JMS Session on the London server
Session sessionLondon = connectionLondon.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 13. Create a JMS Session on the New York server
Session sessionNewYork = connectionNewYork.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 14. Create a JMS MessageProducer orderProducer that sends to the queue orderQueue on the London server
MessageProducer orderProducer = sessionLondon.createProducer(orderQueue);
// Step 15. Create a JMS MessageProducer priceProducer that sends to the topic priceUpdates on the London
// server
MessageProducer priceProducer = sessionLondon.createProducer(priceUpdates);
// Step 15. Create a JMS subscriber which subscribes to the spyTopic on the London server
MessageConsumer spySubscriberA = sessionLondon.createConsumer(spyTopic);
// Step 16. Create another JMS subscriber which also subscribes to the spyTopic on the London server
MessageConsumer spySubscriberB = sessionLondon.createConsumer(spyTopic);
// Step 17. Create a JMS MessageConsumer which consumes orders from the order queue on the London server
MessageConsumer orderConsumer = sessionLondon.createConsumer(orderQueue);
// Step 18. Create a JMS subscriber which subscribes to the priceUpdates topic on the London server
MessageConsumer priceUpdatesSubscriberLondon = sessionLondon.createConsumer(priceUpdates);
// Step 19. Create a JMS subscriber which subscribes to the newYorkPriceUpdates topic on the New York server
MessageConsumer newYorkPriceUpdatesSubscriberA = sessionNewYork.createConsumer(newYorkPriceUpdates);
// Step 20. Create another JMS subscriber which also subscribes to the newYorkPriceUpdates topic on the New
// York server
MessageConsumer newYorkPriceUpdatesSubscriberB = sessionNewYork.createConsumer(newYorkPriceUpdates);
// Step 21. Start the connections
connectionLondon.start();
connectionNewYork.start();
// Step 22. Create an order message
TextMessage orderMessage = sessionLondon.createTextMessage("This is an order");
// Step 23. Send the order message to the order queue on the London server
orderProducer.send(orderMessage);
System.out.println("Sent message: " + orderMessage.getText());
// Step 24. The order message is consumed by the orderConsumer on the London server
TextMessage receivedOrder = (TextMessage) orderConsumer.receive(5000);
System.out.println("Received order: " + receivedOrder.getText());
// Step 25. A copy of the order is also received by the spyTopic subscribers on the London server
TextMessage spiedOrder1 = (TextMessage) spySubscriberA.receive(5000);
System.out.println("Snooped on order: " + spiedOrder1.getText());
TextMessage spiedOrder2 = (TextMessage) spySubscriberB.receive(5000);
System.out.println("Snooped on order: " + spiedOrder2.getText());
// Step 26. Create and send a price update message, destined for London
TextMessage priceUpdateMessageLondon = sessionLondon.createTextMessage("This is a price update for London");
priceUpdateMessageLondon.setStringProperty("office", "London");
priceProducer.send(priceUpdateMessageLondon);
// Step 27. The price update *should* be received by the local subscriber since we only divert messages
// where office = New York
TextMessage receivedUpdate = (TextMessage) priceUpdatesSubscriberLondon.receive(2000);
System.out.println("Received price update locally: " + receivedUpdate.getText());
// Step 28. The price update *should not* be received in New York
TextMessage priceUpdate1 = (TextMessage) newYorkPriceUpdatesSubscriberA.receive(1000);
if (priceUpdate1 != null) {
throw new IllegalStateException("Message is not null");
}
System.out.println("Did not received price update in New York, look it's: " + priceUpdate1);
TextMessage priceUpdate2 = (TextMessage) newYorkPriceUpdatesSubscriberB.receive(1000);
if (priceUpdate2 != null) {
throw new IllegalStateException("Message is not null");
}
System.out.println("Did not received price update in New York, look it's: " + priceUpdate2);
// Step 29. Create a price update message, destined for New York
TextMessage priceUpdateMessageNewYork = sessionLondon.createTextMessage("This is a price update for New York");
priceUpdateMessageNewYork.setStringProperty("office", "New York");
// Step 30. Send the price update message to the priceUpdates topic on the London server
priceProducer.send(priceUpdateMessageNewYork);
// Step 31. The price update *should not* be received by the local subscriber to the priceUpdates topic
// since it has been *exclusively* diverted to the priceForward queue, because it has a header saying
// it is destined for the New York office
Message message = priceUpdatesSubscriberLondon.receive(1000);
if (message != null) {
throw new IllegalStateException("Message is not null");
}
System.out.println("Didn't receive local price update, look, it's: " + message);
// Step 32. The remote subscribers on server 1 *should* receive a copy of the price update since
// it has been diverted to a local priceForward queue which has a bridge consuming from it and which
// forwards it to the same address on server 1.
// We notice how the forwarded messages have had a special header added by our custom transformer that
// we told the divert to use
priceUpdate1 = (TextMessage) newYorkPriceUpdatesSubscriberA.receive(5000);
System.out.println("Received forwarded price update on server 1: " + priceUpdate1.getText());
System.out.println("Time of forward: " + priceUpdate1.getLongProperty("time_of_forward"));
priceUpdate2 = (TextMessage) newYorkPriceUpdatesSubscriberB.receive(5000);
System.out.println("Received forwarded price update on server 2: " + priceUpdate2.getText());
System.out.println("Time of forward: " + priceUpdate2.getLongProperty("time_of_forward"));
} finally {
if (connectionLondon != null) {
connectionLondon.close();
}
if (connectionNewYork != null) {
connectionNewYork.close();
}
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class BrokerPluginExample method sendConsumeCore.
private static void sendConsumeCore() throws JMSException {
Connection connection = null;
try {
// Perform a lookup on the Connection Factory
ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
Queue queue = new ActiveMQQueue("exampleQueue");
// Create a JMS Connection
connection = cf.createConnection();
// Create a JMS Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
// Create a Text Message
TextMessage message = session.createTextMessage("This is a text message");
// Send the Message
producer.send(message);
// Create a JMS Message Consumer
MessageConsumer messageConsumer = session.createConsumer(queue);
// Start the Connection
connection.start();
// Receive the message
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
if (messageReceived.getStringProperty("count") == null) {
throw new RuntimeException(("missed property count"));
}
System.out.println("message = " + messageReceived.getText() + " property count (added by interceptor = " + messageReceived.getStringProperty("count") + ")");
} finally {
if (connection != null) {
connection.close();
}
}
}
Aggregations