use of javax.jms.MessageListener in project activemq-artemis by apache.
the class FailoverConsumerOutstandingCommitTest method doTestFailoverConsumerOutstandingSendTx.
public void doTestFailoverConsumerOutstandingSendTx(final boolean doActualBrokerCommit) throws Exception {
final boolean watchTopicAdvisories = true;
server = createBroker();
server.start();
brokerStopLatch = new CountDownLatch(1);
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")");
cf.setWatchTopicAdvisories(watchTopicAdvisories);
cf.setDispatchAsync(false);
final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
connection.start();
final Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Queue destination = producerSession.createQueue(QUEUE_NAME + "?consumer.prefetchSize=" + prefetch);
final Queue signalDestination = producerSession.createQueue(QUEUE_NAME + ".signal" + "?consumer.prefetchSize=" + prefetch);
final Session consumerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
final CountDownLatch commitDoneLatch = new CountDownLatch(1);
final CountDownLatch messagesReceived = new CountDownLatch(3);
final AtomicBoolean gotCommitException = new AtomicBoolean(false);
final ArrayList<TextMessage> receivedMessages = new ArrayList<>();
final MessageConsumer testConsumer = consumerSession.createConsumer(destination);
doByteman.set(true);
testConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
LOG.info("consume one: " + message);
assertNotNull("got message", message);
receivedMessages.add((TextMessage) message);
try {
LOG.info("send one");
produceMessage(consumerSession, signalDestination, 1);
LOG.info("commit session");
consumerSession.commit();
} catch (JMSException e) {
LOG.info("commit exception", e);
gotCommitException.set(true);
}
commitDoneLatch.countDown();
messagesReceived.countDown();
LOG.info("done commit");
}
});
// may block if broker shutdown happens quickly
new Thread() {
@Override
public void run() {
LOG.info("producer started");
try {
produceMessage(producerSession, destination, prefetch * 2);
} catch (javax.jms.IllegalStateException SessionClosedExpectedOnShutdown) {
} catch (JMSException e) {
e.printStackTrace();
fail("unexpceted ex on producer: " + e);
}
LOG.info("producer done");
}
}.start();
// will be stopped by the plugin
brokerStopLatch.await();
doByteman.set(false);
server.stop();
server = createBroker();
server.start();
assertTrue("commit done through failover", commitDoneLatch.await(20, TimeUnit.SECONDS));
assertTrue("commit failed", gotCommitException.get());
assertTrue("another message was received after failover", messagesReceived.await(20, TimeUnit.SECONDS));
int receivedIndex = 0;
assertEquals("get message 0 first", MESSAGE_TEXT + "0", receivedMessages.get(receivedIndex++).getText());
if (!doActualBrokerCommit) {
// it will be redelivered and not tracked as a duplicate
assertEquals("get message 0 second", MESSAGE_TEXT + "0", receivedMessages.get(receivedIndex++).getText());
}
assertTrue("another message was received", messagesReceived.await(20, TimeUnit.SECONDS));
assertEquals("get message 1 eventually", MESSAGE_TEXT + "1", receivedMessages.get(receivedIndex++).getText());
connection.close();
server.stop();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class StompTest method testSendOverDiskFull.
@Test
public void testSendOverDiskFull() throws Exception {
AssertionLoggerHandler.startCapture();
try {
MessageConsumer consumer = session.createConsumer(queue);
conn.connect(defUser, defPass);
int count = 1000;
final CountDownLatch latch = new CountDownLatch(count);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message arg0) {
latch.countDown();
}
});
((ActiveMQServerImpl) server.getActiveMQServer()).getMonitor().setMaxUsage(0).tick();
// Connection should be closed by broker when disk is full and attempt to send
Exception e = null;
try {
for (int i = 1; i <= count; i++) {
send(conn, getQueuePrefix() + getQueueName(), null, "Hello World!");
}
} catch (Exception se) {
e = se;
}
assertNotNull(e);
// It should encounter the exception on logs
AssertionLoggerHandler.findText("AMQ119119");
} finally {
AssertionLoggerHandler.clear();
AssertionLoggerHandler.stopCapture();
}
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class DestinationGCTest method testDestinationGCWithActiveConsumers.
public void testDestinationGCWithActiveConsumers() throws Exception {
assertEquals(1, broker.getAdminView().getQueues().length);
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?create=false");
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
session.createProducer(otherQueue).close();
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
}
});
connection.start();
TimeUnit.SECONDS.sleep(5);
assertTrue("After GC runs there should be one Queue.", Wait.waitFor(new Condition() {
@Override
public boolean isSatisified() throws Exception {
return broker.getAdminView().getQueues().length == 1;
}
}));
connection.close();
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JobSchedulerTxTest method testTxSendWithRollback.
@Test
public void testTxSendWithRollback() throws Exception {
final int COUNT = 10;
Connection connection = createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
final CountDownLatch latch = new CountDownLatch(COUNT);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
latch.countDown();
}
});
connection.start();
long time = 5000;
Session producerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer producer = producerSession.createProducer(destination);
for (int i = 0; i < COUNT; ++i) {
TextMessage message = session.createTextMessage("test msg");
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
producer.send(message);
}
producer.close();
producerSession.rollback();
// make sure the message isn't delivered early
Thread.sleep(2000);
assertEquals(COUNT, latch.getCount());
latch.await(5, TimeUnit.SECONDS);
assertEquals(COUNT, latch.getCount());
}
use of javax.jms.MessageListener in project activemq-artemis by apache.
the class JobSchedulerTxTest method testTxSendWithCommit.
@Test
public void testTxSendWithCommit() throws Exception {
final int COUNT = 10;
Connection connection = createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
final CountDownLatch latch = new CountDownLatch(COUNT);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
latch.countDown();
}
});
connection.start();
long time = 5000;
Session producerSession = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer producer = producerSession.createProducer(destination);
for (int i = 0; i < COUNT; ++i) {
TextMessage message = session.createTextMessage("test msg");
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
producer.send(message);
}
producer.close();
producerSession.commit();
// make sure the message isn't delivered early
Thread.sleep(2000);
assertEquals(COUNT, latch.getCount());
latch.await(5, TimeUnit.SECONDS);
assertEquals(0, latch.getCount());
}
Aggregations