Search in sources :

Example 86 with ClientProducer

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

the class MessageGroupingTest method dotestMultipleGroupingXACommit.

private void dotestMultipleGroupingXACommit() throws Exception {
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sessionFactory = createSessionFactory(locator);
    ClientSession clientSession = sessionFactory.createSession(true, false, false);
    ClientProducer clientProducer = this.clientSession.createProducer(qName);
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    clientSession.start();
    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);
    clientSession.prepare(xid);
    clientSession.commit(xid, false);
    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;
    }
    consumer.close();
    consumer2.close();
    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 87 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer 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 88 with ClientProducer

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

the class MessageGroupingTest method doTestBasicGrouping.

private void doTestBasicGrouping() throws Exception {
    ClientProducer clientProducer = clientSession.createProducer(qName);
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    clientSession.start();
    SimpleString groupId = new SimpleString("grp1");
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(clientSession, "m" + i);
        message.putStringProperty(Message.HDR_GROUP_ID, groupId);
        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(100, dummyMessageHandler.list.size());
    Assert.assertEquals(0, dummyMessageHandler2.list.size());
    consumer.close();
    consumer2.close();
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) 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)

Example 89 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer 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 90 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer 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)

Aggregations

ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)859 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)716 Test (org.junit.Test)702 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)658 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)644 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)478 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)395 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)173 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)166 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)106 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)103 CountDownLatch (java.util.concurrent.CountDownLatch)102 Configuration (org.apache.activemq.artemis.core.config.Configuration)93 Queue (org.apache.activemq.artemis.core.server.Queue)85 Xid (javax.transaction.xa.Xid)70 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)64 HashMap (java.util.HashMap)63 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)55 ArrayList (java.util.ArrayList)51 QueueControl (org.apache.activemq.artemis.api.core.management.QueueControl)50