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

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

Introduction

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

Prototype

@Override
    public boolean offer(Message<?> message) 

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 {/*  ww  w  .  ja  v a 2 s.com*/
            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 validateMgqInterruptionStoreLock() throws Exception {

    MessageGroupStore mgs = Mockito.mock(MessageGroupStore.class);
    Mockito.doAnswer(invocation -> {/*from  w  w w. ja  v a 2 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);
}