Search in sources :

Example 76 with ClientSessionFactory

use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.

the class MessageGroupingTest method doTestMultipleGroupingXARollback.

private void doTestMultipleGroupingXARollback() throws Exception {
    ServerLocator locator = createInVMNonHALocator();
    locator.setBlockOnAcknowledge(true);
    ClientSessionFactory sessionFactory = createSessionFactory(locator);
    ClientSession clientSession = sessionFactory.createSession(true, false, false);
    ClientProducer clientProducer = this.clientSession.createProducer(qName);
    clientSession.start();
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    Xid xid = new XidImpl("bq".getBytes(), 4, "gtid".getBytes());
    clientSession.start(xid, XAResource.TMNOFLAGS);
    SimpleString groupId = new SimpleString("grp1");
    SimpleString groupId2 = new SimpleString("grp2");
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(clientSession, "m" + i);
        if (i % 2 == 0 || i == 0) {
            message.putStringProperty(Message.HDR_GROUP_ID, groupId);
        } else {
            message.putStringProperty(Message.HDR_GROUP_ID, groupId2);
        }
        clientProducer.send(message);
    }
    CountDownLatch latch = new CountDownLatch(numMessages);
    DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true);
    consumer.setMessageHandler(dummyMessageHandler);
    DummyMessageHandler dummyMessageHandler2 = new DummyMessageHandler(latch, true);
    consumer2.setMessageHandler(dummyMessageHandler2);
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    clientSession.end(xid, XAResource.TMSUCCESS);
    Assert.assertEquals(dummyMessageHandler.list.size(), 50);
    int i = 0;
    for (ClientMessage message : dummyMessageHandler.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    Assert.assertEquals(dummyMessageHandler2.list.size(), 50);
    i = 1;
    for (ClientMessage message : dummyMessageHandler2.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    latch = new CountDownLatch(numMessages);
    dummyMessageHandler.reset(latch);
    dummyMessageHandler2.reset(latch);
    clientSession.rollback(xid);
    clientSession.start(xid, XAResource.TMNOFLAGS);
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    clientSession.end(xid, XAResource.TMSUCCESS);
    clientSession.prepare(xid);
    clientSession.commit(xid, false);
    Assert.assertEquals(dummyMessageHandler.list.size(), 50);
    i = 0;
    for (ClientMessage message : dummyMessageHandler.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    Assert.assertEquals(dummyMessageHandler2.list.size(), 50);
    i = 1;
    for (ClientMessage message : dummyMessageHandler2.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    consumer = this.clientSession.createConsumer(qName);
    Assert.assertNull(consumer.receiveImmediate());
    clientSession.close();
    locator.close();
}
Also used : Xid(javax.transaction.xa.Xid) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

Example 77 with ClientSessionFactory

use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.

the class MessageGroupingTest method doTestMultipleGroupingTXRollback.

private void doTestMultipleGroupingTXRollback() throws Exception {
    log.info("*** starting test");
    ServerLocator locator = createInVMNonHALocator();
    locator.setBlockOnAcknowledge(true);
    ClientSessionFactory sessionFactory = createSessionFactory(locator);
    ClientSession clientSession = sessionFactory.createSession(false, false, false);
    ClientProducer clientProducer = this.clientSession.createProducer(qName);
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    clientSession.start();
    // need to wait a bit or consumers might be busy
    Thread.sleep(200);
    SimpleString groupId = new SimpleString("grp1");
    SimpleString groupId2 = new SimpleString("grp2");
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(clientSession, "m" + i);
        if (i % 2 == 0 || i == 0) {
            message.putStringProperty(Message.HDR_GROUP_ID, groupId);
        } else {
            message.putStringProperty(Message.HDR_GROUP_ID, groupId2);
        }
        clientProducer.send(message);
    }
    CountDownLatch latch = new CountDownLatch(numMessages);
    DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true);
    consumer.setMessageHandler(dummyMessageHandler);
    DummyMessageHandler dummyMessageHandler2 = new DummyMessageHandler(latch, true);
    consumer2.setMessageHandler(dummyMessageHandler2);
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    Assert.assertEquals(50, dummyMessageHandler.list.size(), dummyMessageHandler.list.size());
    int i = 0;
    for (ClientMessage message : dummyMessageHandler.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    Assert.assertEquals(dummyMessageHandler2.list.size(), 50);
    i = 1;
    for (ClientMessage message : dummyMessageHandler2.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    latch = new CountDownLatch(numMessages);
    dummyMessageHandler.reset(latch);
    dummyMessageHandler2.reset(latch);
    clientSession.rollback();
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    Assert.assertEquals(dummyMessageHandler.list.size(), 50);
    i = 0;
    for (ClientMessage message : dummyMessageHandler.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    Assert.assertEquals(dummyMessageHandler2.list.size(), 50);
    i = 1;
    for (ClientMessage message : dummyMessageHandler2.list) {
        Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
        i += 2;
    }
    consumer = this.clientSession.createConsumer(qName);
    Assert.assertNull(consumer.receiveImmediate());
    clientSession.close();
    locator.close();
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

Example 78 with ClientSessionFactory

use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.

the class OrderTest method testSimpleStorage.

// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testSimpleStorage() throws Exception {
    server = createServer(persistent, true);
    server.start();
    locator.setBlockOnNonDurableSend(false).setBlockOnDurableSend(false).setBlockOnAcknowledge(true);
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(true, true, 0);
    session.createQueue("queue", "queue", true);
    ClientProducer prod = session.createProducer("queue");
    for (int i = 0; i < 100; i++) {
        ClientMessage msg = session.createMessage(i % 2 == 0);
        msg.putIntProperty("id", i);
        prod.send(msg);
    }
    session.close();
    boolean started = false;
    for (int start = 0; start < 2; start++) {
        if (persistent && start == 1) {
            started = true;
            server.stop();
            server.start();
            sf = createSessionFactory(locator);
        }
        session = sf.createSession(true, true);
        session.start();
        ClientConsumer cons = session.createConsumer("queue");
        for (int i = 0; i < 100; i++) {
            if (!started || started && i % 2 == 0) {
                ClientMessage msg = cons.receive(10000);
                Assert.assertEquals(i, msg.getIntProperty("id").intValue());
            }
        }
        cons.close();
        cons = session.createConsumer("queue");
        for (int i = 0; i < 100; i++) {
            if (!started || started && i % 2 == 0) {
                ClientMessage msg = cons.receive(10000);
                Assert.assertEquals(i, msg.getIntProperty("id").intValue());
            }
        }
        session.close();
    }
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 79 with ClientSessionFactory

use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.

the class AutogroupIdTest method testGroupIdAutomaticallyNotSet.

/*
   * tests that even though we have a grouping round robin distributor we don't pin the consumer as autogroup is false
   * */
@Test
public void testGroupIdAutomaticallyNotSet() throws Exception {
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(groupTestQ, groupTestQ, null, false);
    ClientProducer producer = session.createProducer(groupTestQ);
    final CountDownLatch latch = new CountDownLatch(100);
    MyMessageHandler myMessageHandler = new MyMessageHandler(latch);
    MyMessageHandler myMessageHandler2 = new MyMessageHandler(latch);
    ClientConsumer consumer = session.createConsumer(groupTestQ);
    consumer.setMessageHandler(myMessageHandler);
    ClientConsumer consumer2 = session.createConsumer(groupTestQ);
    consumer2.setMessageHandler(myMessageHandler2);
    session.start();
    final int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        producer.send(session.createMessage(false));
    }
    waitForLatch(latch);
    session.close();
    Assert.assertEquals(50, myMessageHandler.messagesReceived);
    Assert.assertEquals(50, myMessageHandler2.messagesReceived);
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 80 with ClientSessionFactory

use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.

the class CommitRollbackTest method testAsyncConsumerRollback.

@Test
public void testAsyncConsumerRollback() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    ServerLocator locator = createInVMNonHALocator().setBlockOnAcknowledge(true).setAckBatchSize(0);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession sendSession = cf.createSession(false, true, true);
    final ClientSession session = cf.createSession(false, true, false);
    sendSession.createQueue(addressA, queueA, false);
    ClientProducer cp = sendSession.createProducer(addressA);
    ClientConsumer cc = session.createConsumer(queueA);
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        cp.send(sendSession.createMessage(false));
    }
    CountDownLatch latch = new CountDownLatch(numMessages);
    session.start();
    cc.setMessageHandler(new ackHandler(session, latch));
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
    Assert.assertEquals(numMessages, q.getDeliveringCount());
    Assert.assertEquals(numMessages, getMessageCount(q));
    session.stop();
    session.rollback();
    Assert.assertEquals(0, q.getDeliveringCount());
    Assert.assertEquals(numMessages, getMessageCount(q));
    latch = new CountDownLatch(numMessages);
    cc.setMessageHandler(new ackHandler(session, latch));
    session.start();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    sendSession.close();
    session.close();
    cf.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Aggregations

ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)601 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)518 Test (org.junit.Test)464 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)395 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)372 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)350 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)277 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)254 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)206 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)108 Configuration (org.apache.activemq.artemis.core.config.Configuration)78 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)65 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)63 CountDownLatch (java.util.concurrent.CountDownLatch)54 Queue (org.apache.activemq.artemis.core.server.Queue)49 ArrayList (java.util.ArrayList)44 HashMap (java.util.HashMap)43 HashSet (java.util.HashSet)42 ActiveMQJAASSecurityManager (org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager)42 Message (org.apache.activemq.artemis.api.core.Message)41