use of org.apache.activemq.artemis.api.core.client.ClientConsumer 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();
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer 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();
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer 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();
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer 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();
}
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ProducerFlowControlTest method testFlowControl.
private void testFlowControl(final int numMessages, final int messageSize, final int maxSize, final int producerWindowSize, final int consumerWindowSize, final int ackBatchSize, final int numConsumers, final int numProducers, final long consumerDelay, final boolean anon, final int minLargeMessageSize, final boolean realFiles) throws Exception {
final SimpleString address = new SimpleString("testaddress");
server = createServer(realFiles, isNetty());
AddressSettings addressSettings = new AddressSettings().setMaxSizeBytes(maxSize).setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK);
HierarchicalRepository<AddressSettings> repos = server.getAddressSettingsRepository();
repos.addMatch(address.toString(), addressSettings);
server.start();
waitForServerToStart(server);
locator.setProducerWindowSize(producerWindowSize).setConsumerWindowSize(consumerWindowSize).setAckBatchSize(ackBatchSize);
if (minLargeMessageSize != -1) {
locator.setMinLargeMessageSize(minLargeMessageSize);
}
sf = createSessionFactory(locator);
session = sf.createSession(false, true, true, true);
session.start();
final String queueName = "testqueue";
for (int i = 0; i < numConsumers; i++) {
session.createQueue(address, new SimpleString(queueName + i), null, false);
}
final byte[] bytes = RandomUtil.randomBytes(messageSize);
class MyHandler implements MessageHandler {
int count = 0;
final CountDownLatch latch = new CountDownLatch(1);
volatile Exception exception;
@Override
public void onMessage(final ClientMessage message) {
try {
byte[] bytesRead = new byte[messageSize];
message.getBodyBuffer().readBytes(bytesRead);
ActiveMQTestBase.assertEqualsByteArrays(bytes, bytesRead);
message.acknowledge();
if (++count == numMessages * numProducers) {
latch.countDown();
}
if (consumerDelay > 0) {
Thread.sleep(consumerDelay);
}
} catch (Exception e) {
ProducerFlowControlTest.log.error("Failed to handle message", e);
exception = e;
latch.countDown();
}
}
}
MyHandler[] handlers = new MyHandler[numConsumers];
for (int i = 0; i < numConsumers; i++) {
handlers[i] = new MyHandler();
ClientConsumer consumer = session.createConsumer(new SimpleString(queueName + i));
consumer.setMessageHandler(handlers[i]);
}
ClientProducer[] producers = new ClientProducer[numProducers];
for (int i = 0; i < numProducers; i++) {
if (anon) {
producers[i] = session.createProducer();
} else {
producers[i] = session.createProducer(address);
}
}
long start = System.currentTimeMillis();
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session.createMessage(false);
message.getBodyBuffer().writeBytes(bytes);
for (int j = 0; j < numProducers; j++) {
if (anon) {
producers[j].send(address, message);
} else {
producers[j].send(message);
}
}
}
for (int i = 0; i < numConsumers; i++) {
Assert.assertTrue(handlers[i].latch.await(5, TimeUnit.MINUTES));
Assert.assertNull(handlers[i].exception);
}
long end = System.currentTimeMillis();
double rate = 1000 * (double) numMessages / (end - start);
ProducerFlowControlTest.log.info("rate is " + rate + " msgs / sec");
}
Aggregations