use of javax.jms.MessageListener in project activemq-artemis by apache.
the class NetworkBrokerDetachTest method testNetworkedBrokerDurableSubAfterRestart.
@Test
public void testNetworkedBrokerDurableSubAfterRestart() throws Exception {
final AtomicInteger count = new AtomicInteger(0);
MessageListener counter = new MessageListener() {
@Override
public void onMessage(Message message) {
count.incrementAndGet();
}
};
LOG.info("Creating durable consumer on each broker ...");
ActiveMQTopic destination = registerDurableConsumer(networkedBroker, counter);
registerDurableConsumer(broker, counter);
assertTrue("got expected consumer count from local broker mbean within time limit", verifyConsumerCount(2, destination, broker));
assertTrue("got expected consumer count from network broker mbean within time limit", verifyConsumerCount(2, destination, networkedBroker));
sendMessageTo(destination, broker);
assertTrue("Got one message on each", verifyMessageCount(2, count));
LOG.info("Stopping brokerTwo...");
networkedBroker.stop();
networkedBroker.waitUntilStopped();
LOG.info("restarting broker Two...");
networkedBroker = createNetworkedBroker();
networkedBroker.start();
LOG.info("Recreating durable Consumer on the broker after restart...");
registerDurableConsumer(networkedBroker, counter);
// give advisories a chance to percolate
TimeUnit.SECONDS.sleep(5);
sendMessageTo(destination, broker);
// expect similar after restart
assertTrue("got expected consumer count from local broker mbean within time limit", verifyConsumerCount(2, destination, broker));
// a durable sub is auto bridged on restart unless dynamicOnly=true
assertTrue("got expected consumer count from network broker mbean within time limit", verifyConsumerCount(2, destination, networkedBroker));
assertTrue("got no inactive subs on broker", verifyDurableConsumerCount(0, broker));
assertTrue("got no inactive subs on other broker", verifyDurableConsumerCount(0, networkedBroker));
assertTrue("Got two more messages after restart", verifyMessageCount(4, count));
TimeUnit.SECONDS.sleep(1);
assertTrue("still Got just two more messages", verifyMessageCount(4, count));
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class NetworkFailoverTest method testRequestReply.
public void testRequestReply() throws Exception {
final MessageProducer remoteProducer = remoteSession.createProducer(null);
MessageConsumer remoteConsumer = remoteSession.createConsumer(included);
remoteConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message msg) {
final TextMessage textMsg = (TextMessage) msg;
try {
String payload = "REPLY: " + textMsg.getText() + ", " + textMsg.getJMSMessageID();
Destination replyTo;
replyTo = msg.getJMSReplyTo();
textMsg.clearBody();
textMsg.setText(payload);
LOG.info("*** Sending response: {}", textMsg.getText());
remoteProducer.send(replyTo, textMsg);
LOG.info("replied with: " + textMsg.getJMSMessageID());
} catch (DestinationDoesNotExistException expected) {
// been removed but not yet recreated
replyToNonExistDest.incrementAndGet();
try {
LOG.info("NED: " + textMsg.getJMSMessageID());
} catch (JMSException e) {
e.printStackTrace();
}
} catch (Exception e) {
LOG.warn("*** Responder listener caught exception: ", e);
e.printStackTrace();
}
}
});
Queue tempQueue = localSession.createTemporaryQueue();
MessageProducer requestProducer = localSession.createProducer(included);
requestProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
MessageConsumer requestConsumer = localSession.createConsumer(tempQueue);
// track remote dlq for forward failures
MessageConsumer dlqconsumer = remoteSession.createConsumer(new ActiveMQQueue(SharedDeadLetterStrategy.DEFAULT_DEAD_LETTER_QUEUE_NAME));
dlqconsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
LOG.info("dlq " + message.getJMSMessageID());
} catch (JMSException e) {
e.printStackTrace();
}
remoteDLQCount.incrementAndGet();
}
});
// allow for consumer infos to perculate around
Thread.sleep(2000);
long done = System.currentTimeMillis() + (MESSAGE_COUNT * 6000);
int i = 0;
while (MESSAGE_COUNT > roundTripComplete.get() + remoteDLQCount.get() + replyToNonExistDest.get() && done > System.currentTimeMillis()) {
if (i < MESSAGE_COUNT) {
String payload = "test msg " + i;
i++;
TextMessage msg = localSession.createTextMessage(payload);
msg.setJMSReplyTo(tempQueue);
requestProducer.send(msg);
LOG.info("Sent: " + msg.getJMSMessageID() + ", Failing over");
((FailoverTransport) ((TransportFilter) ((TransportFilter) ((ActiveMQConnection) localConnection).getTransport()).getNext()).getNext()).handleTransportFailure(new IOException("Forcing failover from test"));
}
TextMessage result = (TextMessage) requestConsumer.receive(5000);
if (result != null) {
LOG.info("Got reply: " + result.getJMSMessageID() + ", " + result.getText());
roundTripComplete.incrementAndGet();
}
}
LOG.info("complete: " + roundTripComplete.get() + ", remoteDLQCount: " + remoteDLQCount.get() + ", replyToNonExistDest: " + replyToNonExistDest.get());
assertEquals("complete:" + roundTripComplete.get() + ", remoteDLQCount: " + remoteDLQCount.get() + ", replyToNonExistDest: " + replyToNonExistDest.get(), MESSAGE_COUNT, roundTripComplete.get() + remoteDLQCount.get() + replyToNonExistDest.get());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class ZeroPrefetchConsumerTest method testCannotUseMessageListener.
public void testCannotUseMessageListener() throws Exception {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
MessageListener listener = new SpringConsumer();
try {
consumer.setMessageListener(listener);
fail("Should have thrown JMSException as we cannot use MessageListener with zero prefetch");
} catch (JMSException e) {
LOG.info("Received expected exception : " + e);
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class RedeliveryPolicyTest method testRepeatedRedeliveryOnMessageNoCommit.
public void testRepeatedRedeliveryOnMessageNoCommit() throws Exception {
connection.start();
Session dlqSession = connection.createSession(true, Session.SESSION_TRANSACTED);
ActiveMQQueue destination = new ActiveMQQueue("TEST");
MessageProducer producer = dlqSession.createProducer(destination);
// Send the messages
producer.send(dlqSession.createTextMessage("1st"));
dlqSession.commit();
MessageConsumer dlqConsumer = dlqSession.createConsumer(new ActiveMQQueue("ActiveMQ.DLQ"));
final int maxRedeliveries = 4;
final AtomicInteger receivedCount = new AtomicInteger(0);
for (int i = 0; i <= maxRedeliveries + 1; i++) {
connection = (ActiveMQConnection) factory.createConnection(userName, password);
connections.add(connection);
RedeliveryPolicy policy = connection.getRedeliveryPolicy();
policy.setInitialRedeliveryDelay(0);
policy.setUseExponentialBackOff(false);
policy.setMaximumRedeliveries(maxRedeliveries);
connection.start();
final Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageConsumer consumer = session.createConsumer(destination);
final CountDownLatch done = new CountDownLatch(1);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
ActiveMQTextMessage m = (ActiveMQTextMessage) message;
assertEquals("1st", m.getText());
assertEquals(receivedCount.get(), m.getRedeliveryCounter());
receivedCount.incrementAndGet();
done.countDown();
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
});
if (i <= maxRedeliveries) {
assertTrue("listener done", done.await(5, TimeUnit.SECONDS));
} else {
// final redlivery gets poisoned before dispatch
assertFalse("listener done", done.await(1, TimeUnit.SECONDS));
}
connection.close();
connections.remove(connection);
}
// We should be able to get the message off the DLQ now.
TextMessage m = (TextMessage) dlqConsumer.receive(1000);
assertNotNull("Got message from DLQ", m);
assertEquals("1st", m.getText());
String cause = m.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
assertTrue("cause exception has policy ref", cause.contains("RedeliveryPolicy"));
dlqSession.commit();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class RedeliveryPolicyTest method testRepeatedRedeliveryServerSessionNoCommit.
public void testRepeatedRedeliveryServerSessionNoCommit() throws Exception {
connection.start();
Session dlqSession = connection.createSession(true, Session.SESSION_TRANSACTED);
ActiveMQQueue destination = new ActiveMQQueue("TEST");
MessageProducer producer = dlqSession.createProducer(destination);
// Send the messages
producer.send(dlqSession.createTextMessage("1st"));
dlqSession.commit();
MessageConsumer dlqConsumer = dlqSession.createConsumer(new ActiveMQQueue("ActiveMQ.DLQ"));
final int maxRedeliveries = 4;
final AtomicInteger receivedCount = new AtomicInteger(0);
for (int i = 0; i <= maxRedeliveries + 1; i++) {
connection = (ActiveMQConnection) factory.createConnection(userName, password);
connections.add(connection);
RedeliveryPolicy policy = connection.getRedeliveryPolicy();
policy.setInitialRedeliveryDelay(0);
policy.setUseExponentialBackOff(false);
policy.setMaximumRedeliveries(maxRedeliveries);
connection.start();
final CountDownLatch done = new CountDownLatch(1);
final ActiveMQSession session = (ActiveMQSession) connection.createSession(true, Session.SESSION_TRANSACTED);
session.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
ActiveMQTextMessage m = (ActiveMQTextMessage) message;
assertEquals("1st", m.getText());
assertEquals(receivedCount.get(), m.getRedeliveryCounter());
receivedCount.incrementAndGet();
done.countDown();
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
});
connection.createConnectionConsumer(destination, null, new ServerSessionPool() {
@Override
public ServerSession getServerSession() throws JMSException {
return new ServerSession() {
@Override
public Session getSession() throws JMSException {
return session;
}
@Override
public void start() throws JMSException {
}
};
}
}, 100, false);
Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
session.run();
return done.await(10, TimeUnit.MILLISECONDS);
}
});
if (i <= maxRedeliveries) {
assertTrue("listener done @" + i, done.await(5, TimeUnit.SECONDS));
} else {
// final redlivery gets poisoned before dispatch
assertFalse("listener not done @" + i, done.await(1, TimeUnit.SECONDS));
}
connection.close();
connections.remove(connection);
}
// We should be able to get the message off the DLQ now.
TextMessage m = (TextMessage) dlqConsumer.receive(1000);
assertNotNull("Got message from DLQ", m);
assertEquals("1st", m.getText());
String cause = m.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
assertTrue("cause exception has policy ref", cause.contains("RedeliveryPolicy"));
dlqSession.commit();
}
Aggregations