Search in sources :

Example 11 with ActiveMQSession

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();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 12 with ActiveMQSession

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);
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) Queue(javax.jms.Queue) JMSException(javax.jms.JMSException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) Test(org.junit.Test)

Example 13 with ActiveMQSession

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);
    }
}
Also used : ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Connection(javax.jms.Connection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Example 14 with ActiveMQSession

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();
        }
    }
}
Also used : Message(org.apache.activemq.artemis.api.core.Message) Connection(javax.jms.Connection) SendAcknowledgementHandler(org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler) InitialContext(javax.naming.InitialContext) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession)

Example 15 with ActiveMQSession

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();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) Test(org.junit.Test)

Aggregations

ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)21 Session (javax.jms.Session)18 Connection (javax.jms.Connection)17 Test (org.junit.Test)16 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)14 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)12 Queue (javax.jms.Queue)11 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)11 MessageConsumer (javax.jms.MessageConsumer)10 MessageProducer (javax.jms.MessageProducer)10 TextMessage (javax.jms.TextMessage)9 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)9 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)7 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)7 JMSException (javax.jms.JMSException)5 BytesMessage (javax.jms.BytesMessage)4 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)4 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)3 ActiveMQConnection (org.apache.activemq.artemis.jms.client.ActiveMQConnection)3 CountDownLatch (java.util.concurrent.CountDownLatch)2