Search in sources :

Example 1 with MessageGroupQueue

use of org.springframework.integration.store.MessageGroupQueue in project spring-integration by spring-projects.

the class PriorityChannelSpec method messageStore.

public PriorityChannelSpec messageStore(PriorityCapableChannelMessageStore messageGroupStore, Object groupId) {
    this.messageGroupQueue = new MessageGroupQueue(messageGroupStore, groupId);
    this.messageGroupQueue.setPriority(true);
    return this;
}
Also used : MessageGroupQueue(org.springframework.integration.store.MessageGroupQueue)

Example 2 with MessageGroupQueue

use of org.springframework.integration.store.MessageGroupQueue in project spring-integration by spring-projects.

the class MessageGroupQueueTests method validateMgqInterruption.

@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 {
            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);
}
Also used : SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) MessageGroupQueue(org.springframework.integration.store.MessageGroupQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 3 with MessageGroupQueue

use of org.springframework.integration.store.MessageGroupQueue in project spring-integration by spring-projects.

the class MessageGroupQueueTests method testConcurrentWriteRead.

@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 {
            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"));
}
Also used : SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroupQueue(org.springframework.integration.store.MessageGroupQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 4 with MessageGroupQueue

use of org.springframework.integration.store.MessageGroupQueue in project spring-integration by spring-projects.

the class MessageGroupQueueTests method testConcurrentReadWrite.

@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 {
            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);
}
Also used : SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroupQueue(org.springframework.integration.store.MessageGroupQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 5 with MessageGroupQueue

use of org.springframework.integration.store.MessageGroupQueue in project spring-integration by spring-projects.

the class MessageGroupQueueTests method validateMgqInterruptionStoreLock.

@Test
public void validateMgqInterruptionStoreLock() throws Exception {
    MessageGroupStore mgs = Mockito.mock(MessageGroupStore.class);
    Mockito.doAnswer(invocation -> {
        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);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroupQueue(org.springframework.integration.store.MessageGroupQueue) MessageGroup(org.springframework.integration.store.MessageGroup) AtomicReference(java.util.concurrent.atomic.AtomicReference) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

MessageGroupQueue (org.springframework.integration.store.MessageGroupQueue)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Test (org.junit.Test)7 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)7 SimpleMessageStore (org.springframework.integration.store.SimpleMessageStore)6 GenericMessage (org.springframework.messaging.support.GenericMessage)6 Message (org.springframework.messaging.Message)5 MessageGroup (org.springframework.integration.store.MessageGroup)1 MessageGroupStore (org.springframework.integration.store.MessageGroupStore)1