Search in sources :

Example 21 with ActiveMQSession

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

the class JMSFailoverTest method testSendReceiveLargeMessages.

@Test
public void testSendReceiveLargeMessages() throws Exception {
    SimpleString QUEUE = new SimpleString("somequeue");
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc, backuptc);
    jbcf.setReconnectAttempts(-1);
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    jbcf.setMinLargeMessageSize(1024);
    // jbcf.setConsumerWindowSize(0);
    // jbcf.setMinLargeMessageSize(1024);
    final CountDownLatch flagAlign = new CountDownLatch(1);
    final CountDownLatch waitToKill = new CountDownLatch(1);
    final AtomicBoolean killed = new AtomicBoolean(false);
    jbcf.getServerLocator().addIncomingInterceptor(new Interceptor() {

        int count = 0;

        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            if (packet instanceof SessionReceiveContinuationMessage) {
                if (count++ == 300 && !killed.get()) {
                    System.out.println("sending countDown on latch waitToKill");
                    killed.set(true);
                    waitToKill.countDown();
                }
            }
            return true;
        }
    });
    Connection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
    Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
    final ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    // The thread that will fail the server
    Thread spoilerThread = new Thread() {

        @Override
        public void run() {
            flagAlign.countDown();
            // a large timeout just to help in case of debugging
            try {
                waitToKill.await(120, TimeUnit.SECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                System.out.println("Killing server...");
                JMSUtil.crash(liveServer, coreSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    coreSession.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, true);
    Queue queue = sess.createQueue("somequeue");
    MessageProducer producer = sess.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    for (int i = 0; i < 100; i++) {
        TextMessage message = sess.createTextMessage(new String(new byte[10 * 1024]));
        producer.send(message);
        if (i % 10 == 0) {
            sess.commit();
        }
    }
    sess.commit();
    conn.start();
    spoilerThread.start();
    assertTrue(flagAlign.await(10, TimeUnit.SECONDS));
    MessageConsumer consumer = sess.createConsumer(queue);
    // this test is not meant to validate transactionality during Failover as that would require XA and recovery
    for (int i = 0; i < 90; i++) {
        TextMessage message = null;
        int retryNrs = 0;
        do {
            retryNrs++;
            try {
                message = (TextMessage) consumer.receive(5000);
                assertNotNull(message);
                break;
            } catch (JMSException e) {
                new Exception("Exception on receive message", e).printStackTrace();
            }
        } while (retryNrs < 10);
        assertNotNull(message);
        try {
            sess.commit();
        } catch (Exception e) {
            new Exception("Exception during commit", e);
            sess.rollback();
        }
    }
    conn.close();
    spoilerThread.join();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) SessionReceiveContinuationMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionReceiveContinuationMessage) MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSException(javax.jms.JMSException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) JMSException(javax.jms.JMSException) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) 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