Search in sources :

Example 66 with ClientMessage

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

the class InVMNonPersistentMessageBufferTest method testSendSameMessageMultipleTimes.

@Test
public void testSendSameMessageMultipleTimes() throws Exception {
    ClientMessage message = session.createMessage(false);
    final String body = RandomUtil.randomString();
    message.getBodyBuffer().writeString(body);
    int bodySize = message.getBodySize();
    for (int i = 0; i < 10; i++) {
        ClientMessage received = sendAndReceive(message);
        Assert.assertNotNull(received);
        Assert.assertEquals(bodySize, received.getBodySize());
        Assert.assertEquals(body, received.getBodyBuffer().readString());
        Assert.assertFalse(received.getBodyBuffer().readable());
    }
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Test(org.junit.Test)

Example 67 with ClientMessage

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

the class InVMNonPersistentMessageBufferTest method testSendMessageResetSendAgainDifferentBody.

@Test
public void testSendMessageResetSendAgainDifferentBody() throws Exception {
    ClientMessage message = session.createMessage(false);
    String body = RandomUtil.randomString();
    for (int i = 0; i < 10; i++) {
        // Make the body a bit longer each time
        body += "XX";
        message.getBodyBuffer().writeString(body);
        int bodySize = message.getBodySize();
        ClientMessage received = sendAndReceive(message);
        Assert.assertNotNull(received);
        Assert.assertEquals(bodySize, received.getBodySize());
        Assert.assertEquals(body, received.getBodyBuffer().readString());
        Assert.assertFalse(received.getBodyBuffer().readable());
        message.getBodyBuffer().clear();
        Assert.assertEquals(DataConstants.SIZE_INT, message.getBodyBuffer().writerIndex());
        Assert.assertEquals(DataConstants.SIZE_INT, message.getBodyBuffer().readerIndex());
    }
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Test(org.junit.Test)

Example 68 with ClientMessage

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

the class InVMNonPersistentMessageBufferTest method testCannotReadPastEndOfMessageBody.

@Test
public void testCannotReadPastEndOfMessageBody() throws Exception {
    ClientMessage message = session.createMessage(false);
    final String body = RandomUtil.randomString();
    message.getBodyBuffer().writeString(body);
    ClientMessage received = sendAndReceive(message);
    Assert.assertNotNull(received);
    ActiveMQBuffer buffer = received.getReadOnlyBodyBuffer();
    Assert.assertEquals(body, buffer.readString());
    try {
        buffer.readByte();
        Assert.fail("Should throw exception");
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
    Assert.assertEquals(body, received.getBodyBuffer().readString());
    try {
        received.getBodyBuffer().readByte();
        Assert.fail("Should throw exception");
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
    buffer = received.getReadOnlyBodyBuffer();
    Assert.assertEquals(body, buffer.readString());
    try {
        buffer.readByte();
        Assert.fail("Should throw exception");
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 69 with ClientMessage

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

the class InVMNonPersistentMessageBufferTest method testSimpleSendReceive.

/*
    * Test message can be read after being sent
    * Message can be sent multiple times
    * After sending, local message can be read
    * After sending, local message body can be added to and sent
    * When reset message body it should only reset to after packet headers
    * Should not be able to read past end of body into encoded message
    */
@Test
public void testSimpleSendReceive() throws Exception {
    ClientMessage message = session.createMessage(false);
    final String body = RandomUtil.randomString();
    message.getBodyBuffer().writeString(body);
    ClientMessage received = sendAndReceive(message);
    Assert.assertNotNull(received);
    Assert.assertEquals(body, received.getBodyBuffer().readString());
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Test(org.junit.Test)

Example 70 with ClientMessage

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

the class MessageHandlerTest method testSetUnsetResetMessageHandler.

@Test
public void testSetUnsetResetMessageHandler() throws Exception {
    final ClientSession session = sf.createSession(false, true, true);
    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);
        message.putIntProperty(new SimpleString("i"), i);
        producer.send(message);
    }
    final ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    CountDownLatch latch = new CountDownLatch(50);
    class MyHandler implements MessageHandler {

        int messageReceived = 0;

        boolean failed;

        boolean started = true;

        private final CountDownLatch latch;

        MyHandler(final CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public void onMessage(final ClientMessage message) {
            try {
                if (!started) {
                    failed = true;
                }
                messageReceived++;
                latch.countDown();
                if (latch.getCount() == 0) {
                    message.acknowledge();
                    started = false;
                    consumer.setMessageHandler(null);
                }
            } catch (Exception e) {
            }
        }
    }
    MyHandler handler = new MyHandler(latch);
    consumer.setMessageHandler(handler);
    waitForLatch(latch);
    Thread.sleep(100);
    Assert.assertFalse(handler.failed);
    // Make sure no exceptions were thrown from onMessage
    Assert.assertNull(consumer.getLastException());
    consumer.setMessageHandler(null);
    ClientMessage cm = consumer.receiveImmediate();
    Assert.assertNotNull(cm);
    latch = new CountDownLatch(49);
    handler = new MyHandler(latch);
    consumer.setMessageHandler(handler);
    session.start();
    Assert.assertTrue("message received " + handler.messageReceived, latch.await(5, TimeUnit.SECONDS));
    Thread.sleep(100);
    Assert.assertFalse(handler.failed);
    Assert.assertNull(consumer.getLastException());
    session.close();
}
Also used : MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) 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) 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