Search in sources :

Example 31 with MessageGroupStore

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

the class DelayerHandlerRescheduleIntegrationTests method testRollbackOnDelayerHandlerReleaseTask.

// INT-2649
@Test
public void testRollbackOnDelayerHandlerReleaseTask() throws Exception {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("DelayerHandlerRescheduleIntegrationTests-context.xml", getClass());
    MessageChannel input = context.getBean("transactionalDelayerInput", MessageChannel.class);
    MessageGroupStore messageStore = context.getBean("messageStore", MessageGroupStore.class);
    String delayerMessageGroupId = UUIDConverter.getUUID("transactionalDelayer.messageGroupId").toString();
    assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId));
    input.send(MessageBuilder.withPayload("test").build());
    Thread.sleep(1000);
    assertEquals(1, messageStore.messageGroupSize(delayerMessageGroupId));
    // To check that 'rescheduling' works in the transaction boundaries too
    context.close();
    context.refresh();
    assertTrue(RollbackTxSync.latch.await(20, TimeUnit.SECONDS));
    // On transaction rollback the delayed Message should remain in the persistent MessageStore
    assertEquals(1, messageStore.messageGroupSize(delayerMessageGroupId));
    context.close();
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 32 with MessageGroupStore

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

the class DelayerHandlerRescheduleIntegrationTests method testDelayerHandlerRescheduleWithGemfireMessageStore.

@Test
public void testDelayerHandlerRescheduleWithGemfireMessageStore() throws Exception {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("DelayerHandlerRescheduleIntegrationTests-context.xml", this.getClass());
    MessageChannel input = context.getBean("input", MessageChannel.class);
    MessageGroupStore messageStore = context.getBean("messageStore", MessageGroupStore.class);
    String delayerMessageGroupId = DELAYER_ID + ".messageGroupId";
    Message<String> message1 = MessageBuilder.withPayload("test1").build();
    input.send(message1);
    input.send(MessageBuilder.withPayload("test2").build());
    // Emulate restart and check Cache 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'"));
    }
    assertEquals(1, messageStore.getMessageGroupCount());
    assertEquals(delayerMessageGroupId, messageStore.iterator().next().getGroupId());
    assertEquals(2, messageStore.messageGroupSize(delayerMessageGroupId));
    assertEquals(2, messageStore.getMessageCountForAllMessageGroups());
    MessageGroup messageGroup = messageStore.getMessageGroup(delayerMessageGroupId);
    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());
    int n = 0;
    while (n++ < 200 && messageStore.messageGroupSize(delayerMessageGroupId) > 0) {
        Thread.sleep(100);
    }
    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) 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) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 33 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testRemoveMessageGroup.

@Test
@MongoDbAvailable
public void testRemoveMessageGroup() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store = this.getMessageGroupStore();
    MessageStore messageStore = this.getMessageStore();
    MessageGroup messageGroup = store.getMessageGroup(1);
    Message<?> message = new GenericMessage<String>("Hello");
    UUID id = message.getHeaders().getId();
    messageGroup = store.addMessageToGroup(1, message);
    assertNotNull(messageGroup);
    assertEquals(1, messageGroup.size());
    message = messageStore.getMessage(id);
    assertNotNull(message);
    store.removeMessageGroup(1);
    MessageGroup messageGroupA = store.getMessageGroup(1);
    assertEquals(0, messageGroupA.size());
    assertFalse(messageGroupA.equals(messageGroup));
}
Also used : MessageStore(org.springframework.integration.store.MessageStore) 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) UUID(java.util.UUID) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 34 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testMessageGroupIterator.

@Test
@MongoDbAvailable
public void testMessageGroupIterator() throws Exception {
    this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
    MessageGroupStore store1 = this.getMessageGroupStore();
    MessageGroupStore store2 = this.getMessageGroupStore();
    Message<?> message = new GenericMessage<String>("1");
    store2.addMessagesToGroup("1", message);
    store1.addMessagesToGroup("2", new GenericMessage<String>("2"));
    store2.addMessagesToGroup("3", new GenericMessage<String>("3"));
    MessageGroupStore store3 = this.getMessageGroupStore();
    Iterator<MessageGroup> iterator = store3.iterator();
    assertNotNull(iterator);
    int counter = 0;
    while (iterator.hasNext()) {
        iterator.next();
        counter++;
    }
    assertEquals(3, counter);
    store2.removeMessagesFromGroup("1", message);
    iterator = store3.iterator();
    counter = 0;
    while (iterator.hasNext()) {
        iterator.next();
        counter++;
    }
    assertEquals(2, counter);
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 35 with MessageGroupStore

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

the class AbstractMongoDbMessageGroupStoreTests method testCompleteMessageGroup.

@Test
@MongoDbAvailable
public void testCompleteMessageGroup() 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.completeGroup(messageGroup.getGroupId());
    messageGroup = store.getMessageGroup(1);
    assertTrue(messageGroup.isComplete());
}
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)

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