Search in sources :

Example 16 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testPutTakeUntakeRollbackUntake.

@Test
public void testPutTakeUntakeRollbackUntake() throws Exception {
    final QueueManager mgr = createQueueManager();
    mgr.start();
    final Latch latch = new Latch();
    final Serializable object1 = "string1";
    final Serializable object2 = "string2";
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                latch.countDown();
                Thread.sleep(200);
                QueueSession s = mgr.getQueueSession();
                Queue q = s.getQueue("queue1");
                assertEquals("Queue size", 0, q.size());
                s.begin();
                q.untake(object1);
                s.commit();
                s.begin();
                q.untake(object2);
                s.rollback();
            } catch (Exception e) {
            // ignore, let test fail
            }
        }
    };
    t.start();
    latch.await();
    long t0 = System.currentTimeMillis();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("queue1");
    assertEquals("Queue size", 0, q.size());
    Object o = q.take();
    long t1 = System.currentTimeMillis();
    t.join();
    assertNotNull(o);
    assertEquals("Queue content", object1, o);
    assertEquals("Queue size", 0, q.size());
    assertTrue(t1 - t0 > 100);
    mgr.stop();
}
Also used : Serializable(java.io.Serializable) Latch(org.mule.runtime.api.util.concurrent.Latch) Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) QueueManager(org.mule.runtime.core.api.util.queue.QueueManager) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 17 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testRecoverWarmRestart.

@Test
public void testRecoverWarmRestart() throws Exception {
    QueueManager mgr = createQueueManager();
    mgr.start();
    QueueSession s = mgr.getQueueSession();
    Queue q = s.getQueue("warmRecoverQueue");
    int toPopulate = 50;
    // Populate queue
    Random rnd = new Random();
    for (int j = 0; j < toPopulate; j++) {
        byte[] o = new byte[2048];
        rnd.nextBytes(o);
        q.put(o);
    }
    assertEquals(q.size(), toPopulate);
    // Stop and start TransactionalQueueManager
    mgr.stop();
    mgr.start();
    assertEquals(toPopulate, q.size());
}
Also used : Random(java.util.Random) Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) QueueManager(org.mule.runtime.core.api.util.queue.QueueManager) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 18 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testTransactedPutCommitWithPersistence.

@Test
public void testTransactedPutCommitWithPersistence() throws Exception {
    if (isPersistent()) {
        AbstractQueueManager mgr = createQueueManager();
        try {
            QueueSession s = mgr.getQueueSession();
            Queue q = s.getQueue("queue1");
            mgr.start();
            s.begin();
            q.put("String1");
            assertEquals("Queue size", 1, q.size());
            s.commit();
            assertEquals("Queue size", 1, q.size());
            s = mgr.getQueueSession();
            q = s.getQueue("queue1");
            assertEquals("Queue size", 1, q.size());
            mgr.stop();
            mgr = createQueueManager();
            s = mgr.getQueueSession();
            q = s.getQueue("queue1");
            mgr.start();
            assertEquals("Queue size", 1, q.size());
        } finally {
            mgr.stop();
            mgr.dispose();
        }
    } else {
        logger.info("Ignoring test because queue manager is not persistent");
    }
}
Also used : Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 19 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testClearInTransaction.

@Test
public void testClearInTransaction() throws Exception {
    final QueueManager mgr = createQueueManager();
    mgr.start();
    QueueSession s = mgr.getQueueSession();
    // insert item in transaction
    s.begin();
    Queue q = s.getQueue("queue1");
    assertEquals("Queue size", 0, q.size());
    q.put("String1");
    s.commit();
    assertEquals("Queue size", 1, q.size());
    // clear queue but rollback
    s.begin();
    assertEquals("Queue size", 1, q.size());
    q.clear();
    s.rollback();
    assertEquals("Queue size", 1, q.size());
    // do clear in transaction
    s.begin();
    assertEquals("Queue size", 1, q.size());
    q.clear();
    s.commit();
    assertEquals("Queue size", 0, q.size());
    mgr.stop();
}
Also used : Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) QueueManager(org.mule.runtime.core.api.util.queue.QueueManager) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Example 20 with QueueSession

use of org.mule.runtime.core.api.util.queue.QueueSession in project mule by mulesoft.

the class AbstractTransactionQueueManagerTestCase method testPutTake_RespectsOrderOnPersistence.

@Test
public void testPutTake_RespectsOrderOnPersistence() throws Exception {
    if (isPersistent()) {
        AbstractQueueManager mgr1 = createQueueManager();
        QueueSession s1 = mgr1.getQueueSession();
        Queue q1 = s1.getQueue("queue1");
        mgr1.start();
        assertEquals("Queue size", 0, q1.size());
        final int numberOfElements = 10;
        for (int i = 1; i <= numberOfElements; i++) {
            q1.put("String" + i);
            assertEquals("Queue size", i, q1.size());
        }
        mgr1.stop();
        AbstractQueueManager mgr2 = createQueueManager();
        QueueSession s2 = mgr2.getQueueSession();
        Queue q2 = s2.getQueue("queue1");
        mgr2.start();
        for (int i = 1; i <= numberOfElements; i++) {
            Object o = q2.take();
            assertNotNull(o);
            assertEquals("Queue content", "String" + i, o);
        }
        assertEquals("Queue size", 0, q2.size());
        mgr2.stop();
        mgr2.dispose();
    }
}
Also used : Queue(org.mule.runtime.core.api.util.queue.Queue) QueueSession(org.mule.runtime.core.api.util.queue.QueueSession) AbstractQueueManager(org.mule.runtime.core.internal.util.queue.AbstractQueueManager) Test(org.junit.Test)

Aggregations

QueueSession (org.mule.runtime.core.api.util.queue.QueueSession)22 Test (org.junit.Test)20 Queue (org.mule.runtime.core.api.util.queue.Queue)20 AbstractQueueManager (org.mule.runtime.core.internal.util.queue.AbstractQueueManager)18 QueueManager (org.mule.runtime.core.api.util.queue.QueueManager)16 Latch (org.mule.runtime.api.util.concurrent.Latch)7 DefaultQueueConfiguration (org.mule.runtime.core.api.util.queue.DefaultQueueConfiguration)5 Serializable (java.io.Serializable)2 Random (java.util.Random)2