Search in sources :

Example 56 with ClientMessage

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

the class AddressSettingsTest method testSimpleHierarchyWithDLA.

@Test
public void testSimpleHierarchyWithDLA() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    AddressSettings addressSettings = new AddressSettings().setDeadLetterAddress(dlaA).setMaxDeliveryAttempts(1);
    AddressSettings addressSettings2 = new AddressSettings().setDeadLetterAddress(dlaB).setMaxDeliveryAttempts(1);
    HierarchicalRepository<AddressSettings> repos = server.getAddressSettingsRepository();
    repos.addMatch(addressA.toString(), addressSettings);
    repos.addMatch(addressB.toString(), addressSettings2);
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, false);
    session.createQueue(addressA, queueA, false);
    session.createQueue(addressB, queueB, false);
    session.createQueue(dlaA, dlqA, false);
    session.createQueue(dlaB, dlqB, false);
    ClientSession sendSession = sf.createSession(false, true, true);
    ClientMessage cm = sendSession.createMessage(true);
    cm.getBodyBuffer().writeString("A");
    ClientMessage cm2 = sendSession.createMessage(true);
    cm2.getBodyBuffer().writeString("B");
    ClientProducer cp1 = sendSession.createProducer(addressA);
    ClientProducer cp2 = sendSession.createProducer(addressB);
    cp1.send(cm);
    cp2.send(cm2);
    ClientConsumer dlqARec = session.createConsumer(dlqA);
    ClientConsumer dlqBrec = session.createConsumer(dlqB);
    ClientConsumer cc1 = session.createConsumer(queueA);
    ClientConsumer cc2 = session.createConsumer(queueB);
    session.start();
    ClientMessage message = cc1.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    message = cc2.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    session.rollback();
    cc1.close();
    cc2.close();
    message = dlqARec.receive(5000);
    Assert.assertNotNull(message);
    Assert.assertEquals("A", message.getBodyBuffer().readString());
    message = dlqBrec.receive(5000);
    Assert.assertNotNull(message);
    Assert.assertEquals("B", message.getBodyBuffer().readString());
    sendSession.close();
    session.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) 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) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 57 with ClientMessage

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

the class CoreClientTest method testCoreClient.

private void testCoreClient(final boolean netty, ServerLocator serverLocator) throws Exception {
    final SimpleString QUEUE = new SimpleString("CoreClientTestQueue");
    ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(createDefaultConfig(netty), false));
    server.start();
    ServerLocator locator = serverLocator == null ? createNonHALocator(netty) : serverLocator;
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(QUEUE, QUEUE, null, false);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 1000;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
        message.putStringProperty("foo", "bar");
        // One way around the setting destination problem is as follows -
        // Remove destination as an attribute from client producer.
        // The destination always has to be set explicitly before sending a message
        message.setAddress(QUEUE);
        message.getBodyBuffer().writeString("testINVMCoreClient");
        producer.send(message);
    }
    CoreClientTest.log.info("sent messages");
    ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message2 = consumer.receive();
        ActiveMQBuffer buffer = message2.getBodyBuffer();
        Assert.assertEquals("testINVMCoreClient", buffer.readString());
        message2.acknowledge();
    }
    sf.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) 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) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Example 58 with ClientMessage

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

the class DeliveryOrderTest method testSendDeliveryOrderOnCommit.

@Test
public void testSendDeliveryOrderOnCommit() throws Exception {
    ClientSession sendSession = cf.createSession(false, false, true);
    ClientProducer cp = sendSession.createProducer(addressA);
    int numMessages = 1000;
    sendSession.createQueue(addressA, queueA, false);
    for (int i = 0; i < numMessages; i++) {
        ClientMessage cm = sendSession.createMessage(false);
        cm.getBodyBuffer().writeInt(i);
        cp.send(cm);
        if (i % 10 == 0) {
            sendSession.commit();
        }
        sendSession.commit();
    }
    ClientConsumer c = sendSession.createConsumer(queueA);
    sendSession.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage cm = c.receive(5000);
        Assert.assertNotNull(cm);
        Assert.assertEquals(i, cm.getBodyBuffer().readInt());
    }
    sendSession.close();
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) 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 59 with ClientMessage

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

the class DeliveryOrderTest method testMultipleConsumersMessageOrder.

@Test
public void testMultipleConsumersMessageOrder() throws Exception {
    ClientSession sendSession = cf.createSession(false, true, true);
    ClientSession recSession = cf.createSession(false, true, true);
    sendSession.createQueue(addressA, queueA, false);
    int numReceivers = 100;
    AtomicInteger count = new AtomicInteger(0);
    int numMessage = 10000;
    ClientConsumer[] clientConsumers = new ClientConsumer[numReceivers];
    Receiver[] receivers = new Receiver[numReceivers];
    CountDownLatch latch = new CountDownLatch(numMessage);
    for (int i = 0; i < numReceivers; i++) {
        clientConsumers[i] = recSession.createConsumer(queueA);
        receivers[i] = new Receiver(latch);
        clientConsumers[i].setMessageHandler(receivers[i]);
    }
    recSession.start();
    ClientProducer clientProducer = sendSession.createProducer(addressA);
    for (int i = 0; i < numMessage; i++) {
        ClientMessage cm = sendSession.createMessage(false);
        cm.getBodyBuffer().writeInt(count.getAndIncrement());
        clientProducer.send(cm);
    }
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    for (Receiver receiver : receivers) {
        Assert.assertFalse("" + receiver.lastMessage, receiver.failed);
    }
    sendSession.close();
    recSession.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) 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) Test(org.junit.Test)

Example 60 with ClientMessage

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

the class ExpireTestOnRestartTest method testRestartWithExpire.

// The biggest problem on this test was the exceptions that happened. I couldn't find any wrong state beyond the exceptions
@Test
public void testRestartWithExpire() throws Exception {
    int NUMBER_OF_EXPIRED_MESSAGES = 1000;
    ServerLocator locator = createInVMNonHALocator();
    locator.setBlockOnDurableSend(false);
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(true, true);
    session.createQueue("test", "test", true);
    session.createQueue("exp", "exp", true);
    ClientProducer prod = session.createProducer("test");
    for (int i = 0; i < 10; i++) {
        ClientMessage message = session.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[1024 * 10]);
        prod.send(message);
    }
    for (int i = 0; i < NUMBER_OF_EXPIRED_MESSAGES; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("i", i);
        message.getBodyBuffer().writeBytes(new byte[1024 * 10]);
        message.setExpiration(System.currentTimeMillis() + 5000);
        prod.send(message);
    }
    session.commit();
    session.close();
    server.stop();
    server.getConfiguration().setMessageExpiryScanPeriod(1);
    // enough time for expiration of the messages
    Thread.sleep(5500);
    server.start();
    Queue queue = server.locateQueue(SimpleString.toSimpleString("test"));
    factory = locator.createSessionFactory();
    session = factory.createSession(false, false);
    ClientConsumer cons = session.createConsumer("test");
    session.start();
    for (int i = 0; i < 10; i++) {
        ClientMessage msg = cons.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    assertNull(cons.receiveImmediate());
    cons.close();
    long timeout = System.currentTimeMillis() + 60000;
    while (queue.getPageSubscription().getPagingStore().isPaging() && timeout > System.currentTimeMillis()) {
        Thread.sleep(1);
    }
    assertFalse(queue.getPageSubscription().getPagingStore().isPaging());
    cons = session.createConsumer("exp");
    for (int i = 0; i < NUMBER_OF_EXPIRED_MESSAGES; i++) {
        ClientMessage msg = cons.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    session.commit();
    int extras = 0;
    ClientMessage msg;
    while ((msg = cons.receiveImmediate()) != null) {
        System.out.println(msg);
        extras++;
    }
    assertEquals("Received extra messages on expire address", 0, extras);
    session.commit();
    session.close();
    locator.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) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Aggregations

ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)889 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)708 Test (org.junit.Test)647 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)623 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)606 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)445 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)363 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)167 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)140 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)102 CountDownLatch (java.util.concurrent.CountDownLatch)99 Configuration (org.apache.activemq.artemis.core.config.Configuration)87 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)85 Queue (org.apache.activemq.artemis.core.server.Queue)72 Xid (javax.transaction.xa.Xid)67 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)63 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)54 HashMap (java.util.HashMap)53 ArrayList (java.util.ArrayList)51 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)47