use of org.eclipse.scout.rt.mom.api.IMessageListener in project scout.rt by eclipse.
the class JmsMomImplementorTest method testQueuePublishSubscribeMultipleSubscriptions.
@Test
// regression
@Times(10)
public void testQueuePublishSubscribeMultipleSubscriptions() throws InterruptedException {
IDestination<String> queue = MOM.newDestination("test/mom/testQueuePublishSubscribeMultipleSubscriptions", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
final AtomicInteger msgCounter = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
// Subscribe for the destination
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
msgCounter.incrementAndGet();
latch.countDown();
}
}));
// Subscribe for the destination
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
msgCounter.incrementAndGet();
latch.countDown();
}
}));
// Publish a message
MOM.publish(JmsTestMom.class, queue, "hello world");
// Verify
latch.await(5, TimeUnit.SECONDS);
Thread.sleep(50);
assertEquals(1, msgCounter.get());
}
use of org.eclipse.scout.rt.mom.api.IMessageListener in project scout.rt by eclipse.
the class JmsMomImplementorTest method testSubscribeTransactional.
@Test
public void testSubscribeTransactional() throws InterruptedException {
IDestination<String> queue = MOM.newDestination("test/mom/testSubscribeTransactional", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queue, BEANS.get(ObjectMarshaller.class)));
MOM.publish(JmsTestMom.class, queue, "message-1", MOM.newPublishInput());
final AtomicInteger messageCounter = new AtomicInteger();
// 1. Receive message, but reject it (rollback)
final BlockingCountDownLatch message1Latch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch message2Latch = new BlockingCountDownLatch(1);
IMessageListener<String> listener = new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
switch(message.getTransferObject()) {
case "message-1":
switch(messageCounter.incrementAndGet()) {
case 1:
case 2:
case 3:
ITransaction.CURRENT.get().rollback();
break;
default:
message1Latch.countDown();
break;
}
return;
case "message-2":
message2Latch.countDown();
break;
default:
throw new IllegalArgumentException("illegal message");
}
}
};
// Register transactional subscriber
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, listener, MOM.newSubscribeInput().withAcknowledgementMode(SubscribeInput.ACKNOWLEDGE_TRANSACTED)));
assertTrue("message expected to be rejected 3 times", message1Latch.await());
// Publish a next message
MOM.publish(JmsTestMom.class, queue, "message-2", MOM.newPublishInput());
// Check that the message was received
assertTrue("message expected to be received", message2Latch.await());
// Wait some time to verify that 'message-1' is no longer received.
Thread.sleep(1000);
assertEquals(messageCounter.get(), 4);
}
use of org.eclipse.scout.rt.mom.api.IMessageListener in project scout.rt by eclipse.
the class JmsMomImplementorTest method testSerialMessageConsumption.
@Test
public void testSerialMessageConsumption() throws InterruptedException {
IDestination<Object> queue = MOM.newDestination("test/mom/testSerialMessageConsumption", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
// 1. Publish some messages
int msgCount = 10;
for (int i = 0; i < msgCount; i++) {
MOM.publish(JmsTestMom.class, queue, "hello");
}
// 2. Consume the messages
final BlockingCountDownLatch latch = new BlockingCountDownLatch(msgCount, 3, TimeUnit.SECONDS);
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<Object>() {
@Override
public void onMessage(IMessage<Object> message) {
try {
// timeout must be greater than the default latch timeout
latch.countDownAndBlock(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
}
}
}, MOM.newSubscribeInput().withAcknowledgementMode(SubscribeInput.ACKNOWLEDGE_AUTO_SINGLE_THREADED)));
try {
assertFalse("messages not expected to be consumed concurrently", latch.await());
assertEquals("one message expected to be consumed", msgCount - 1, latch.getCount());
} finally {
latch.unblock();
}
}
use of org.eclipse.scout.rt.mom.api.IMessageListener in project scout.rt by eclipse.
the class JmsMomImplementorTest method testQueuePublishFirst.
@Test
// regression
@Times(10)
public void testQueuePublishFirst() throws InterruptedException {
IDestination<String> queue = MOM.newDestination("test/mom/testQueuePublishFirst", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
// Publish a message
MOM.publish(JmsTestMom.class, queue, "hello world");
// Subscribe for the destination
final CountDownLatch latch = new CountDownLatch(1);
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
latch.countDown();
}
}));
// Verify
assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
}
use of org.eclipse.scout.rt.mom.api.IMessageListener in project scout.rt by eclipse.
the class JmsMomImplementorTest method testPublishTransactional.
@Test
public void testPublishTransactional() throws InterruptedException {
final Capturer<Person> capturer = new Capturer<>();
Person person = new Person();
person.setLastname("smith");
person.setFirstname("anna");
ITransaction tx = BEANS.get(ITransaction.class);
ITransaction.CURRENT.set(tx);
IDestination<Person> queue = MOM.newDestination("test/mom/testPublishTransactional", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queue, BEANS.get(ObjectMarshaller.class)));
MOM.publish(JmsTestMom.class, queue, person, MOM.newPublishInput().withTransactional(true));
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<Person>() {
@Override
public void onMessage(IMessage<Person> message) {
capturer.set(message.getTransferObject());
}
}));
try {
capturer.get(2, TimeUnit.SECONDS);
fail();
} catch (TimedOutError e) {
// expected
}
tx.commitPhase1();
tx.commitPhase2();
try {
capturer.get(5, TimeUnit.SECONDS);
} catch (TimedOutError e) {
fail();
} finally {
tx.release();
}
// Verify
Person testee = capturer.get();
assertEquals("smith", testee.getLastname());
assertEquals("anna", testee.getFirstname());
}
Aggregations