Search in sources :

Example 6 with IMessage

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));
}
Also used : IMessage(org.eclipse.scout.rt.mom.api.IMessage) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 7 with IMessage

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IMessage(org.eclipse.scout.rt.mom.api.IMessage) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 8 with IMessage

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);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IMessage(org.eclipse.scout.rt.mom.api.IMessage) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test)

Example 9 with IMessage

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();
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IMessage(org.eclipse.scout.rt.mom.api.IMessage) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test)

Example 10 with IMessage

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));
}
Also used : IRequestListener(org.eclipse.scout.rt.mom.api.IRequestListener) IMessage(org.eclipse.scout.rt.mom.api.IMessage) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Aggregations

IMessage (org.eclipse.scout.rt.mom.api.IMessage)16 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)12 Test (org.junit.Test)12 IMessageListener (org.eclipse.scout.rt.mom.api.IMessageListener)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 IRequestListener (org.eclipse.scout.rt.mom.api.IRequestListener)5 Times (org.eclipse.scout.rt.testing.platform.runner.Times)5 JMSException (javax.jms.JMSException)3 NamingException (javax.naming.NamingException)3 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)3 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)3 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)3 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)3 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)2 StringHolder (org.eclipse.scout.rt.platform.holders.StringHolder)1 ITransaction (org.eclipse.scout.rt.platform.transaction.ITransaction)1 FinalValue (org.eclipse.scout.rt.platform.util.FinalValue)1