use of javax.jms.MessageListener in project activemq-artemis by apache.
the class QueueRepeaterTest method testTransaction.
public void testTransaction() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
connection = factory.createConnection();
queue = new ActiveMQQueue(getClass().getName() + "." + getName());
producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumerSession = connection.createSession(true, 0);
producer = producerSession.createProducer(queue);
consumer = consumerSession.createConsumer(queue);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
try {
TextMessage tm = (TextMessage) m;
receivedText = tm.getText();
latch.countDown();
LOG.info("consumer received message :" + receivedText);
consumerSession.commit();
LOG.info("committed transaction");
} catch (JMSException e) {
try {
consumerSession.rollback();
LOG.info("rolled back transaction");
} catch (JMSException e1) {
LOG.info(e1.toString());
e1.printStackTrace();
}
LOG.info(e.toString());
e.printStackTrace();
}
}
});
connection.start();
TextMessage tm = null;
try {
tm = producerSession.createTextMessage();
tm.setText("Hello, " + new Date());
producer.send(tm);
LOG.info("producer sent message :" + tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
LOG.info("Waiting for latch");
latch.await(2, TimeUnit.SECONDS);
assertNotNull(receivedText);
LOG.info("test completed, destination=" + receivedText);
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class NonBlockingConsumerRedeliveryTest method testMessageDeleiveredWhenNonBlockingEnabled.
@Test
public void testMessageDeleiveredWhenNonBlockingEnabled() throws Exception {
final LinkedHashSet<Message> received = new LinkedHashSet<>();
final LinkedHashSet<Message> beforeRollback = new LinkedHashSet<>();
final LinkedHashSet<Message> afterRollback = new LinkedHashSet<>();
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.add(message);
}
});
sendMessages();
session.commit();
connection.start();
assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages.");
return received.size() == MSG_COUNT;
}
}));
beforeRollback.addAll(received);
received.clear();
session.rollback();
assertTrue("Post-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages since rollback.");
return received.size() == MSG_COUNT;
}
}));
afterRollback.addAll(received);
received.clear();
assertEquals(beforeRollback.size(), afterRollback.size());
assertEquals(beforeRollback, afterRollback);
session.commit();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class NonBlockingConsumerRedeliveryTest method testNonBlockingMessageDeleiveryIsDelayed.
@Test
public void testNonBlockingMessageDeleiveryIsDelayed() throws Exception {
final LinkedHashSet<Message> received = new LinkedHashSet<>();
ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
connection.getRedeliveryPolicy().setInitialRedeliveryDelay(TimeUnit.SECONDS.toMillis(6));
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
received.add(message);
}
});
sendMessages();
connection.start();
assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
LOG.info("Consumer has received " + received.size() + " messages.");
return received.size() == MSG_COUNT;
}
}));
received.clear();
session.rollback();
assertFalse("Delayed redelivery test not expecting any messages yet.", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
return received.size() > 0;
}
}, TimeUnit.SECONDS.toMillis(4)));
session.commit();
session.close();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class ConnectorXBeanConfigTest method testBrokerWontStop.
public void testBrokerWontStop() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?async=false");
factory.setDispatchAsync(false);
factory.setAlwaysSessionAsync(false);
Connection conn = factory.createConnection();
final Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
conn.start();
final Destination dest = new ActiveMQQueue("TEST");
final CountDownLatch stop = new CountDownLatch(1);
final CountDownLatch sendSecond = new CountDownLatch(1);
final CountDownLatch shutdown = new CountDownLatch(1);
final CountDownLatch test = new CountDownLatch(1);
ActiveMQConnectionFactory testFactory = new ActiveMQConnectionFactory("vm://localhost?async=false");
Connection testConn = testFactory.createConnection();
testConn.start();
Destination testDestination = sess.createQueue("NEW");
Session testSess = testConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer testProducer = testSess.createProducer(testDestination);
final Thread consumerThread = new Thread() {
@Override
public void run() {
try {
MessageProducer producer = sess.createProducer(dest);
producer.send(sess.createTextMessage("msg1"));
MessageConsumer consumer = sess.createConsumer(dest);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
// send a message that will block
Thread.sleep(2000);
sendSecond.countDown();
// try to stop the broker
Thread.sleep(5000);
stop.countDown();
// run the test
Thread.sleep(5000);
test.countDown();
shutdown.await();
} catch (InterruptedException ie) {
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};
consumerThread.start();
final Thread producerThread = new Thread() {
@Override
public void run() {
try {
sendSecond.await();
MessageProducer producer = sess.createProducer(dest);
producer.send(sess.createTextMessage("msg2"));
} catch (Exception e) {
e.printStackTrace();
}
}
};
producerThread.start();
final Thread stopThread = new Thread() {
@Override
public void run() {
try {
stop.await();
brokerService.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
};
stopThread.start();
test.await();
try {
testSess.createConsumer(testDestination);
fail("Should have failed creating a consumer!");
} catch (Exception e) {
e.printStackTrace();
}
try {
testProducer.send(testSess.createTextMessage("msg3"));
fail("Should have failed sending a message!");
} catch (Exception e) {
e.printStackTrace();
}
shutdown.countDown();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JmsConsumerTest method testIndividualACKMessageConsumer.
@Test
public void testIndividualACKMessageConsumer() throws Exception {
Connection conn = cf.createConnection();
Session session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
MessageProducer producer = session.createProducer(jBossQueue);
MessageConsumer consumer = session.createConsumer(jBossQueue);
int noOfMessages = 100;
for (int i = 0; i < noOfMessages; i++) {
producer.setPriority(2);
producer.send(session.createTextMessage("m" + i));
}
conn.start();
final AtomicInteger errors = new AtomicInteger(0);
final ReusableLatch latch = new ReusableLatch();
latch.setCount(noOfMessages);
class MessageAckEven implements MessageListener {
int count = 0;
@Override
public void onMessage(Message msg) {
try {
TextMessage txtmsg = (TextMessage) msg;
if (!txtmsg.getText().equals("m" + count)) {
errors.incrementAndGet();
}
if (count % 2 == 0) {
msg.acknowledge();
}
count++;
} catch (Exception e) {
errors.incrementAndGet();
} finally {
latch.countDown();
}
}
}
consumer.setMessageListener(new MessageAckEven());
Assert.assertTrue(latch.await(5000));
session.close();
session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
consumer = session.createConsumer(jBossQueue);
// Consume odd numbers first
for (int i = 0; i < noOfMessages; i++) {
if (i % 2 == 0) {
continue;
}
TextMessage m = (TextMessage) consumer.receive(1000);
Assert.assertNotNull(m);
m.acknowledge();
Assert.assertEquals("m" + i, m.getText());
}
SimpleString queueName = new SimpleString(JmsConsumerTest.Q_NAME);
conn.close();
Queue queue = server.locateQueue(queueName);
Wait.assertEquals(0, queue::getDeliveringCount);
Wait.assertEquals(0, queue::getMessageCount);
}
Aggregations