Search in sources :

Example 26 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testDontReapMessageOfOtherHandler.

@Test
public void testDontReapMessageOfOtherHandler() throws Exception {
    MessageGroupStore groupStore = new SimpleMessageStore();
    AggregatingMessageHandler handler1 = new AggregatingMessageHandler(group -> group, groupStore);
    AggregatingMessageHandler handler2 = new AggregatingMessageHandler(group -> group, groupStore);
    QueueChannel handler1DiscardChannel = new QueueChannel();
    handler1.setDiscardChannel(handler1DiscardChannel);
    QueueChannel handler2DiscardChannel = new QueueChannel();
    handler2.setDiscardChannel(handler2DiscardChannel);
    handler1.setReleaseStrategy(group -> false);
    handler2.setReleaseStrategy(group -> false);
    handler1.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    handler1.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    handler2.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("bar").build());
    groupStore.expireMessageGroups(0);
    assertTrue(handler1DiscardChannel.getQueueSize() == 2);
    assertTrue(handler2DiscardChannel.getQueueSize() == 1);
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 27 with MessageGroupStore

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

the class AggregatorIntegrationTests method testGroupTimeoutScheduling.

@Test
public void testGroupTimeoutScheduling() throws Exception {
    for (int i = 0; i < 5; i++) {
        Map<String, Object> headers = stubHeaders(i, 5, 1);
        this.groupTimeoutAggregatorInput.send(new GenericMessage<Integer>(i, headers));
        // Wait until 'group-timeout' does its stuff.
        MessageGroupStore mgs = TestUtils.getPropertyValue(this.context.getBean("gta.handler"), "messageStore", MessageGroupStore.class);
        int n = 0;
        while (n++ < 100 && mgs.getMessageGroupCount() > 0) {
            Thread.sleep(100);
        }
        assertTrue("Group did not complete", n < 100);
        assertNotNull(this.output.receive(10000));
        assertNull(this.discard.receive(0));
    }
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 28 with MessageGroupStore

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

the class DelayHandlerTests method testRescheduleForTheDateDelay.

@Test
public void testRescheduleForTheDateDelay() throws Exception {
    this.delayHandler.setDelayExpression(new SpelExpressionParser().parseExpression("payload"));
    this.delayHandler.setOutputChannel(new DirectChannel());
    this.delayHandler.setIgnoreExpressionFailures(false);
    startDelayerHandler();
    Calendar releaseDate = Calendar.getInstance();
    releaseDate.add(Calendar.HOUR, 1);
    this.delayHandler.handleMessage(new GenericMessage<>(releaseDate.getTime()));
    // emulate restart
    this.taskScheduler.destroy();
    MessageGroupStore messageStore = TestUtils.getPropertyValue(this.delayHandler, "messageStore", MessageGroupStore.class);
    MessageGroup messageGroup = messageStore.getMessageGroup(DELAYER_MESSAGE_GROUP_ID);
    Message<?> messageInStore = messageGroup.getMessages().iterator().next();
    Object payload = messageInStore.getPayload();
    DirectFieldAccessor dfa = new DirectFieldAccessor(payload);
    long requestTime = (long) dfa.getPropertyValue("requestDate");
    Calendar requestDate = Calendar.getInstance();
    requestDate.setTimeInMillis(requestTime);
    requestDate.add(Calendar.HOUR, -2);
    dfa.setPropertyValue("requestDate", requestDate.getTimeInMillis());
    this.taskScheduler.afterPropertiesSet();
    this.delayHandler.reschedulePersistedMessages();
    Queue<?> works = TestUtils.getPropertyValue(this.taskScheduler, "scheduledExecutor.workQueue", Queue.class);
    int n = 0;
    while (n++ < 2000 && works.size() == 0) {
        Thread.sleep(10);
    }
    assertEquals(1, works.size());
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) DirectChannel(org.springframework.integration.channel.DirectChannel) Calendar(java.util.Calendar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test)

Example 29 with MessageGroupStore

use of org.springframework.integration.store.MessageGroupStore 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)

Example 30 with MessageGroupStore

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

the class DelayerHandlerRescheduleIntegrationTests method testDelayerHandlerRescheduleWithJdbcMessageStore.

// INT-1132
@Test
public void testDelayerHandlerRescheduleWithJdbcMessageStore() throws Exception {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("DelayerHandlerRescheduleIntegrationTests-context.xml", getClass());
    MessageChannel input = context.getBean("input", MessageChannel.class);
    MessageGroupStore messageStore = context.getBean("messageStore", MessageGroupStore.class);
    assertEquals(0, messageStore.getMessageGroupCount());
    Message<String> message1 = MessageBuilder.withPayload("test1").build();
    input.send(message1);
    input.send(MessageBuilder.withPayload("test2").build());
    // Emulate restart and check DB state before next start
    // Interrupt taskScheduler as quickly as possible
    ThreadPoolTaskScheduler taskScheduler = (ThreadPoolTaskScheduler) IntegrationContextUtils.getTaskScheduler(context);
    taskScheduler.shutdown();
    taskScheduler.getScheduledExecutor().awaitTermination(10, TimeUnit.SECONDS);
    context.close();
    try {
        context.getBean("input", MessageChannel.class);
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertTrue(e instanceof IllegalStateException);
        assertTrue(e.getMessage().contains("BeanFactory not initialized or already closed - call 'refresh'"));
    }
    String delayerMessageGroupId = UUIDConverter.getUUID(DELAYER_ID + ".messageGroupId").toString();
    assertEquals(1, messageStore.getMessageGroupCount());
    assertEquals(delayerMessageGroupId, messageStore.iterator().next().getGroupId());
    assertEquals(2, messageStore.messageGroupSize(delayerMessageGroupId));
    assertEquals(2, messageStore.getMessageCountForAllMessageGroups());
    MessageGroup messageGroup = messageStore.getMessageGroup(delayerMessageGroupId);
    // Ensure that with the lazyLoadMessageGroups = false the MessageStore doesn't return PersistentMessageGroup
    assertThat(messageGroup, instanceOf(SimpleMessageGroup.class));
    Message<?> messageInStore = messageGroup.getMessages().iterator().next();
    Object payload = messageInStore.getPayload();
    // INT-3049
    assertTrue(payload instanceof DelayHandler.DelayedMessageWrapper);
    assertEquals(message1, ((DelayHandler.DelayedMessageWrapper) payload).getOriginal());
    context.refresh();
    PollableChannel output = context.getBean("output", PollableChannel.class);
    Message<?> message = output.receive(20000);
    assertNotNull(message);
    Object payload1 = message.getPayload();
    message = output.receive(20000);
    assertNotNull(message);
    Object payload2 = message.getPayload();
    assertNotSame(payload1, payload2);
    assertEquals(1, messageStore.getMessageGroupCount());
    assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId));
    context.close();
}
Also used : DelayHandler(org.springframework.integration.handler.DelayHandler) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) MessagingException(org.springframework.messaging.MessagingException) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

MessageGroupStore (org.springframework.integration.store.MessageGroupStore)40 Test (org.junit.Test)37 MessageGroup (org.springframework.integration.store.MessageGroup)25 GenericMessage (org.springframework.messaging.support.GenericMessage)21 MongoDbAvailable (org.springframework.integration.mongodb.rules.MongoDbAvailable)17 AbstractBatchingMessageGroupStore (org.springframework.integration.store.AbstractBatchingMessageGroupStore)17 MongoClient (com.mongodb.MongoClient)16 SimpleMongoDbFactory (org.springframework.data.mongodb.core.SimpleMongoDbFactory)16 QueueChannel (org.springframework.integration.channel.QueueChannel)8 SimpleMessageStore (org.springframework.integration.store.SimpleMessageStore)8 Message (org.springframework.messaging.Message)8 Matchers.containsString (org.hamcrest.Matchers.containsString)7 MessageChannel (org.springframework.messaging.MessageChannel)7 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)6 ArrayList (java.util.ArrayList)5 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)5 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)5 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)5