use of org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException in project activemq-artemis by apache.
the class FailoverTest method testTimeoutOnFailoverConsumeBlocked.
@Test(timeout = 120000)
public void testTimeoutOnFailoverConsumeBlocked() throws Exception {
locator.setCallTimeout(5000).setBlockOnNonDurableSend(true).setConsumerWindowSize(0).setBlockOnDurableSend(true).setAckBatchSize(0).setBlockOnAcknowledge(true).setReconnectAttempts(-1).setAckBatchSize(0);
if (nodeManager instanceof InVMNodeManager) {
((InVMNodeManager) nodeManager).failoverPause = 5000L;
}
ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
final ClientSession session = createSession(sf1, true, true);
session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
for (int i = 0; i < 500; i++) {
ClientMessage message = session.createMessage(true);
message.putIntProperty("counter", i);
message.putBooleanProperty("end", i == 499);
producer.send(message);
}
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch endLatch = new CountDownLatch(1);
final ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
session.start();
final Map<Integer, ClientMessage> received = new HashMap<>();
Thread t = new Thread() {
@Override
public void run() {
ClientMessage message = null;
try {
while ((message = getMessage()) != null) {
Integer counter = message.getIntProperty("counter");
received.put(counter, message);
try {
log.info("acking message = id = " + message.getMessageID() + ", counter = " + message.getIntProperty("counter"));
message.acknowledge();
} catch (ActiveMQException e) {
e.printStackTrace();
continue;
}
log.info("Acked counter = " + counter);
if (counter.equals(10)) {
latch.countDown();
}
if (received.size() == 500) {
endLatch.countDown();
}
if (message.getBooleanProperty("end")) {
break;
}
}
} catch (Exception e) {
Assert.fail("failing due to exception " + e);
}
}
private ClientMessage getMessage() {
while (true) {
try {
ClientMessage msg = consumer.receive(20000);
if (msg == null) {
log.info("Returning null message on consuming");
}
return msg;
} catch (ActiveMQObjectClosedException oce) {
throw new RuntimeException(oce);
} catch (ActiveMQException ignored) {
// retry
ignored.printStackTrace();
}
}
}
};
t.start();
latch.await(10, TimeUnit.SECONDS);
log.info("crashing session");
crash(session);
endLatch.await(60, TimeUnit.SECONDS);
t.join();
Assert.assertTrue("received only " + received.size(), received.size() == 500);
session.close();
}
use of org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException in project activemq-artemis by apache.
the class KillSlowConsumerExample method main.
public static void main(final String[] args) throws Exception {
// Step 1. Create an initial context to perform the JNDI lookup.
InitialContext initialContext = new InitialContext();
// Step 2. Perform a lookup on the queue
Queue slowConsumerKillQueue = (Queue) initialContext.lookup("queue/slow.consumer.kill");
// Step 3. Perform a lookup on the Connection Factory
ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 4.Create a JMS Connection
try (Connection connection = connectionFactory.createConnection()) {
// Step 5. Create a JMS Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(slowConsumerKillQueue);
// Step 7. Create a Text Message
TextMessage message = session.createTextMessage("This is a text message");
System.out.println("Sending messages to queue ... ");
// Step 8. Send messages to the queue
for (int i = 0; i < 50; i++) {
producer.send(message);
}
// Step 9. Create a JMS Message Consumer
MessageConsumer messageConsumer = session.createConsumer(slowConsumerKillQueue);
// Step 10. Start the Connection
connection.start();
System.out.println("About to wait for " + WAIT_TIME + " seconds");
// Step 11. Wait for slow consumer to be detected
Thread.sleep(TimeUnit.SECONDS.toMillis(WAIT_TIME));
try {
// step 12. Try to us the connection - expect it to be closed already
messageConsumer.receive(TimeUnit.SECONDS.toMillis(1));
// messageConsumer.receive() should throw exception - we should not get to here.
throw new RuntimeException("SlowConsumerExample.slowConsumerKill() FAILED - expected " + "connection to be shutdown by Slow Consumer policy");
} catch (JMSException ex) {
if (ex.getCause() instanceof ActiveMQObjectClosedException) {
// received exception - as expected
System.out.println("SUCCESS! Received EXPECTED exception: " + ex);
} else {
throw new RuntimeException("SlowConsumerExample.slowConsumerKill() FAILED - expected " + "ActiveMQObjectClosedException BUT got " + ex.getCause());
}
}
}
}
use of org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException in project activemq-artemis by apache.
the class SlowConsumerTest method testSlowConsumerKilled.
@Test
public void testSlowConsumerKilled() throws Exception {
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = addClientSession(sf.createSession(false, true, true, false));
ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
assertPaging();
final int numMessages = 25;
for (int i = 0; i < numMessages; i++) {
producer.send(createTextMessage(session, "m" + i));
}
ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
session.start();
Thread.sleep(3000);
try {
consumer.receiveImmediate();
fail();
} catch (ActiveMQObjectClosedException e) {
assertEquals(e.getType(), ActiveMQExceptionType.OBJECT_CLOSED);
}
}
Aggregations