Example usage for org.springframework.integration.store MessageGroupQueue MessageGroupQueue

List of usage examples for org.springframework.integration.store MessageGroupQueue MessageGroupQueue

Introduction

In this page you can find the example usage for org.springframework.integration.store MessageGroupQueue MessageGroupQueue.

Prototype

public MessageGroupQueue(BasicMessageGroupStore messageGroupStore, Object groupId, Lock storeLock) 

Source Link

Usage

From source file:org.springframework.integration.jdbc.MessageGroupQueueTests.java

@Test
public void validateMgqInterruption() throws Exception {

    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1);

    final AtomicReference<InterruptedException> exceptionHolder = new AtomicReference<InterruptedException>();

    Thread t = new Thread(() -> {
        queue.offer(new GenericMessage<String>("hello"));
        try {/*from ww w . j a v a 2 s . c  o  m*/
            queue.offer(new GenericMessage<String>("hello"), 100, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            exceptionHolder.set(e);
        }
    });
    t.start();
    Thread.sleep(1000);
    t.interrupt();
    Thread.sleep(1000);
    assertTrue(exceptionHolder.get() instanceof InterruptedException);
}

From source file:org.springframework.integration.jdbc.MessageGroupQueueTests.java

@Test
public void testConcurrentReadWrite() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1);
    final AtomicReference<Message<?>> messageHolder = new AtomicReference<Message<?>>();

    Thread t1 = new Thread(() -> {
        try {/*from ww w.  j  a va  2 s.  c o  m*/
            messageHolder.set(queue.poll(1000, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            queue.offer(new GenericMessage<String>("hello"), 1000, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    t1.start();
    t2.start();
    Thread.sleep(1000);
    assertTrue(messageHolder.get() instanceof Message);
}

From source file:org.springframework.integration.jdbc.MessageGroupQueueTests.java

@Test
public void testConcurrentWriteRead() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1);
    final AtomicReference<Message<?>> messageHolder = new AtomicReference<Message<?>>();

    queue.offer(new GenericMessage<String>("hello"), 1000, TimeUnit.SECONDS);

    Thread t1 = new Thread(() -> {
        try {//from  w  ww  .j  a  va 2s  .  co m
            queue.offer(new GenericMessage<String>("Hi"), 1000, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            queue.poll(1000, TimeUnit.SECONDS);
            messageHolder.set(queue.poll(1000, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });

    t1.start();
    Thread.sleep(1000);
    t2.start();
    Thread.sleep(1000);
    assertTrue(messageHolder.get().getPayload().equals("Hi"));
}

From source file:org.springframework.integration.jdbc.MessageGroupQueueTests.java

@Test
public void testConcurrentReadersWithTimeout() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1);
    final AtomicReference<Message<?>> messageHolder1 = new AtomicReference<Message<?>>();
    final AtomicReference<Message<?>> messageHolder2 = new AtomicReference<Message<?>>();
    final AtomicReference<Message<?>> messageHolder3 = new AtomicReference<Message<?>>();

    Thread t1 = new Thread(() -> {
        try {//  ww  w. j a  v a2s.co  m
            messageHolder1.set(queue.poll(10, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            messageHolder2.set(queue.poll(10, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    Thread t3 = new Thread(() -> {
        try {
            messageHolder3.set(queue.poll(10, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });
    Thread t4 = new Thread(() -> {
        try {
            queue.offer(new GenericMessage<String>("Hi"), 10, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    t1.start();
    Thread.sleep(1000);
    t2.start();
    Thread.sleep(1000);
    t3.start();
    Thread.sleep(1000);
    t4.start();
    Thread.sleep(1000);
    assertNotNull(messageHolder1.get());
    assertEquals("Hi", messageHolder1.get().getPayload());
    Thread.sleep(4000);
    assertTrue(messageHolder2.get() == null);
}

From source file:org.springframework.integration.jdbc.MessageGroupQueueTests.java

@Test
public void testConcurrentWritersWithTimeout() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1);
    final AtomicReference<Boolean> booleanHolder1 = new AtomicReference<Boolean>(true);
    final AtomicReference<Boolean> booleanHolder2 = new AtomicReference<Boolean>(true);
    final AtomicReference<Boolean> booleanHolder3 = new AtomicReference<Boolean>(true);

    Thread t1 = new Thread(() -> {
        try {//from   ww w  .  ja  v a  2 s.  c  o m
            booleanHolder1.set(queue.offer(new GenericMessage<String>("Hi-1"), 2, TimeUnit.SECONDS));
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            boolean offered = queue.offer(new GenericMessage<String>("Hi-2"), 2, TimeUnit.SECONDS);
            booleanHolder2.set(offered);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    Thread t3 = new Thread(() -> {
        try {
            boolean offered = queue.offer(new GenericMessage<String>("Hi-3"), 2, TimeUnit.SECONDS);
            booleanHolder3.set(offered);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    t1.start();
    Thread.sleep(1000);
    t2.start();
    Thread.sleep(100);
    t3.start();
    Thread.sleep(4000);
    assertTrue(booleanHolder1.get());
    assertFalse(booleanHolder2.get());
    assertFalse(booleanHolder3.get());
}

From source file:org.springframework.integration.jdbc.MessageGroupQueueTests.java

@Test
public void testConcurrentWriteReadMulti() throws Exception {
    final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 4);
    final AtomicReference<Message<?>> messageHolder = new AtomicReference<Message<?>>();

    queue.offer(new GenericMessage<String>("hello"), 1000, TimeUnit.SECONDS);

    Thread t1 = new Thread(() -> {
        try {/*w w  w .  j a  v  a2 s  .  co m*/
            queue.offer(new GenericMessage<String>("Hi"), 1000, TimeUnit.SECONDS);
            queue.offer(new GenericMessage<String>("Hi"), 1000, TimeUnit.SECONDS);
            queue.offer(new GenericMessage<String>("Hi"), 1000, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue offer failed", e);
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            queue.poll(1000, TimeUnit.SECONDS);
            messageHolder.set(queue.poll(1000, TimeUnit.SECONDS));
            queue.poll(1000, TimeUnit.SECONDS);
            queue.poll(1000, TimeUnit.SECONDS);
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("queue poll failed", e);
        }
    });

    t1.start();
    Thread.sleep(1000);
    t2.start();
    Thread.sleep(1000);
    assertTrue(messageHolder.get().getPayload().equals("Hi"));
    assertNull(queue.poll(5, TimeUnit.SECONDS));
}

From source file:org.springframework.integration.jdbc.MessageGroupQueueTests.java

@Test
public void validateMgqInterruptionStoreLock() throws Exception {

    MessageGroupStore mgs = Mockito.mock(MessageGroupStore.class);
    Mockito.doAnswer(invocation -> {//from w  w w . j a v  a2  s  .c o  m
        Thread.sleep(5000);
        return null;
    }).when(mgs).addMessageToGroup(Mockito.any(Integer.class), Mockito.any(Message.class));

    MessageGroup mg = Mockito.mock(MessageGroup.class);
    Mockito.when(mgs.getMessageGroup(Mockito.any())).thenReturn(mg);
    Mockito.when(mg.size()).thenReturn(0);

    final MessageGroupQueue queue = new MessageGroupQueue(mgs, 1, 1);

    final AtomicReference<InterruptedException> exceptionHolder = new AtomicReference<InterruptedException>();

    Thread t1 = new Thread(() -> queue.offer(new GenericMessage<String>("hello")));
    t1.start();
    Thread.sleep(500);
    Thread t2 = new Thread(() -> {
        queue.offer(new GenericMessage<String>("hello"));
        try {
            queue.offer(new GenericMessage<String>("hello"), 100, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            exceptionHolder.set(e);
        }
    });
    t2.start();
    Thread.sleep(1000);
    t2.interrupt();
    Thread.sleep(1000);
    assertTrue(exceptionHolder.get() instanceof InterruptedException);
}

From source file:org.springframework.integration.store.MessageGroupQueueTests.java

@Test
public void testCapacityAfterExpiry() throws Exception {
    SimpleMessageStore messageGroupStore = new SimpleMessageStore();
    MessageGroupQueue queue = new MessageGroupQueue(messageGroupStore, "FOO", 2);
    queue.put(new GenericMessage<String>("foo"));
    assertEquals(1, queue.remainingCapacity());
    queue.put(new GenericMessage<String>("bar"));
    assertEquals(0, queue.remainingCapacity());
    Message<?> result = queue.poll(100, TimeUnit.MILLISECONDS);
    assertNotNull(result);//ww  w .j  a  v  a2 s  .  co m
    assertEquals(1, queue.remainingCapacity());
}

From source file:org.springframework.integration.store.MessageGroupQueueTests.java

@Test
public void testCapacityExceeded() throws Exception {
    SimpleMessageStore messageGroupStore = new SimpleMessageStore();
    MessageGroupQueue queue = new MessageGroupQueue(messageGroupStore, "FOO", 1);
    queue.put(new GenericMessage<String>("foo"));
    assertFalse(queue.offer(new GenericMessage<String>("bar"), 100, TimeUnit.MILLISECONDS));
}