use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class JMSUtil method createConnectionAndWaitForTopology.
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
public static ActiveMQConnection createConnectionAndWaitForTopology(ActiveMQConnectionFactory factory, int topologyMembers, int timeout) throws Exception {
ActiveMQConnection conn;
CountDownLatch countDownLatch = new CountDownLatch(topologyMembers);
ServerLocator locator = factory.getServerLocator();
locator.addClusterTopologyListener(new FailoverTestBase.LatchClusterTopologyListener(countDownLatch));
conn = (ActiveMQConnection) factory.createConnection();
boolean ok = countDownLatch.await(timeout, TimeUnit.SECONDS);
if (!ok) {
throw new IllegalStateException("timed out waiting for topology");
}
return conn;
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class ClientSideFailoverListerExample method main.
public static void main(final String[] args) throws Exception {
InitialContext initialContext = null;
Connection connectionA = null;
try {
server0 = ServerUtil.startServer(args[0], ClientSideFailoverListerExample.class.getSimpleName() + "0", 0, 5000);
server1 = ServerUtil.startServer(args[1], ClientSideFailoverListerExample.class.getSimpleName() + "1", 1, 0);
// Step 1. Get an initial context for looking up JNDI from server 0
initialContext = new InitialContext();
// Step 2. Look-up the JMS Queue object from JNDI
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 4. We create 1 JMS connections from the same connection factory.
// Wait a little while to make sure broadcasts from all nodes have reached the client
Thread.sleep(5000);
connectionA = connectionFactory.createConnection();
((ActiveMQConnection) connectionA).setFailoverListener(new FailoverListenerImpl());
// Step 5. We create JMS Sessions
Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 6. We create JMS MessageProducer objects on the sessions
MessageProducer producerA = sessionA.createProducer(queue);
// Step 7. We send some messages on each producer
final int numMessages = 10;
for (int i = 0; i < numMessages; i++) {
TextMessage messageA = sessionA.createTextMessage("A:This is text message " + i);
producerA.send(messageA);
System.out.println("Sent message: " + messageA.getText());
}
// Step 8. We start the connection to consume messages
connectionA.start();
// Step 9. We consume messages from the session A, one at a time.
// We reached message no 5 the first server will crash
consume(sessionA, queue, numMessages, "A");
} finally {
if (connectionA != null) {
connectionA.close();
}
if (initialContext != null) {
initialContext.close();
}
ServerUtil.killServer(server0);
ServerUtil.killServer(server1);
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class HQFailoverTest method failoverTest.
@Test
public void failoverTest() throws Throwable {
String textBody = "a rapadura e doce mas nao e mole nao";
String queueName = "queue";
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:5445?ha=true&reconnectAttempts=10&protocolManagerFactoryStr=org.apache.activemq.artemis.core.protocol.hornetq.client.HornetQClientProtocolManagerFactory&confirmationWindowSize=1048576&blockOnDurableSend=false");
ActiveMQConnection conn = (ActiveMQConnection) cf.createConnection();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(queueName);
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < 10; i++) {
producer.send(session.createTextMessage(textBody + i));
}
CountDownLatch latch = new CountDownLatch(1);
conn.setFailoverListener(eventType -> {
if (eventType == FailoverEventType.FAILOVER_COMPLETED) {
latch.countDown();
}
});
execute(serverClassloader, "liveServer.stop(true)");
assertTrue(latch.await(10, TimeUnit.SECONDS));
// This test is to validate stuff can still work well after failover against hornetq
for (int i = 0; i < 10; i++) {
producer.send(session.createTextMessage(textBody + i));
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class ExceptionListenerTest method testListenerCalledForOneConnectionAndSessions.
@Test
public void testListenerCalledForOneConnectionAndSessions() throws Exception {
Connection conn = cf.createConnection();
CountDownLatch latch = new CountDownLatch(1);
MyExceptionListener listener = new MyExceptionListener(latch);
conn.setExceptionListener(listener);
Session sess1 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sess2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sess3 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSessionInternal coreSession0 = (ClientSessionInternal) ((ActiveMQConnection) conn).getInitialSession();
ClientSessionInternal coreSession1 = (ClientSessionInternal) ((ActiveMQSession) sess1).getCoreSession();
ClientSessionInternal coreSession2 = (ClientSessionInternal) ((ActiveMQSession) sess2).getCoreSession();
ClientSessionInternal coreSession3 = (ClientSessionInternal) ((ActiveMQSession) sess3).getCoreSession();
coreSession0.getConnection().fail(new ActiveMQInternalErrorException("blah"));
coreSession1.getConnection().fail(new ActiveMQInternalErrorException("blah"));
coreSession2.getConnection().fail(new ActiveMQInternalErrorException("blah"));
coreSession3.getConnection().fail(new ActiveMQInternalErrorException("blah"));
latch.await(5, TimeUnit.SECONDS);
// Listener should only be called once even if all sessions connections die
Assert.assertEquals(1, listener.numCalls);
conn.close();
}
use of org.apache.activemq.artemis.jms.client.ActiveMQConnection in project activemq-artemis by apache.
the class ExceptionListenerTest method testListenerCalledForOneConnection.
@Test
public void testListenerCalledForOneConnection() throws Exception {
Connection conn = cf.createConnection();
CountDownLatch latch = new CountDownLatch(1);
MyExceptionListener listener = new MyExceptionListener(latch);
conn.setExceptionListener(listener);
ClientSessionInternal coreSession = (ClientSessionInternal) ((ActiveMQConnection) conn).getInitialSession();
coreSession.getConnection().fail(new ActiveMQInternalErrorException("blah"));
latch.await(5, TimeUnit.SECONDS);
Assert.assertEquals(1, listener.numCalls);
conn.close();
}
Aggregations