Search in sources :

Example 1 with ServerSession

use of javax.jms.ServerSession in project activemq-artemis by apache.

the class FailoverTransactionTest method testFailoverWithConnectionConsumer.

// https://issues.apache.org/activemq/browse/AMQ-2772
@Test
public void testFailoverWithConnectionConsumer() throws Exception {
    LOG.info(this + " running test testFailoverWithConnectionConsumer");
    startCleanBroker();
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
    configureConnectionFactory(cf);
    Connection connection = cf.createConnection();
    connection.start();
    final CountDownLatch connectionConsumerGotOne = new CountDownLatch(1);
    try {
        Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        Queue destination = session.createQueue(QUEUE_NAME);
        final Session poolSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        connection.createConnectionConsumer(destination, null, new ServerSessionPool() {

            @Override
            public ServerSession getServerSession() throws JMSException {
                return new ServerSession() {

                    @Override
                    public Session getSession() throws JMSException {
                        return poolSession;
                    }

                    @Override
                    public void start() throws JMSException {
                        connectionConsumerGotOne.countDown();
                        poolSession.run();
                    }
                };
            }
        }, 1);
        MessageConsumer consumer = session.createConsumer(destination);
        MessageProducer producer;
        TextMessage message;
        final int count = 10;
        for (int i = 0; i < count; i++) {
            producer = session.createProducer(destination);
            message = session.createTextMessage("Test message: " + count);
            producer.send(message);
            producer.close();
        }
        // restart to force failover and connection state recovery before the commit
        broker.stop();
        startBroker();
        session.commit();
        for (int i = 0; i < count - 1; i++) {
            Message received = consumer.receive(20000);
            Assert.assertNotNull("Failed to get message: " + count, received);
        }
        session.commit();
    } finally {
        connection.close();
    }
    Assert.assertTrue("connectionconsumer did not get a message", connectionConsumerGotOne.await(10, TimeUnit.SECONDS));
}
Also used : ServerSessionPool(javax.jms.ServerSessionPool) ServerSession(javax.jms.ServerSession) ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) OpenWireConnection(org.apache.activemq.artemis.core.protocol.openwire.OpenWireConnection) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ServerSession(javax.jms.ServerSession) Test(org.junit.Test) OpenwireArtemisBaseTest(org.apache.activemq.broker.artemiswrapper.OpenwireArtemisBaseTest)

Example 2 with ServerSession

use of javax.jms.ServerSession in project activemq-artemis by apache.

the class OnePrefetchAsyncConsumerTest method testPrefetchExtension.

@Ignore("https://issues.apache.org/jira/browse/AMQ-5126")
@Test(timeout = 60 * 1000)
public void testPrefetchExtension() throws Exception {
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(queue);
    // when Msg1 is acked, the PrefetchSubscription will (incorrectly?) increment its prefetchExtension
    producer.send(session.createTextMessage("Msg1"));
    // Msg2 will exhaust the ServerSessionPool (since it only has 1 ServerSession)
    producer.send(session.createTextMessage("Msg2"));
    // Msg3 will cause the test to fail as it will attempt to retrieve an additional ServerSession from
    // an exhausted ServerSessionPool due to the (incorrectly?) incremented prefetchExtension in the
    // PrefetchSubscription
    producer.send(session.createTextMessage("Msg3"));
    session.commit();
    assertTrue("test completed on time", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return completed.get();
        }
    }));
    assertTrue("Attempted to retrieve more than one ServerSession at a time", success.get());
}
Also used : MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) ServerSession(javax.jms.ServerSession) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ServerSession

use of javax.jms.ServerSession in project activemq-artemis by apache.

the class RedeliveryPolicyTest method testRepeatedRedeliveryServerSessionNoCommit.

public void testRepeatedRedeliveryServerSessionNoCommit() throws Exception {
    connection.start();
    Session dlqSession = connection.createSession(true, Session.SESSION_TRANSACTED);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");
    MessageProducer producer = dlqSession.createProducer(destination);
    // Send the messages
    producer.send(dlqSession.createTextMessage("1st"));
    dlqSession.commit();
    MessageConsumer dlqConsumer = dlqSession.createConsumer(new ActiveMQQueue("ActiveMQ.DLQ"));
    final int maxRedeliveries = 4;
    final AtomicInteger receivedCount = new AtomicInteger(0);
    for (int i = 0; i <= maxRedeliveries + 1; i++) {
        connection = (ActiveMQConnection) factory.createConnection(userName, password);
        connections.add(connection);
        RedeliveryPolicy policy = connection.getRedeliveryPolicy();
        policy.setInitialRedeliveryDelay(0);
        policy.setUseExponentialBackOff(false);
        policy.setMaximumRedeliveries(maxRedeliveries);
        connection.start();
        final CountDownLatch done = new CountDownLatch(1);
        final ActiveMQSession session = (ActiveMQSession) connection.createSession(true, Session.SESSION_TRANSACTED);
        session.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                try {
                    ActiveMQTextMessage m = (ActiveMQTextMessage) message;
                    assertEquals("1st", m.getText());
                    assertEquals(receivedCount.get(), m.getRedeliveryCounter());
                    receivedCount.incrementAndGet();
                    done.countDown();
                } catch (Exception ignored) {
                    ignored.printStackTrace();
                }
            }
        });
        connection.createConnectionConsumer(destination, null, new ServerSessionPool() {

            @Override
            public ServerSession getServerSession() throws JMSException {
                return new ServerSession() {

                    @Override
                    public Session getSession() throws JMSException {
                        return session;
                    }

                    @Override
                    public void start() throws JMSException {
                    }
                };
            }
        }, 100, false);
        Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisified() throws Exception {
                session.run();
                return done.await(10, TimeUnit.MILLISECONDS);
            }
        });
        if (i <= maxRedeliveries) {
            assertTrue("listener done @" + i, done.await(5, TimeUnit.SECONDS));
        } else {
            // final redlivery gets poisoned before dispatch
            assertFalse("listener not done @" + i, done.await(1, TimeUnit.SECONDS));
        }
        connection.close();
        connections.remove(connection);
    }
    // We should be able to get the message off the DLQ now.
    TextMessage m = (TextMessage) dlqConsumer.receive(1000);
    assertNotNull("Got message from DLQ", m);
    assertEquals("1st", m.getText());
    String cause = m.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    assertTrue("cause exception has policy ref", cause.contains("RedeliveryPolicy"));
    dlqSession.commit();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ServerSessionPool(javax.jms.ServerSessionPool) ServerSession(javax.jms.ServerSession) TextMessage(javax.jms.TextMessage) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) JMSException(javax.jms.JMSException) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) Wait(org.apache.activemq.util.Wait) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Session(javax.jms.Session) ServerSession(javax.jms.ServerSession)

Aggregations

MessageProducer (javax.jms.MessageProducer)3 ServerSession (javax.jms.ServerSession)3 Session (javax.jms.Session)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 JMSException (javax.jms.JMSException)2 Message (javax.jms.Message)2 MessageConsumer (javax.jms.MessageConsumer)2 ServerSessionPool (javax.jms.ServerSessionPool)2 TextMessage (javax.jms.TextMessage)2 Test (org.junit.Test)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Connection (javax.jms.Connection)1 MessageListener (javax.jms.MessageListener)1 Queue (javax.jms.Queue)1 ActiveMQConnection (org.apache.activemq.ActiveMQConnection)1 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)1 ActiveMQMessageConsumer (org.apache.activemq.ActiveMQMessageConsumer)1 OpenWireConnection (org.apache.activemq.artemis.core.protocol.openwire.OpenWireConnection)1 OpenwireArtemisBaseTest (org.apache.activemq.broker.artemiswrapper.OpenwireArtemisBaseTest)1 ActiveMQMessage (org.apache.activemq.command.ActiveMQMessage)1