Search in sources :

Example 71 with MessageGroup

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

the class GemfireGroupStoreTests method testCompleteMessageGroup.

@Test
public void testCompleteMessageGroup() throws Exception {
    GemfireMessageStore store = new GemfireMessageStore(region);
    MessageGroup messageGroup = store.getMessageGroup(1);
    Message<?> messageToMark = new GenericMessage<String>("1");
    store.addMessagesToGroup(messageGroup.getGroupId(), messageToMark);
    store.completeGroup(messageGroup.getGroupId());
    messageGroup = store.getMessageGroup(1);
    assertTrue(messageGroup.isComplete());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Test(org.junit.Test)

Example 72 with MessageGroup

use of org.springframework.integration.store.MessageGroup 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 73 with MessageGroup

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

the class JdbcMessageStore method getMessageGroup.

@Override
public MessageGroup getMessageGroup(Object groupId) {
    String key = getKey(groupId);
    final AtomicReference<Date> createDate = new AtomicReference<Date>();
    final AtomicReference<Date> updateDate = new AtomicReference<Date>();
    final AtomicReference<Boolean> completeFlag = new AtomicReference<Boolean>();
    final AtomicReference<Integer> lastReleasedSequenceRef = new AtomicReference<Integer>();
    this.jdbcTemplate.query(getQuery(Query.GET_GROUP_INFO), rs -> {
        updateDate.set(rs.getTimestamp("UPDATED_DATE"));
        createDate.set(rs.getTimestamp("CREATED_DATE"));
        completeFlag.set(rs.getInt("COMPLETE") > 0);
        lastReleasedSequenceRef.set(rs.getInt("LAST_RELEASED_SEQUENCE"));
    }, key, this.region);
    if (createDate.get() == null && updateDate.get() == null) {
        return new SimpleMessageGroup(groupId);
    }
    MessageGroup messageGroup = getMessageGroupFactory().create(this, groupId, createDate.get().getTime(), completeFlag.get());
    messageGroup.setLastModified(updateDate.get().getTime());
    messageGroup.setLastReleasedMessageSequenceNumber(lastReleasedSequenceRef.get());
    return messageGroup;
}
Also used : MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) AtomicReference(java.util.concurrent.atomic.AtomicReference) Date(java.util.Date) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup)

Example 74 with MessageGroup

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

Example 75 with MessageGroup

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

Aggregations

MessageGroup (org.springframework.integration.store.MessageGroup)98 Test (org.junit.Test)79 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)54 GenericMessage (org.springframework.messaging.support.GenericMessage)36 MessageGroupStore (org.springframework.integration.store.MessageGroupStore)25 Message (org.springframework.messaging.Message)20 ArrayList (java.util.ArrayList)19 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)15 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)14 MongoDbAvailable (org.springframework.integration.mongodb.rules.MongoDbAvailable)13 AbstractBatchingMessageGroupStore (org.springframework.integration.store.AbstractBatchingMessageGroupStore)13 MongoClient (com.mongodb.MongoClient)12 SimpleMongoDbFactory (org.springframework.data.mongodb.core.SimpleMongoDbFactory)12 Transactional (org.springframework.transaction.annotation.Transactional)10 LinkedList (java.util.LinkedList)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 UUID (java.util.UUID)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 HashMap (java.util.HashMap)5