Search in sources :

Example 6 with IMessageListener

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());
}
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 7 with IMessageListener

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);
}
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 8 with IMessageListener

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();
    }
}
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 9 with IMessageListener

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));
}
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 10 with IMessageListener

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

Aggregations

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