use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class JMSFailoverTest method testManualFailover.
@Test
public void testManualFailover() throws Exception {
ActiveMQConnectionFactory jbcfLive = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
jbcfLive.setBlockOnNonDurableSend(true);
jbcfLive.setBlockOnDurableSend(true);
ActiveMQConnectionFactory jbcfBackup = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, backupParams));
jbcfBackup.setBlockOnNonDurableSend(true);
jbcfBackup.setBlockOnDurableSend(true);
jbcfBackup.setInitialConnectAttempts(-1);
jbcfBackup.setReconnectAttempts(-1);
Connection connLive = jbcfLive.createConnection();
MyExceptionListener listener = new MyExceptionListener();
connLive.setExceptionListener(listener);
Session sessLive = connLive.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSession coreSessionLive = ((ActiveMQSession) sessLive).getCoreSession();
RemotingConnection coreConnLive = ((ClientSessionInternal) coreSessionLive).getConnection();
SimpleString jmsQueueName = new SimpleString("myqueue");
coreSessionLive.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
Queue queue = sessLive.createQueue("myqueue");
final int numMessages = 1000;
MessageProducer producerLive = sessLive.createProducer(queue);
for (int i = 0; i < numMessages; i++) {
TextMessage tm = sessLive.createTextMessage("message" + i);
producerLive.send(tm);
}
// Note we block on P send to make sure all messages get to server before failover
JMSUtil.crash(liveServer, coreSessionLive);
connLive.close();
// Now recreate on backup
Connection connBackup = jbcfBackup.createConnection();
Session sessBackup = connBackup.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumerBackup = sessBackup.createConsumer(queue);
connBackup.start();
for (int i = 0; i < numMessages; i++) {
TextMessage tm = (TextMessage) consumerBackup.receive(1000);
Assert.assertNotNull(tm);
Assert.assertEquals("message" + i, tm.getText());
}
TextMessage tm = (TextMessage) consumerBackup.receiveNoWait();
Assert.assertNull(tm);
connBackup.close();
}
use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class CloseDestroyedConnectionTest method testCloseDestroyedConnection.
/*
* Closing a connection that is destroyed should cleanly close everything without throwing exceptions
*/
@Test
public void testCloseDestroyedConnection() throws Exception {
long connectionTTL = 500;
cf.setClientFailureCheckPeriod(connectionTTL / 2);
// Need to set connection ttl to a low figure so connections get removed quickly on the server
cf.setConnectionTTL(connectionTTL);
conn = cf.createConnection();
Assert.assertEquals(1, server.getRemotingService().getConnections().size());
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Give time for the initial ping to reach the server before we fail (it has connection TTL in it)
Thread.sleep(500);
String queueName = "myqueue";
Queue queue = ActiveMQJMSClient.createQueue(queueName);
super.createQueue(queueName);
sess.createConsumer(queue);
sess.createProducer(queue);
sess.createBrowser(queue);
// Now fail the underlying connection
ClientSessionInternal sessi = (ClientSessionInternal) ((ActiveMQSession) sess).getCoreSession();
RemotingConnection rc = sessi.getConnection();
rc.fail(new ActiveMQInternalErrorException());
// Now close the connection
conn.close();
long start = System.currentTimeMillis();
while (true) {
int cons = server.getRemotingService().getConnections().size();
if (cons == 0) {
break;
}
long now = System.currentTimeMillis();
if (now - start > 10000) {
throw new Exception("Timed out waiting for connections to close");
}
Thread.sleep(50);
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class OpenWireManagementTest method checkQueueFromInternalAddress.
private boolean checkQueueFromInternalAddress(String queue) throws JMSException, ActiveMQException {
try (Connection coreConn = coreCf.createConnection()) {
ActiveMQSession session = (ActiveMQSession) coreConn.createSession();
ClientSession coreSession = session.getCoreSession();
ClientSession.QueueQuery query = coreSession.queueQuery(new SimpleString(queue));
assertTrue("Queue doesn't exist: " + queue, query.isExists());
SimpleString qAddr = query.getAddress();
return qAddr.toString().startsWith(AdvisorySupport.ADVISORY_TOPIC_PREFIX);
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.
the class SendAcknowledgementsExample method main.
public static void main(final String[] args) throws Exception {
Connection connection = null;
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Perfom a lookup on the queue
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
// Step 3. Perform a lookup on the Connection Factory
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 4. Create a JMS Connection
connection = cf.createConnection();
// Step 5. Define a SendAcknowledgementHandler which will receive asynchronous acknowledgements
class MySendAcknowledgementsHandler implements SendAcknowledgementHandler {
int count = 0;
@Override
public void sendAcknowledged(final Message message) {
System.out.println("Received send acknowledgement for message " + count++);
}
}
// Step 6. Create a JMS Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 7. Set the handler on the underlying core session
ClientSession coreSession = ((ActiveMQSession) session).getCoreSession();
coreSession.setSendAcknowledgementHandler(new MySendAcknowledgementsHandler());
// Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Step 7. Send 5000 messages, the handler will get called asynchronously some time later after the messages
// are sent.
final int numMessages = 5000;
for (int i = 0; i < numMessages; i++) {
javax.jms.Message jmsMessage = session.createMessage();
producer.send(jmsMessage);
System.out.println("Sent message " + i);
}
} finally {
// Step 12. Be sure to close our JMS resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
use of org.apache.activemq.artemis.jms.client.ActiveMQSession 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();
}
Aggregations