use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSBridgeImplTest method testSendMessagesWhenMaxBatchTimeExpires.
@Test
public void testSendMessagesWhenMaxBatchTimeExpires() throws Exception {
int maxBatchSize = 2;
long maxBatchTime = 500;
ConnectionFactoryFactory sourceCFF = JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
ConnectionFactoryFactory targetCFF = JMSBridgeImplTest.newConnectionFactoryFactory(JMSBridgeImplTest.createConnectionFactory());
DestinationFactory sourceDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.SOURCE));
DestinationFactory targetDF = JMSBridgeImplTest.newDestinationFactory(ActiveMQJMSClient.createQueue(JMSBridgeImplTest.TARGET));
TransactionManager tm = JMSBridgeImplTest.newTransactionManager();
JMSBridgeImpl bridge = new JMSBridgeImpl();
Assert.assertNotNull(bridge);
bridge.setSourceConnectionFactoryFactory(sourceCFF);
bridge.setSourceDestinationFactory(sourceDF);
bridge.setTargetConnectionFactoryFactory(targetCFF);
bridge.setTargetDestinationFactory(targetDF);
bridge.setFailureRetryInterval(10);
bridge.setMaxRetries(-1);
bridge.setMaxBatchSize(maxBatchSize);
bridge.setMaxBatchTime(maxBatchTime);
bridge.setTransactionManager(tm);
bridge.setQualityOfServiceMode(QualityOfServiceMode.AT_MOST_ONCE);
Assert.assertFalse(bridge.isStarted());
bridge.start();
Assert.assertTrue(bridge.isStarted());
Connection targetConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
Session targetSess = targetConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = targetSess.createConsumer(targetDF.createDestination());
final List<Message> messages = new LinkedList<>();
MessageListener listener = new MessageListener() {
@Override
public void onMessage(final Message message) {
messages.add(message);
}
};
consumer.setMessageListener(listener);
targetConn.start();
Connection sourceConn = JMSBridgeImplTest.createConnectionFactory().createConnection();
Session sourceSess = sourceConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = sourceSess.createProducer(sourceDF.createDestination());
producer.send(sourceSess.createTextMessage());
sourceConn.close();
Assert.assertEquals(0, messages.size());
Thread.sleep(3 * maxBatchTime);
Assert.assertEquals(1, messages.size());
bridge.stop();
Assert.assertFalse(bridge.isStarted());
targetConn.close();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class SimpleNetworkTest method testRequestReply.
@Test(timeout = 60 * 1000)
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) {
try {
TextMessage textMsg = (TextMessage) msg;
String payload = "REPLY: " + textMsg.getText();
Destination replyTo;
replyTo = msg.getJMSReplyTo();
textMsg.clearBody();
textMsg.setText(payload);
remoteProducer.send(replyTo, textMsg);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
TopicRequestor requestor = new TopicRequestor((TopicSession) localSession, included);
// allow for consumer infos to perculate around
Thread.sleep(5000);
for (int i = 0; i < MESSAGE_COUNT; i++) {
TextMessage msg = localSession.createTextMessage("test msg: " + i);
TextMessage result = (TextMessage) requestor.request(msg);
assertNotNull(result);
LOG.info(result.getText());
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class MessageListenerRedeliveryTest method testQueueSessionListenerExceptionRetry.
@Test
public void testQueueSessionListenerExceptionRetry() throws Exception {
redeliverConnection.start();
Session session = redeliverConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
String qname = "queue-testQueueSessionListenerExceptionRetry";
Queue queue = session.createQueue(qname);
this.makeSureCoreQueueExist(qname);
MessageProducer producer = createProducer(session, queue);
Message message = createTextMessage(session, "1");
producer.send(message);
message = createTextMessage(session, "2");
producer.send(message);
MessageConsumer consumer = session.createConsumer(queue);
final CountDownLatch gotMessage = new CountDownLatch(2);
final AtomicInteger count = new AtomicInteger(0);
final int maxDeliveries = getRedeliveryPolicy().getMaximumRedeliveries();
final ArrayList<String> received = new ArrayList<>();
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
System.out.println("Message Received: " + message);
try {
received.add(((TextMessage) message).getText());
} catch (JMSException e) {
e.printStackTrace();
fail(e.toString());
}
if (count.incrementAndGet() < maxDeliveries) {
throw new RuntimeException(getName() + " force a redelivery");
}
// new blood
count.set(0);
gotMessage.countDown();
}
});
assertTrue("got message before retry expiry", gotMessage.await(20, TimeUnit.SECONDS));
for (int i = 0; i < maxDeliveries; i++) {
assertEquals("got first redelivered: " + i, "1", received.get(i));
}
for (int i = maxDeliveries; i < maxDeliveries * 2; i++) {
assertEquals("got first redelivered: " + i, "2", received.get(i));
}
session.close();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class MessageListenerRedeliveryTest method testQueueSessionListenerExceptionDlq.
@Test
public void testQueueSessionListenerExceptionDlq() throws Exception {
redeliverConnection.start();
Session session = redeliverConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
String qname = "queue-testQueueSessionListenerExceptionDlq";
Queue queue = session.createQueue(qname);
this.makeSureCoreQueueExist(qname);
MessageProducer producer = createProducer(session, queue);
Message message = createTextMessage(session);
producer.send(message);
final Message[] dlqMessage = new Message[1];
ActiveMQDestination dlqDestination = new ActiveMQQueue("ActiveMQ.DLQ");
this.makeSureCoreQueueExist("ActiveMQ.DLQ");
MessageConsumer dlqConsumer = session.createConsumer(dlqDestination);
final CountDownLatch gotDlqMessage = new CountDownLatch(1);
dlqConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
System.out.println("DLQ Message Received: " + message);
dlqMessage[0] = message;
gotDlqMessage.countDown();
}
});
MessageConsumer consumer = session.createConsumer(queue);
final int maxDeliveries = getRedeliveryPolicy().getMaximumRedeliveries();
System.out.println("max redlivery: " + maxDeliveries);
final CountDownLatch gotMessage = new CountDownLatch(maxDeliveries);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
System.out.println("Message Received: " + message);
gotMessage.countDown();
throw new RuntimeException(getName() + " force a redelivery");
}
});
assertTrue("got message before retry expiry", gotMessage.await(20, TimeUnit.SECONDS));
// check DLQ
assertTrue("got dlq message", gotDlqMessage.await(20, TimeUnit.SECONDS));
// check DLQ message cause is captured
message = dlqMessage[0];
assertNotNull("dlq message captured", message);
String cause = message.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
System.out.println("DLQ'd message cause reported as: " + cause);
assertTrue("cause 'cause' exception is remembered", cause.contains("RuntimeException"));
assertTrue("is correct exception", cause.contains(getName()));
assertTrue("cause exception is remembered", cause.contains("Throwable"));
assertTrue("cause policy is remembered", cause.contains("RedeliveryPolicy"));
session.close();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JMSConsumer1Test method testMessageListenerWithConsumerCanBeStopped.
@Test
public void testMessageListenerWithConsumerCanBeStopped() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch done1 = new CountDownLatch(1);
final CountDownLatch done2 = new CountDownLatch(1);
// Receive a message with the JMS API
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = createDestination(session, destinationType);
ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
counter.incrementAndGet();
if (counter.get() == 1) {
done1.countDown();
}
if (counter.get() == 2) {
done2.countDown();
}
}
});
// Send a first message to make sure that the consumer dispatcher is
// running
sendMessages(session, destination, 1);
assertTrue(done1.await(1, TimeUnit.SECONDS));
assertEquals(1, counter.get());
// Stop the consumer.
consumer.stop();
// Send a message, but should not get delivered.
sendMessages(session, destination, 1);
assertFalse(done2.await(1, TimeUnit.SECONDS));
assertEquals(1, counter.get());
// Start the consumer, and the message should now get delivered.
consumer.start();
assertTrue(done2.await(1, TimeUnit.SECONDS));
assertEquals(2, counter.get());
}
Aggregations