use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ActiveMQMessageHandlerTest method testSimpleMessageReceivedOnQueueTwoPhase.
@Test
@BMRules(rules = { @BMRule(name = "interrupt", targetClass = "org.apache.activemq.artemis.core.protocol.core.impl.ActiveMQSessionContext", targetMethod = "xaEnd", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.ActiveMQMessageHandlerTest.interrupt();") })
public void testSimpleMessageReceivedOnQueueTwoPhase() throws Exception {
ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
resourceAdapter = qResourceAdapter;
MyBootstrapContext ctx = new MyBootstrapContext();
qResourceAdapter.setConnectorClassName(NETTY_CONNECTOR_FACTORY);
qResourceAdapter.start(ctx);
ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
spec.setMaxSession(1);
spec.setCallTimeout(1000L);
spec.setResourceAdapter(qResourceAdapter);
spec.setUseJNDI(false);
spec.setDestinationType("javax.jms.Queue");
spec.setDestination(MDBQUEUE);
CountDownLatch latch = new CountDownLatch(1);
XADummyEndpoint endpoint = new XADummyEndpoint(latch, true);
DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, true);
qResourceAdapter.endpointActivation(endpointFactory, spec);
ClientSession session = locator.createSessionFactory().createSession();
ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED);
ClientMessage message = session.createMessage(true);
message.getBodyBuffer().writeString("teststring");
clientProducer.send(message);
session.close();
latch.await(5, TimeUnit.SECONDS);
assertNotNull(endpoint.lastMessage);
assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "teststring");
qResourceAdapter.endpointDeactivation(endpointFactory, spec);
qResourceAdapter.stop();
Binding binding = server.getPostOffice().getBinding(SimpleString.toSimpleString(MDBQUEUEPREFIXED));
assertEquals(1, getMessageCount(((Queue) binding.getBindable())));
server.stop();
server.start();
ClientSessionFactory factory = locator.createSessionFactory();
session = factory.createSession(true, true);
session.start();
ClientConsumer consumer = session.createConsumer(MDBQUEUEPREFIXED);
assertNotNull(consumer.receive(5000));
session.close();
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class BMFailoverTest method testFailoverOnCommit2.
@Test
@BMRules(rules = { @BMRule(name = "trace clientsessionimpl commit", targetClass = "org.apache.activemq.artemis.core.client.impl.ClientSessionImpl", targetMethod = "start(javax.transaction.xa.Xid, int)", targetLocation = "AT EXIT", action = "org.apache.activemq.artemis.tests.extras.byteman.BMFailoverTest.serverToStop.getServer().stop(true)") })
public void testFailoverOnCommit2() throws Exception {
serverToStop = liveServer;
locator = getServerLocator().setFailoverOnInitialConnection(true);
SimpleString inQueue = new SimpleString("inQueue");
SimpleString outQueue = new SimpleString("outQueue");
createSessionFactory();
createSessionFactory2();
// closeable will take care of closing it
try (ClientSession session = sf.createSession(false, true, true);
ClientProducer sendInitialProducer = session.createProducer()) {
session.createQueue(inQueue, inQueue, null, true);
session.createQueue(outQueue, outQueue, null, true);
sendInitialProducer.send(inQueue, createMessage(session, 0, true));
}
ClientSession xaSessionRec = addClientSession(sf.createSession(true, false, false));
ClientConsumer consumer = addClientConsumer(xaSessionRec.createConsumer(inQueue));
byte[] globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
Xid xidRec = new XidImpl("xa2".getBytes(), 1, globalTransactionId);
xaSessionRec.start();
xaSessionRec.getXAResource().start(xidRec, XAResource.TMNOFLAGS);
// failover is now occurring, receive, ack and end will be called whilst this is happening.
ClientMessageImpl m = (ClientMessageImpl) consumer.receive(5000);
assertNotNull(m);
System.out.println("********************" + m.getIntProperty("counter"));
// the mdb would ack the message before calling onMessage()
m.acknowledge();
try {
// this may fail but thats ok, it depends on the race and when failover actually happens
xaSessionRec.end(xidRec, XAResource.TMSUCCESS);
} catch (XAException ignore) {
}
// we always reset the client on the RA
((ClientSessionInternal) xaSessionRec).resetIfNeeded();
// closeable will take care of closing it
try (ClientSession session = sf.createSession(false, true, true);
ClientProducer sendInitialProducer = session.createProducer()) {
sendInitialProducer.send(inQueue, createMessage(session, 0, true));
}
// now receive and send a message successfully
globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
xidRec = new XidImpl("xa4".getBytes(), 1, globalTransactionId);
xaSessionRec.getXAResource().start(xidRec, XAResource.TMNOFLAGS);
Binding binding = backupServer.getServer().getPostOffice().getBinding(inQueue);
Queue inQ = (Queue) binding.getBindable();
m = (ClientMessageImpl) consumer.receive(5000);
assertNotNull(m);
// the mdb would ack the message before calling onMessage()
m.acknowledge();
System.out.println("********************" + m.getIntProperty("counter"));
xaSessionRec.getXAResource().end(xidRec, XAResource.TMSUCCESS);
xaSessionRec.getXAResource().prepare(xidRec);
xaSessionRec.getXAResource().commit(xidRec, false);
// let's close the consumer so anything pending is handled
consumer.close();
assertEquals(1, getMessageCount(inQ));
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class BMFailoverTest method testFailoverOnReceiveCommit.
@Test
@BMRules(rules = { @BMRule(name = "trace clientsessionimpl commit", targetClass = "org.apache.activemq.artemis.core.client.impl.ClientSessionImpl", targetMethod = "commit", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.BMFailoverTest.serverToStop.getServer().stop(true)") })
public void testFailoverOnReceiveCommit() throws Exception {
serverToStop = liveServer;
locator = getServerLocator().setFailoverOnInitialConnection(true);
createSessionFactory();
ClientSession session = createSessionAndQueue();
ClientSession sendSession = createSession(sf, true, true);
ClientProducer producer = addClientProducer(sendSession.createProducer(FailoverTestBase.ADDRESS));
sendMessages(sendSession, producer, 10);
ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
session.start();
for (int i = 0; i < 10; i++) {
ClientMessage m = consumer.receive(500);
assertNotNull(m);
m.acknowledge();
}
try {
session.commit();
fail("should have thrown an exception");
} catch (ActiveMQTransactionOutcomeUnknownException e) {
// pass
} catch (ActiveMQTransactionRolledBackException e1) {
// pass
}
Queue bindable = (Queue) backupServer.getServer().getPostOffice().getBinding(FailoverTestBase.ADDRESS).getBindable();
assertEquals(10, getMessageCount(bindable));
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class DivertTest method testSingleDivertWithExpiry.
@Test
public void testSingleDivertWithExpiry() throws Exception {
final String testAddress = "testAddress";
final String forwardAddress = "forwardAddress";
final String expiryAddress = "expiryAddress";
AddressSettings expirySettings = new AddressSettings().setExpiryAddress(new SimpleString(expiryAddress));
DivertConfiguration divertConf = new DivertConfiguration().setName("divert1").setRoutingName("divert1").setAddress(testAddress).setForwardingAddress(forwardAddress);
Configuration config = createDefaultInVMConfig().addDivertConfiguration(divertConf).clearAddressesSettings().addAddressesSetting("#", expirySettings);
ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, true));
server.start();
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = sf.createSession(false, false, false);
final SimpleString queueName1 = new SimpleString("queue1");
final SimpleString queueName2 = new SimpleString("queue2");
session.createQueue(new SimpleString(forwardAddress), RoutingType.MULTICAST, queueName1, null, true);
session.createQueue(new SimpleString(testAddress), RoutingType.MULTICAST, queueName2, null, true);
session.createQueue(new SimpleString(expiryAddress), RoutingType.MULTICAST, new SimpleString(expiryAddress), null, true);
session.start();
ClientProducer producer = session.createProducer(new SimpleString(testAddress));
ClientConsumer consumer1 = session.createConsumer(queueName1);
ClientConsumer consumer2 = session.createConsumer(queueName2);
final int numMessages = 1;
final SimpleString propKey = new SimpleString("testkey");
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session.createMessage(true);
message.putIntProperty(propKey, i);
message.setExpiration(System.currentTimeMillis() + 1000);
producer.send(message);
}
session.commit();
// this context is validating if these messages are routed correctly
{
int count1 = 0;
ClientMessage message = null;
while ((message = consumer1.receiveImmediate()) != null) {
message.acknowledge();
count1++;
}
int count2 = 0;
while ((message = consumer2.receiveImmediate()) != null) {
message.acknowledge();
count2++;
}
assertEquals(1, count1);
assertEquals(1, count2);
session.rollback();
}
Thread.sleep(2000);
// it must been expired by now
assertNull(consumer1.receiveImmediate());
// it must been expired by now
assertNull(consumer2.receiveImmediate());
int countOriginal1 = 0;
int countOriginal2 = 0;
ClientConsumer consumerExpiry = session.createConsumer(expiryAddress);
for (int i = 0; i < numMessages * 2; i++) {
ClientMessage message = consumerExpiry.receive(5000);
System.out.println("Received message " + message);
assertNotNull(message);
if (message.getStringProperty(Message.HDR_ORIGINAL_QUEUE).equals("divert1")) {
countOriginal1++;
} else if (message.getStringProperty(Message.HDR_ORIGINAL_QUEUE).equals("queue2")) {
countOriginal2++;
} else {
System.out.println("message not part of any expired queue" + message);
}
}
assertEquals(numMessages, countOriginal1);
assertEquals(numMessages, countOriginal2);
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class DivertTest method testMultipleNonExclusiveDivert.
@Test
public void testMultipleNonExclusiveDivert() throws Exception {
final String testAddress = "testAddress";
final String forwardAddress1 = "forwardAddress1";
final String forwardAddress2 = "forwardAddress2";
final String forwardAddress3 = "forwardAddress3";
DivertConfiguration divertConf1 = new DivertConfiguration().setName("divert1").setRoutingName("divert1").setAddress(testAddress).setForwardingAddress(forwardAddress1);
DivertConfiguration divertConf2 = new DivertConfiguration().setName("divert2").setRoutingName("divert2").setAddress(testAddress).setForwardingAddress(forwardAddress2);
DivertConfiguration divertConf3 = new DivertConfiguration().setName("divert3").setRoutingName("divert3").setAddress(testAddress).setForwardingAddress(forwardAddress3);
Configuration config = createDefaultInVMConfig().addDivertConfiguration(divertConf1).addDivertConfiguration(divertConf2).addDivertConfiguration(divertConf3);
ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, false));
server.start();
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = sf.createSession(false, true, true);
final SimpleString queueName1 = new SimpleString("queue1");
final SimpleString queueName2 = new SimpleString("queue2");
final SimpleString queueName3 = new SimpleString("queue3");
final SimpleString queueName4 = new SimpleString("queue4");
session.createQueue(new SimpleString(forwardAddress1), RoutingType.MULTICAST, queueName1, null, false);
session.createQueue(new SimpleString(forwardAddress2), RoutingType.MULTICAST, queueName2, null, false);
session.createQueue(new SimpleString(forwardAddress3), RoutingType.MULTICAST, queueName3, null, false);
session.createQueue(new SimpleString(testAddress), RoutingType.MULTICAST, queueName4, null, false);
session.start();
ClientProducer producer = session.createProducer(new SimpleString(testAddress));
ClientConsumer consumer1 = session.createConsumer(queueName1);
ClientConsumer consumer2 = session.createConsumer(queueName2);
ClientConsumer consumer3 = session.createConsumer(queueName3);
ClientConsumer consumer4 = session.createConsumer(queueName4);
final int numMessages = 10;
final SimpleString propKey = new SimpleString("testkey");
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session.createMessage(false);
message.putIntProperty(propKey, i);
producer.send(message);
}
for (int i = 0; i < numMessages; i++) {
ClientMessage message = consumer1.receive(DivertTest.TIMEOUT);
Assert.assertNotNull(message);
Assert.assertEquals(i, message.getIntProperty(propKey).intValue());
message.acknowledge();
}
Assert.assertNull(consumer1.receiveImmediate());
for (int i = 0; i < numMessages; i++) {
ClientMessage message = consumer2.receive(DivertTest.TIMEOUT);
Assert.assertNotNull(message);
Assert.assertEquals(i, message.getIntProperty(propKey).intValue());
message.acknowledge();
}
Assert.assertNull(consumer2.receiveImmediate());
for (int i = 0; i < numMessages; i++) {
ClientMessage message = consumer3.receive(DivertTest.TIMEOUT);
Assert.assertNotNull(message);
Assert.assertEquals(i, message.getIntProperty(propKey).intValue());
message.acknowledge();
}
Assert.assertNull(consumer3.receiveImmediate());
for (int i = 0; i < numMessages; i++) {
ClientMessage message = consumer4.receive(DivertTest.TIMEOUT);
Assert.assertNotNull(message);
Assert.assertEquals(i, message.getIntProperty(propKey).intValue());
message.acknowledge();
}
Assert.assertNull(consumer4.receiveImmediate());
}
Aggregations