Search in sources :

Example 16 with ActiveMQSession

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

the class SessionClosedOnRemotingConnectionFailureTest method testSessionClosedOnRemotingConnectionFailure.

// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testSessionClosedOnRemotingConnectionFailure() throws Exception {
    List<TransportConfiguration> connectorConfigs = new ArrayList<>();
    connectorConfigs.add(new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jmsServer.createConnectionFactory("cffoo", false, JMSFactoryType.CF, registerConnectors(server, connectorConfigs), null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_CACHE_LARGE_MESSAGE_CLIENT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_COMPRESS_LARGE_MESSAGES, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_MAX_RETRY_INTERVAL, 0, ActiveMQClient.DEFAULT_FAILOVER_ON_INITIAL_CONNECTION, null, "/cffoo");
    cf = (ConnectionFactory) namingContext.lookup("/cffoo");
    Connection conn = cf.createConnection();
    Queue queue = createQueue("testQueue");
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer prod = session.createProducer(queue);
        MessageConsumer cons = session.createConsumer(queue);
        conn.start();
        prod.send(session.createMessage());
        Assert.assertNotNull(cons.receive());
        // Now fail the underlying connection
        RemotingConnection connection = ((ClientSessionInternal) ((ActiveMQSession) session).getCoreSession()).getConnection();
        connection.fail(new ActiveMQNotConnectedException());
        try {
            prod.send(session.createMessage());
            Assert.fail("Should throw exception");
        } catch (JMSException e) {
        // assertEquals(ActiveMQException.OBJECT_CLOSED, e.getCode());
        }
        try {
            cons.receive();
            Assert.fail("Should throw exception");
        } catch (JMSException e) {
        // assertEquals(ActiveMQException.OBJECT_CLOSED, e.getCode());
        }
        session.close();
        conn.close();
    } finally {
        try {
            conn.close();
        } catch (Throwable igonred) {
        }
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) MessageConsumer(javax.jms.MessageConsumer) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) JMSException(javax.jms.JMSException) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) Test(org.junit.Test)

Example 17 with ActiveMQSession

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

the class JMSReconnectTest method testReconnectOrReattachSameNode.

private void testReconnectOrReattachSameNode(boolean reattach) throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    jbcf.setReconnectAttempts(-1);
    if (reattach) {
        jbcf.setConfirmationWindowSize(1024 * 1024);
    }
    // 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 = jbcf.createConnection();
    MyExceptionListener listener = new MyExceptionListener();
    conn.setExceptionListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    RemotingConnection coreConn = ((ClientSessionInternal) coreSession).getConnection();
    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();
    Thread.sleep(2000);
    ActiveMQException me = new ActiveMQNotConnectedException();
    coreConn.fail(me);
    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);
    Assert.assertTrue(me == listener.e.getCause());
}
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) BytesMessage(javax.jms.BytesMessage) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) 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)

Example 18 with ActiveMQSession

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

the class JMSReconnectTest method testNoReconnectCloseAfterFailToReconnectWithTopicConsumer.

// If the server is shutdown after a non durable sub is created, then close on the connection should proceed normally
@Test
public void testNoReconnectCloseAfterFailToReconnectWithTopicConsumer() throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jbcf.setReconnectAttempts(0);
    Connection conn = jbcf.createConnection();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    coreSession.createQueue("mytopic", "blahblah", null, false);
    Topic topic = ActiveMQJMSClient.createTopic("mytopic");
    // Create a non durable subscriber
    sess.createConsumer(topic);
    Thread.sleep(2000);
    this.server.stop();
    this.server.start();
    sess.close();
    conn.close();
}
Also used : 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) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) Topic(javax.jms.Topic) 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 19 with ActiveMQSession

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

the class JMSFailoverTest method testCreateQueue.

// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testCreateQueue() throws Exception {
    liveJMSServer.createQueue(true, "queue1", null, true, "/queue/queue1");
    assertNotNull(ctx1.lookup("/queue/queue1"));
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
    jbcf.setReconnectAttempts(-1);
    Connection conn = null;
    try {
        conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
        Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
        JMSUtil.crash(liveServer, coreSession);
        assertNotNull(ctx2.lookup("/queue/queue1"));
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : 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) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) 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 20 with ActiveMQSession

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

the class JMSFailoverTest method testCreateTopic.

@Test
public void testCreateTopic() throws Exception {
    liveJMSServer.createTopic(true, "topic", "/topic/t1");
    assertNotNull(ctx1.lookup("//topic/t1"));
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
    jbcf.setReconnectAttempts(-1);
    Connection conn = null;
    try {
        conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
        Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
        JMSUtil.crash(liveServer, coreSession);
        assertNotNull(ctx2.lookup("/topic/t1"));
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : 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) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) 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