use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory 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.ClientSessionFactory 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.ClientSessionFactory 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.ClientSessionFactory in project activemq-artemis by apache.
the class AutogroupIdTest method testGroupIdAutomaticallyNotSet.
/*
* tests that even though we have a grouping round robin distributor we don't pin the consumer as autogroup is false
* */
@Test
public void testGroupIdAutomaticallyNotSet() throws Exception {
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = sf.createSession(false, true, true);
session.createQueue(groupTestQ, groupTestQ, null, false);
ClientProducer producer = session.createProducer(groupTestQ);
final CountDownLatch latch = new CountDownLatch(100);
MyMessageHandler myMessageHandler = new MyMessageHandler(latch);
MyMessageHandler myMessageHandler2 = new MyMessageHandler(latch);
ClientConsumer consumer = session.createConsumer(groupTestQ);
consumer.setMessageHandler(myMessageHandler);
ClientConsumer consumer2 = session.createConsumer(groupTestQ);
consumer2.setMessageHandler(myMessageHandler2);
session.start();
final int numMessages = 100;
for (int i = 0; i < numMessages; i++) {
producer.send(session.createMessage(false));
}
waitForLatch(latch);
session.close();
Assert.assertEquals(50, myMessageHandler.messagesReceived);
Assert.assertEquals(50, myMessageHandler2.messagesReceived);
}
use of org.apache.activemq.artemis.api.core.client.ClientSessionFactory in project activemq-artemis by apache.
the class CommitRollbackTest method testAsyncConsumerRollback.
@Test
public void testAsyncConsumerRollback() throws Exception {
ActiveMQServer server = createServer(false);
server.start();
ServerLocator locator = createInVMNonHALocator().setBlockOnAcknowledge(true).setAckBatchSize(0);
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession sendSession = cf.createSession(false, true, true);
final ClientSession session = cf.createSession(false, true, false);
sendSession.createQueue(addressA, queueA, false);
ClientProducer cp = sendSession.createProducer(addressA);
ClientConsumer cc = session.createConsumer(queueA);
int numMessages = 100;
for (int i = 0; i < numMessages; i++) {
cp.send(sendSession.createMessage(false));
}
CountDownLatch latch = new CountDownLatch(numMessages);
session.start();
cc.setMessageHandler(new ackHandler(session, latch));
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
Assert.assertEquals(numMessages, q.getDeliveringCount());
Assert.assertEquals(numMessages, getMessageCount(q));
session.stop();
session.rollback();
Assert.assertEquals(0, q.getDeliveringCount());
Assert.assertEquals(numMessages, getMessageCount(q));
latch = new CountDownLatch(numMessages);
cc.setMessageHandler(new ackHandler(session, latch));
session.start();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
sendSession.close();
session.close();
cf.close();
}
Aggregations