use of org.eclipse.scout.rt.mom.api.IMessage in project scout.rt by eclipse.
the class JmsMomImplementorTest method testTimeToLive.
@Test
@Times(10)
public void testTimeToLive() throws InterruptedException {
IDestination<String> queue = MOM.newDestination("test/mom/testTimeToLive", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
final CountDownLatch latch = new CountDownLatch(1);
// Publish a message
MOM.publish(JmsTestMom.class, queue, "hello world", MOM.newPublishInput().withTimeToLive(1, TimeUnit.MILLISECONDS));
Thread.sleep(100);
// Subscribe for the destination
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
latch.countDown();
}
}));
// Verify
// expect the message not to be received
assertFalse(latch.await(50, TimeUnit.MILLISECONDS));
}
use of org.eclipse.scout.rt.mom.api.IMessage 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.IMessage 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.IMessage 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.IMessage in project scout.rt by eclipse.
the class JmsMomImplementorTest method testQueueRequestReplyMultipleSubscriptions.
@Test(timeout = 200_000)
// regression
@Times(10)
public void testQueueRequestReplyMultipleSubscriptions() throws InterruptedException {
IBiDestination<String, String> queue = MOM.newBiDestination("test/mom/testQueueRequestReplyMultipleSubscriptions", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
final CountDownLatch msgLatch = new CountDownLatch(2);
// Subscribe for the destination
m_disposables.add(MOM.reply(JmsTestMom.class, queue, new IRequestListener<String, String>() {
@Override
public String onRequest(IMessage<String> request) {
msgLatch.countDown();
return request.getTransferObject().toUpperCase();
}
}));
// Subscribe for the destination
m_disposables.add(MOM.reply(JmsTestMom.class, queue, new IRequestListener<String, String>() {
@Override
public String onRequest(IMessage<String> request) {
msgLatch.countDown();
return request.getTransferObject().toUpperCase();
}
}));
// Initiate 'request-reply' communication
String testee = MOM.request(JmsTestMom.class, queue, "hello world");
// Verify
assertEquals("HELLO WORLD", testee);
assertFalse(msgLatch.await(50, TimeUnit.MILLISECONDS));
}
Aggregations