Search in sources :

Example 16 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testLastReleasedSequenceNumber.

@Test
@MongoDbAvailable
public void testLastReleasedSequenceNumber() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    MessageGroup messageGroup = store.getMessageGroup(1);
    assertNotNull(messageGroup);
    Message<?> message = new GenericMessage<String>("Hello");
    store.addMessagesToGroup(messageGroup.getGroupId(), message);
    store.setLastReleasedSequenceNumberForGroup(messageGroup.getGroupId(), 5);
    messageGroup = store.getMessageGroup(1);
    assertEquals(5, messageGroup.getLastReleasedMessageSequenceNumber());
}
Also used : MongoClient(com.mongodb.MongoClient) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractBatchingMessageGroupStore(org.springframework.integration.store.AbstractBatchingMessageGroupStore) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 17 with MessageGroupStore

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

the class DelayerHandlerRescheduleIntegrationTests method testDelayerHandlerRescheduleWithMongoDbMessageStore.

@SuppressWarnings("unchecked")
private void testDelayerHandlerRescheduleWithMongoDbMessageStore(String config) throws Exception {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext(config, this.getClass());
    MessageChannel input = context.getBean("input", MessageChannel.class);
    MessageGroupStore messageStore = context.getBean("messageStore", MessageGroupStore.class);
    String delayerMessageGroupId = DELAYER_ID + ".messageGroupId";
    messageStore.removeMessageGroup(delayerMessageGroupId);
    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);
    assertEquals(2, messageStore.messageGroupSize(delayerMessageGroupId));
    MessageGroup messageGroup = messageStore.getMessageGroup(delayerMessageGroupId);
    Iterator<Message<?>> iterator = messageGroup.getMessages().iterator();
    Message<?> messageInStore = iterator.next();
    Object payload = messageInStore.getPayload();
    // INT-3049
    assertTrue(payload instanceof DelayHandler.DelayedMessageWrapper);
    Message<String> original1 = (Message<String>) ((DelayHandler.DelayedMessageWrapper) payload).getOriginal();
    messageInStore = iterator.next();
    Message<String> original2 = (Message<String>) ((DelayHandler.DelayedMessageWrapper) messageInStore.getPayload()).getOriginal();
    assertThat(message1, Matchers.anyOf(Matchers.is(original1), Matchers.is(original2)));
    context.close();
    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);
    messageStore = context.getBean("messageStore", MessageGroupStore.class);
    assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId));
    context.close();
}
Also used : DelayHandler(org.springframework.integration.handler.DelayHandler) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) Message(org.springframework.messaging.Message) MessageGroup(org.springframework.integration.store.MessageGroup) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel)

Example 18 with MessageGroupStore

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

the class ResequencerIntegrationTests method validateUnboundedResequencerLight.

@Test
public void validateUnboundedResequencerLight() {
    MessageChannel inputChannel = context.getBean("resequencerLightInput", MessageChannel.class);
    QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
    EventDrivenConsumer edc = context.getBean("resequencerLight", EventDrivenConsumer.class);
    ResequencingMessageHandler handler = TestUtils.getPropertyValue(edc, "handler", ResequencingMessageHandler.class);
    MessageGroupStore store = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
    Message<?> message1 = MessageBuilder.withPayload("1").setCorrelationId("A").setSequenceNumber(1).build();
    Message<?> message2 = MessageBuilder.withPayload("2").setCorrelationId("A").setSequenceNumber(2).setCorrelationId("A").setSequenceNumber(2).setHeader("foo", "foo").build();
    Message<?> message3 = MessageBuilder.withPayload("3").setCorrelationId("A").setSequenceNumber(3).build();
    Message<?> message4 = MessageBuilder.withPayload("4").setCorrelationId("A").setSequenceNumber(4).setHeader("foo", "foo").build();
    Message<?> message5 = MessageBuilder.withPayload("5").setCorrelationId("A").setSequenceNumber(5).build();
    Message<?> message6 = MessageBuilder.withPayload("6").setCorrelationId("A").setSequenceNumber(6).build();
    inputChannel.send(message3);
    assertNull(outputChannel.receive(0));
    inputChannel.send(message1);
    message1 = outputChannel.receive(0);
    assertNotNull(message1);
    assertEquals(1, new IntegrationMessageHeaderAccessor(message1).getSequenceNumber());
    assertFalse(message1.getHeaders().containsKey("foo"));
    inputChannel.send(message2);
    message2 = outputChannel.receive(0);
    message3 = outputChannel.receive(0);
    assertNotNull(message2);
    assertNotNull(message3);
    assertEquals(2, new IntegrationMessageHeaderAccessor(message2).getSequenceNumber());
    assertTrue(message2.getHeaders().containsKey("foo"));
    assertEquals(3, new IntegrationMessageHeaderAccessor(message3).getSequenceNumber());
    assertFalse(message3.getHeaders().containsKey("foo"));
    inputChannel.send(message5);
    assertNull(outputChannel.receive(0));
    inputChannel.send(message6);
    assertNull(outputChannel.receive(0));
    inputChannel.send(message4);
    message4 = outputChannel.receive(0);
    message5 = outputChannel.receive(0);
    message6 = outputChannel.receive(0);
    assertNotNull(message4);
    assertNotNull(message5);
    assertNotNull(message6);
    assertEquals(4, new IntegrationMessageHeaderAccessor(message4).getSequenceNumber());
    assertTrue(message4.getHeaders().containsKey("foo"));
    assertEquals(5, new IntegrationMessageHeaderAccessor(message5).getSequenceNumber());
    assertFalse(message5.getHeaders().containsKey("foo"));
    assertEquals(6, new IntegrationMessageHeaderAccessor(message6).getSequenceNumber());
    assertFalse(message6.getHeaders().containsKey("foo"));
    assertEquals(0, store.getMessageGroup("A").getMessages().size());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) ResequencingMessageHandler(org.springframework.integration.aggregator.ResequencingMessageHandler) Test(org.junit.Test)

Example 19 with MessageGroupStore

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

the class ResequencerIntegrationTests method validateUnboundedResequencerDeep.

@Test
public void validateUnboundedResequencerDeep() {
    MessageChannel inputChannel = context.getBean("resequencerDeepInput", MessageChannel.class);
    QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
    EventDrivenConsumer edc = context.getBean("resequencerDeep", EventDrivenConsumer.class);
    ResequencingMessageHandler handler = TestUtils.getPropertyValue(edc, "handler", ResequencingMessageHandler.class);
    MessageGroupStore store = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
    Message<?> message1 = MessageBuilder.withPayload("1").setCorrelationId("A").setSequenceNumber(1).build();
    Message<?> message2 = MessageBuilder.withPayload("2").setCorrelationId("A").setSequenceNumber(2).build();
    Message<?> message3 = MessageBuilder.withPayload("3").setCorrelationId("A").setSequenceNumber(3).build();
    inputChannel.send(message3);
    assertNull(outputChannel.receive(0));
    inputChannel.send(message1);
    assertNotNull(outputChannel.receive(0));
    inputChannel.send(message2);
    assertNotNull(outputChannel.receive(0));
    assertNotNull(outputChannel.receive(0));
    assertEquals(0, store.getMessageGroup("A").getMessages().size());
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) ResequencingMessageHandler(org.springframework.integration.aggregator.ResequencingMessageHandler) Test(org.junit.Test)

Example 20 with MessageGroupStore

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

the class AbstractCorrelatingMessageHandlerTests method testReaperDoesntReapAProcessingGroup.

// INT-2751
@Test
public void testReaperDoesntReapAProcessingGroup() throws Exception {
    final MessageGroupStore groupStore = new SimpleMessageStore();
    final CountDownLatch waitForSendLatch = new CountDownLatch(1);
    final CountDownLatch waitReapStartLatch = new CountDownLatch(1);
    final CountDownLatch waitReapCompleteLatch = new CountDownLatch(1);
    AbstractCorrelatingMessageHandler handler = new AbstractCorrelatingMessageHandler(group -> group, groupStore) {

        @Override
        protected void afterRelease(MessageGroup group, Collection<Message<?>> completedMessages) {
        }
    };
    handler.setReleasePartialSequences(true);
    /*
		 * Runs "reap" when group 'bar' is in completion
		 */
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        try {
            waitReapStartLatch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
        }
        waitForSendLatch.countDown();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e2) {
            Thread.currentThread().interrupt();
        }
        groupStore.expireMessageGroups(50);
        waitReapCompleteLatch.countDown();
    });
    final List<Message<?>> outputMessages = new ArrayList<Message<?>>();
    handler.setOutputChannel((message, timeout) -> {
        /*
			 * Executes when group 'bar' completes normally
			 */
        outputMessages.add(message);
        // wake reaper
        waitReapStartLatch.countDown();
        try {
            waitForSendLatch.await(10, TimeUnit.SECONDS);
            // wait a little longer for reaper to grab groups
            Thread.sleep(2000);
            // simulate tx commit
            groupStore.removeMessageGroup("bar");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return true;
    });
    handler.setReleaseStrategy(group -> group.size() == 2);
    QueueChannel discards = new QueueChannel();
    handler.setDiscardChannel(discards);
    handler.setSendPartialResultOnExpiry(true);
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId("qux").build();
    // partial group that will be reaped
    handler.handleMessage(message);
    message = MessageBuilder.withPayload("foo").setCorrelationId("bar").build();
    // full group that should not be reaped
    handler.handleMessage(message);
    message = MessageBuilder.withPayload("baz").setCorrelationId("bar").build();
    handler.handleMessage(message);
    assertTrue(waitReapCompleteLatch.await(10, TimeUnit.SECONDS));
    // Before INT-2751 we got bar + bar + qux
    // bar + qux
    assertEquals(2, outputMessages.size());
    // normal release
    // 'bar'
    assertEquals(2, ((MessageGroup) outputMessages.get(0).getPayload()).size());
    // reaper release
    // 'qux'
    assertEquals(1, ((MessageGroup) outputMessages.get(1).getPayload()).size());
    assertNull(discards.receive(0));
    exec.shutdownNow();
}
Also used : MessageGroupStore(org.springframework.integration.store.MessageGroupStore) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection) 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