Search in sources :

Example 6 with ActiveMQSession

use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.

the class JMSFailoverListenerTest method testAutomaticFailover.

// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testAutomaticFailover() throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
    jbcf.setReconnectAttempts(-1);
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    // Note we set consumer window size to a value so we can verify that consumer credit re-sending
    // works properly on failover
    // The value is small enough that credits will have to be resent several time
    final int numMessages = 10;
    final int bodySize = 1000;
    jbcf.setConsumerWindowSize(numMessages * bodySize / 10);
    ActiveMQConnection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
    MyFailoverListener listener = new MyFailoverListener();
    conn.setFailoverListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    SimpleString jmsQueueName = new SimpleString("myqueue");
    coreSession.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
    Queue queue = sess.createQueue("myqueue");
    MessageProducer producer = sess.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    MessageConsumer consumer = sess.createConsumer(queue);
    byte[] body = RandomUtil.randomBytes(bodySize);
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.writeBytes(body);
        producer.send(bm);
    }
    conn.start();
    JMSFailoverListenerTest.log.info("sent messages and started connection");
    Thread.sleep(2000);
    JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
    Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
    for (int i = 0; i < numMessages; i++) {
        JMSFailoverListenerTest.log.info("got message " + i);
        BytesMessage bm = (BytesMessage) consumer.receive(1000);
        Assert.assertNotNull(bm);
        Assert.assertEquals(body.length, bm.getBodyLength());
    }
    TextMessage tm = (TextMessage) consumer.receiveNoWait();
    Assert.assertNull(tm);
    Assert.assertEquals(FailoverEventType.FAILOVER_COMPLETED, listener.get(1));
    conn.close();
    Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BytesMessage(javax.jms.BytesMessage) 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 7 with ActiveMQSession

use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.

the class JMSFailoverListenerTest 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);
    ActiveMQConnection connLive = (ActiveMQConnection) jbcfLive.createConnection();
    MyFailoverListener listener = new MyFailoverListener();
    connLive.setFailoverListener(listener);
    Session sessLive = connLive.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSessionLive = ((ActiveMQSession) sessLive).getCoreSession();
    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);
    Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
    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.assertEquals(FailoverEventType.FAILOVER_FAILED, listener.get(1));
    Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
    Assert.assertNull(tm);
    connBackup.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) 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 8 with ActiveMQSession

use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.

the class JMSReconnectTest method testReconnectSameNodeServerRestartedWithNonDurableSubOrTempQueue.

// Test that non durable JMS sub gets recreated in auto reconnect
private void testReconnectSameNodeServerRestartedWithNonDurableSubOrTempQueue(final boolean nonDurableSub) throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jbcf.setReconnectAttempts(-1);
    Connection conn = jbcf.createConnection();
    MyExceptionListener listener = new MyExceptionListener();
    conn.setExceptionListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    Destination dest;
    if (nonDurableSub) {
        coreSession.createQueue("mytopic", "blahblah", null, false);
        dest = ActiveMQJMSClient.createTopic("mytopic");
    } else {
        dest = sess.createTemporaryQueue();
    }
    MessageProducer producer = sess.createProducer(dest);
    // Create a non durable subscriber
    MessageConsumer consumer = sess.createConsumer(dest);
    this.server.stop();
    this.server.start();
    // Allow client some time to reconnect
    Thread.sleep(3000);
    final int numMessages = 100;
    byte[] body = RandomUtil.randomBytes(1000);
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.writeBytes(body);
        producer.send(bm);
    }
    conn.start();
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = (BytesMessage) consumer.receive(1000);
        Assert.assertNotNull(bm);
        Assert.assertEquals(body.length, bm.getBodyLength());
    }
    TextMessage tm = (TextMessage) consumer.receiveNoWait();
    Assert.assertNull(tm);
    conn.close();
    Assert.assertNotNull(listener.e);
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) BytesMessage(javax.jms.BytesMessage) 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) 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)

Example 9 with ActiveMQSession

use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.

the class ActiveMQServerControlTest method testCloseJMSclient.

@Test
public void testCloseJMSclient() throws Exception {
    ActiveMQServerControl serverControl = createManagementControl();
    ConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    Connection conn = cf.createConnection();
    conn.start();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    javax.jms.Topic topic = ActiveMQJMSClient.createTopic("ConsumerTestTopic");
    MessageConsumer JMSclient = session.createConsumer(topic, "test1");
    long clientID = -1;
    String sessionID = ((ClientSessionImpl) (((ActiveMQSession) session).getCoreSession())).getName();
    Set<ServerSession> sessions = server.getSessions();
    for (ServerSession sess : sessions) {
        if (sess.getName().equals(sessionID.toString())) {
            Set<ServerConsumer> serverConsumers = sess.getServerConsumers();
            for (ServerConsumer serverConsumer : serverConsumers) {
                clientID = serverConsumer.sequentialID();
            }
        }
    }
    Assert.assertFalse(((org.apache.activemq.artemis.jms.client.ActiveMQMessageConsumer) JMSclient).isClosed());
    serverControl.closeConsumerWithID(sessionID, Long.toString(clientID));
    Wait.waitFor(() -> ((org.apache.activemq.artemis.jms.client.ActiveMQMessageConsumer) JMSclient).isClosed());
    Assert.assertTrue(((org.apache.activemq.artemis.jms.client.ActiveMQMessageConsumer) JMSclient).isClosed());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ServerConsumer(org.apache.activemq.artemis.core.server.ServerConsumer) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSessionImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionImpl) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 10 with ActiveMQSession

use of org.apache.activemq.artemis.jms.client.ActiveMQSession in project activemq-artemis by apache.

the class JMSFailoverTest method testAutomaticFailover.

@Test
public void testAutomaticFailover() throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
    jbcf.setReconnectAttempts(-1);
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    // Note we set consumer window size to a value so we can verify that consumer credit re-sending
    // works properly on failover
    // The value is small enough that credits will have to be resent several time
    final int numMessages = 10;
    final int bodySize = 1000;
    jbcf.setConsumerWindowSize(numMessages * bodySize / 10);
    Connection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
    MyExceptionListener listener = new MyExceptionListener();
    conn.setExceptionListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    SimpleString jmsQueueName = new SimpleString("myqueue");
    coreSession.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
    Queue queue = sess.createQueue("myqueue");
    MessageProducer producer = sess.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    MessageConsumer consumer = sess.createConsumer(queue);
    byte[] body = RandomUtil.randomBytes(bodySize);
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.writeBytes(body);
        producer.send(bm);
    }
    conn.start();
    JMSFailoverTest.log.info("sent messages and started connection");
    Thread.sleep(2000);
    JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
    for (int i = 0; i < numMessages; i++) {
        JMSFailoverTest.log.info("got message " + i);
        BytesMessage bm = (BytesMessage) consumer.receive(1000);
        Assert.assertNotNull(bm);
        Assert.assertEquals(body.length, bm.getBodyLength());
    }
    TextMessage tm = (TextMessage) consumer.receiveNoWait();
    Assert.assertNull(tm);
    conn.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BytesMessage(javax.jms.BytesMessage) 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)

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