Search in sources :

Example 71 with ClientConsumer

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

the class MessageGroupingConnectionFactoryTest method doTestBasicGroupingUsingConnectionFactory.

private void doTestBasicGroupingUsingConnectionFactory() throws Exception {
    ClientProducer clientProducer = clientSession.createProducer(qName);
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    clientSession.start();
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(clientSession, "m" + i);
        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 : 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 72 with ClientConsumer

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

the class MessageCounterTest method testMessageCounter.

@Test
public void testMessageCounter() throws Exception {
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true);
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(null, null, false, false, false, false, 0);
    session.createQueue(QUEUE, QUEUE, null, false);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(session, "m" + i);
        producer.send(message);
    }
    session.commit();
    session.start();
    Assert.assertEquals(100, getMessageCount(server.getPostOffice(), QUEUE.toString()));
    ClientConsumer consumer = session.createConsumer(QUEUE, null, false);
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer.receive(1000);
        Assert.assertNotNull(message);
        message.acknowledge();
        session.commit();
        Assert.assertEquals("m" + i, message.getBodyBuffer().readString());
    }
    session.close();
    Assert.assertEquals(0, getMessageCount(server.getPostOffice(), QUEUE.toString()));
}
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 73 with ClientConsumer

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

the class MessageExpirationTest method testMessageExpiredWithExpiryAddress.

@Test
public void testMessageExpiredWithExpiryAddress() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    final SimpleString expiryAddress = RandomUtil.randomSimpleString();
    SimpleString expiryQueue = RandomUtil.randomSimpleString();
    server.getAddressSettingsRepository().addMatch(address.toString(), new AddressSettings() {

        private static final long serialVersionUID = -6476053400596299130L;

        @Override
        public SimpleString getExpiryAddress() {
            return expiryAddress;
        }
    });
    session.createQueue(address, queue, false);
    session.createQueue(expiryAddress, expiryQueue, false);
    ClientProducer producer = session.createProducer(address);
    ClientMessage message = session.createMessage(false);
    message.setExpiration(System.currentTimeMillis() + MessageExpirationTest.EXPIRATION);
    producer.send(message);
    Thread.sleep(MessageExpirationTest.EXPIRATION * 2);
    session.start();
    ClientConsumer consumer = session.createConsumer(queue);
    ClientMessage message2 = consumer.receiveImmediate();
    Assert.assertNull(message2);
    ClientConsumer expiryConsumer = session.createConsumer(expiryQueue);
    ClientMessage expiredMessage = expiryConsumer.receive(500);
    Assert.assertNotNull(expiredMessage);
    Assert.assertNotNull(expiredMessage.getObjectProperty(Message.HDR_ACTUAL_EXPIRY_TIME));
    Assert.assertEquals(address, expiredMessage.getObjectProperty(Message.HDR_ORIGINAL_ADDRESS));
    Assert.assertEquals(queue, expiredMessage.getObjectProperty(Message.HDR_ORIGINAL_QUEUE));
    consumer.close();
    expiryConsumer.close();
    session.deleteQueue(queue);
    session.deleteQueue(expiryQueue);
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) 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) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 74 with ClientConsumer

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

the class MessageExpirationTest method testMessageExpiredWithoutExpiryAddress.

@Test
public void testMessageExpiredWithoutExpiryAddress() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    session.createQueue(address, queue, false);
    ClientProducer producer = session.createProducer(address);
    ClientMessage message = session.createMessage(false);
    message.setExpiration(System.currentTimeMillis() + MessageExpirationTest.EXPIRATION);
    producer.send(message);
    Thread.sleep(MessageExpirationTest.EXPIRATION * 2);
    session.start();
    ClientConsumer consumer = session.createConsumer(queue);
    ClientMessage message2 = consumer.receiveImmediate();
    Assert.assertNull(message2);
    consumer.close();
    session.deleteQueue(queue);
}
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) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 75 with ClientConsumer

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

Aggregations

ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)720 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)642 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)624 Test (org.junit.Test)584 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)569 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)409 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)348 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)154 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)132 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)88 Configuration (org.apache.activemq.artemis.core.config.Configuration)81 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)79 CountDownLatch (java.util.concurrent.CountDownLatch)72 Queue (org.apache.activemq.artemis.core.server.Queue)63 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)62 Xid (javax.transaction.xa.Xid)55 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)52 HashMap (java.util.HashMap)48 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)46 ArrayList (java.util.ArrayList)44