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());
}
}
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());
}
}
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
}
}
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());
}
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();
}
Aggregations