use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class MessageHandlerTest method testSetUnsetMessageHandler.
@Test
public void testSetUnsetMessageHandler() 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);
session.close();
}
use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class HeuristicXATest method internalTest.
private void internalTest(final boolean isCommit) throws Exception {
Configuration configuration = createDefaultInVMConfig().setJMXManagementEnabled(true);
ActiveMQServer server = createServer(false, configuration);
server.setMBeanServer(mbeanServer);
server.start();
Xid xid = newXID();
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = sf.createSession(true, false, false);
session.createQueue(ADDRESS, ADDRESS, true);
session.start(xid, XAResource.TMNOFLAGS);
ClientProducer producer = session.createProducer(ADDRESS);
ClientMessage msg = session.createMessage(true);
msg.getBodyBuffer().writeString(body);
producer.send(msg);
session.end(xid, XAResource.TMSUCCESS);
session.prepare(xid);
session.close();
ActiveMQServerControl jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
String[] preparedTransactions = jmxServer.listPreparedTransactions();
Assert.assertEquals(1, preparedTransactions.length);
System.out.println(preparedTransactions[0]);
Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
if (isCommit) {
jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
} else {
jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
}
Assert.assertEquals(0, jmxServer.listPreparedTransactions().length);
if (isCommit) {
Assert.assertEquals(1, jmxServer.listHeuristicCommittedTransactions().length);
Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
} else {
Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
Assert.assertEquals(1, jmxServer.listHeuristicRolledBackTransactions().length);
}
if (isCommit) {
Assert.assertEquals(1, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
session = sf.createSession(false, false, false);
session.start();
ClientConsumer consumer = session.createConsumer(ADDRESS);
msg = consumer.receive(1000);
Assert.assertNotNull(msg);
msg.acknowledge();
Assert.assertEquals(body, msg.getBodyBuffer().readString());
session.commit();
session.close();
}
Assert.assertEquals(0, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
}
use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class HeuristicXATest method doHeuristicCompletionWithRestart.
private void doHeuristicCompletionWithRestart(final boolean isCommit) throws Exception {
Configuration configuration = createDefaultInVMConfig().setJMXManagementEnabled(true);
ActiveMQServer server = createServer(true, configuration);
server.setMBeanServer(mbeanServer);
server.start();
Xid xid = newXID();
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = sf.createSession(true, false, false);
session.createQueue(ADDRESS, ADDRESS, true);
session.start(xid, XAResource.TMNOFLAGS);
ClientProducer producer = session.createProducer(ADDRESS);
ClientMessage msg = session.createMessage(true);
msg.getBodyBuffer().writeString(body);
producer.send(msg);
session.end(xid, XAResource.TMSUCCESS);
session.prepare(xid);
session.close();
ActiveMQServerControl jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
String[] preparedTransactions = jmxServer.listPreparedTransactions();
Assert.assertEquals(1, preparedTransactions.length);
System.out.println(preparedTransactions[0]);
if (isCommit) {
jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
} else {
jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
}
preparedTransactions = jmxServer.listPreparedTransactions();
Assert.assertEquals(0, preparedTransactions.length);
if (isCommit) {
Assert.assertEquals(1, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
session = sf.createSession(false, false, false);
session.start();
ClientConsumer consumer = session.createConsumer(ADDRESS);
msg = consumer.receive(1000);
Assert.assertNotNull(msg);
msg.acknowledge();
Assert.assertEquals(body, msg.getBodyBuffer().readString());
session.commit();
session.close();
}
Assert.assertEquals(0, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
server.stop();
server.start();
jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
if (isCommit) {
String[] listHeuristicCommittedTransactions = jmxServer.listHeuristicCommittedTransactions();
Assert.assertEquals(1, listHeuristicCommittedTransactions.length);
System.out.println(listHeuristicCommittedTransactions[0]);
} else {
String[] listHeuristicRolledBackTransactions = jmxServer.listHeuristicRolledBackTransactions();
Assert.assertEquals(1, listHeuristicRolledBackTransactions.length);
System.out.println(listHeuristicRolledBackTransactions[0]);
}
}
use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class HeuristicXATest method doForgetHeuristicCompletedTxAndRestart.
private void doForgetHeuristicCompletedTxAndRestart(final boolean heuristicCommit) throws Exception {
Configuration configuration = createDefaultInVMConfig().setJMXManagementEnabled(true);
ActiveMQServer server = createServer(true, configuration);
server.setMBeanServer(mbeanServer);
server.start();
Xid xid = newXID();
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = sf.createSession(true, false, false);
session.createQueue(ADDRESS, ADDRESS, true);
session.start(xid, XAResource.TMNOFLAGS);
ClientProducer producer = session.createProducer(ADDRESS);
ClientMessage msg = session.createMessage(true);
msg.getBodyBuffer().writeBytes(new byte[123]);
producer.send(msg);
session.end(xid, XAResource.TMSUCCESS);
session.prepare(xid);
ActiveMQServerControl jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
String[] preparedTransactions = jmxServer.listPreparedTransactions();
Assert.assertEquals(1, preparedTransactions.length);
System.out.println(preparedTransactions[0]);
if (heuristicCommit) {
jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
} else {
jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
}
preparedTransactions = jmxServer.listPreparedTransactions();
Assert.assertEquals(0, preparedTransactions.length);
session.forget(xid);
session.close();
if (heuristicCommit) {
Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
} else {
Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
}
server.stop();
server.start();
// we need to recreate the sf
sf = createSessionFactory(locator);
session = sf.createSession(true, false, false);
Xid[] recoveredXids = session.recover(XAResource.TMSTARTRSCAN);
Assert.assertEquals(0, recoveredXids.length);
jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
if (heuristicCommit) {
Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
} else {
Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
}
session.close();
}
use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class InterruptedLargeMessageTest method testCloseConsumerDuringTransmission.
@Test
public void testCloseConsumerDuringTransmission() throws Exception {
ActiveMQServer server = createServer(true, isNetty());
LargeMessageTestInterceptorIgnoreLastPacket.disableInterrupt();
server.start();
locator.setBlockOnNonDurableSend(false).setBlockOnDurableSend(false).addIncomingInterceptor(new LargeMessageTestInterceptorIgnoreLastPacket());
ClientSessionFactory sf = createSessionFactory(locator);
final ClientSession session = sf.createSession(false, true, true);
session.createQueue(ADDRESS, ADDRESS, true);
ClientProducer producer = session.createProducer(ADDRESS);
Message clientFile = createLargeClientMessageStreaming(session, LARGE_MESSAGE_SIZE, true);
producer.send(clientFile);
session.commit();
LargeMessageTestInterceptorIgnoreLastPacket.clearInterrupt();
final AtomicInteger unexpectedErrors = new AtomicInteger(0);
final AtomicInteger expectedErrors = new AtomicInteger(0);
final ClientConsumer cons = session.createConsumer(ADDRESS);
session.start();
Thread t = new Thread() {
@Override
public void run() {
try {
System.out.println("Receiving message");
ClientMessage msg = cons.receive(5000);
if (msg == null) {
System.err.println("Message not received");
unexpectedErrors.incrementAndGet();
return;
}
msg.checkCompletion();
} catch (ActiveMQException e) {
e.printStackTrace();
expectedErrors.incrementAndGet();
}
}
};
t.start();
LargeMessageTestInterceptorIgnoreLastPacket.awaitInterrupt();
cons.close();
t.join();
Assert.assertEquals(0, unexpectedErrors.get());
Assert.assertEquals(1, expectedErrors.get());
session.close();
server.stop();
}
Aggregations