use of org.apache.activemq.command.ActiveMQDestination in project activemq-artemis by apache.
the class JMSConsumer2Test method testRedispatchOfRolledbackTx.
@Test
public void testRedispatchOfRolledbackTx() throws Exception {
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
sendMessages(connection, destination, 2);
MessageConsumer consumer = session.createConsumer(destination);
assertNotNull(consumer.receive(1000));
assertNotNull(consumer.receive(1000));
// install another consumer while message dispatch is unacked/uncommitted
Session redispatchSession = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageConsumer redispatchConsumer = redispatchSession.createConsumer(destination);
session.rollback();
session.close();
Message msg = redispatchConsumer.receive(1000);
assertNotNull(msg);
assertTrue(msg.getJMSRedelivered());
assertEquals(2, msg.getLongProperty("JMSXDeliveryCount"));
msg = redispatchConsumer.receive(1000);
assertNotNull(msg);
assertTrue(msg.getJMSRedelivered());
assertEquals(2, msg.getLongProperty("JMSXDeliveryCount"));
redispatchSession.commit();
assertNull(redispatchConsumer.receive(500));
redispatchSession.close();
}
use of org.apache.activemq.command.ActiveMQDestination in project activemq-artemis by apache.
the class JMSConsumer2Test method testMessageListenerWithConsumerCanBeStoppedConcurently.
@Test
public void testMessageListenerWithConsumerCanBeStoppedConcurently() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch closeDone = new CountDownLatch(1);
connection.start();
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
// preload the queue
sendMessages(session, destination, 2000);
final ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
final Map<Thread, Throwable> exceptions = Collections.synchronizedMap(new HashMap<Thread, Throwable>());
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
exceptions.put(t, e);
}
});
final class AckAndClose implements Runnable {
private final Message message;
AckAndClose(Message m) {
this.message = m;
}
@Override
public void run() {
try {
int count = counter.incrementAndGet();
if (count == 590) {
// close in a separate thread is ok by jms
consumer.close();
closeDone.countDown();
}
if (count % 200 == 0) {
// ensure there are some outstanding messages
// ack every 200
message.acknowledge();
}
} catch (Exception e) {
exceptions.put(Thread.currentThread(), e);
}
}
}
final ExecutorService executor = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory());
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message m) {
// ack and close eventually in separate thread
executor.execute(new AckAndClose(m));
}
});
assertTrue(closeDone.await(20, TimeUnit.SECONDS));
// await possible exceptions
Thread.sleep(1000);
assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
}
use of org.apache.activemq.command.ActiveMQDestination in project activemq-artemis by apache.
the class JMSConsumer4Test method testDurableConsumerSelectorChange.
@Test
public void testDurableConsumerSelectorChange() throws Exception {
// Receive a message with the JMS API
connection.setClientID("test");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
ActiveMQDestination destination = createDestination(session, destinationType);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(deliveryMode);
MessageConsumer consumer = session.createDurableSubscriber((Topic) destination, "test", "color='red'", false);
// Send the messages
TextMessage message = session.createTextMessage("1st");
message.setStringProperty("color", "red");
producer.send(message);
Message m = consumer.receive(1000);
assertNotNull(m);
assertEquals("1st", ((TextMessage) m).getText());
// Change the subscription.
consumer.close();
consumer = session.createDurableSubscriber((Topic) destination, "test", "color='blue'", false);
message = session.createTextMessage("2nd");
message.setStringProperty("color", "red");
producer.send(message);
message = session.createTextMessage("3rd");
message.setStringProperty("color", "blue");
producer.send(message);
// Selector should skip the 2nd message.
m = consumer.receive(1000);
assertNotNull(m);
assertEquals("3rd", ((TextMessage) m).getText());
assertNull(consumer.receiveNoWait());
}
use of org.apache.activemq.command.ActiveMQDestination in project activemq-artemis by apache.
the class JMSUsecase1Test method testSendReceiveTransacted.
@Test
public void testSendReceiveTransacted() throws Exception {
// Send a message to the broker.
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
ActiveMQDestination destination = createDestination(session, destinationType);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(this.deliveryMode);
MessageConsumer consumer = session.createConsumer(destination);
producer.send(session.createTextMessage("test"));
// Message should not be delivered until commit.
assertNull(consumer.receiveNoWait());
session.commit();
// Make sure only 1 message was delivered.
Message message = consumer.receive(1000);
assertNotNull(message);
assertFalse(message.getJMSRedelivered());
assertNull(consumer.receiveNoWait());
// Message should be redelivered is rollback is used.
session.rollback();
// Make sure only 1 message was delivered.
message = consumer.receive(2000);
assertNotNull(message);
assertTrue(message.getJMSRedelivered());
assertNull(consumer.receiveNoWait());
// If we commit now, the message should not be redelivered.
session.commit();
assertNull(consumer.receiveNoWait());
}
use of org.apache.activemq.command.ActiveMQDestination in project activemq-artemis by apache.
the class JMSUsecaseTest method testQueueBrowser.
@Test
public void testQueueBrowser() throws Exception {
// Send a message to the broker.
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
ActiveMQDestination destination = createDestination(session, destinationType);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(this.deliveryMode);
sendMessages(session, producer, 5);
producer.close();
QueueBrowser browser = session.createBrowser((Queue) destination);
Enumeration<?> enumeration = browser.getEnumeration();
for (int i = 0; i < 5; i++) {
Thread.sleep(100);
assertTrue(enumeration.hasMoreElements());
Message m = (Message) enumeration.nextElement();
assertNotNull(m);
assertEquals("" + i, ((TextMessage) m).getText());
}
assertFalse(enumeration.hasMoreElements());
}
Aggregations