use of org.apache.activemq.artemis.api.core.client.ClientProducer 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();
}
use of org.apache.activemq.artemis.api.core.client.ClientProducer 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.ClientProducer 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.ClientProducer 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.ClientProducer 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();
}
}
Aggregations