Search in sources :

Example 86 with MessageGroup

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

the class MySqlJdbcMessageStoreTests method testMessagePollingFromTheGroup.

@Test
@Transactional
public void testMessagePollingFromTheGroup() throws Exception {
    final String groupX = "X";
    messageStore.addMessageToGroup(groupX, MessageBuilder.withPayload("foo").setCorrelationId(groupX).build());
    Thread.sleep(100);
    messageStore.addMessageToGroup(groupX, MessageBuilder.withPayload("bar").setCorrelationId(groupX).build());
    Thread.sleep(100);
    messageStore.addMessageToGroup(groupX, MessageBuilder.withPayload("baz").setCorrelationId(groupX).build());
    Thread.sleep(100);
    messageStore.addMessageToGroup("Y", MessageBuilder.withPayload("barA").setCorrelationId(groupX).build());
    Thread.sleep(100);
    messageStore.addMessageToGroup("Y", MessageBuilder.withPayload("bazA").setCorrelationId(groupX).build());
    Thread.sleep(100);
    MessageGroup group = messageStore.getMessageGroup(groupX);
    assertEquals(3, group.size());
    Message<?> message1 = messageStore.pollMessageFromGroup(groupX);
    assertNotNull(message1);
    assertEquals("foo", message1.getPayload());
    group = messageStore.getMessageGroup(groupX);
    assertEquals(2, group.size());
    Message<?> message2 = messageStore.pollMessageFromGroup(groupX);
    assertNotNull(message2);
    assertEquals("bar", message2.getPayload());
    group = messageStore.getMessageGroup(groupX);
    assertEquals(1, group.size());
}
Also used : MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 87 with MessageGroup

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

the class JdbcMessageStoreTests method testExpireMessageGroupOnCreateOnly.

@Test
public void testExpireMessageGroupOnCreateOnly() throws Exception {
    final String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(groupId).build();
    messageStore.addMessagesToGroup(groupId, message);
    final CountDownLatch groupRemovalLatch = new CountDownLatch(1);
    messageStore.registerMessageGroupExpiryCallback((messageGroupStore, group) -> {
        messageGroupStore.removeMessageGroup(group.getGroupId());
        groupRemovalLatch.countDown();
    });
    messageStore.expireMessageGroups(2000);
    MessageGroup group = messageStore.getMessageGroup(groupId);
    assertEquals(1, group.size());
    messageStore.addMessagesToGroup(groupId, MessageBuilder.withPayload("bar").setCorrelationId(groupId).build());
    JdbcTemplate template = new JdbcTemplate(this.dataSource);
    template.afterPropertiesSet();
    template.update("UPDATE INT_MESSAGE_GROUP set CREATED_DATE=? where GROUP_KEY=? and REGION=?", (PreparedStatementSetter) ps -> {
        ps.setTimestamp(1, new Timestamp(System.currentTimeMillis() - 10000));
        ps.setString(2, UUIDConverter.getUUID(groupId).toString());
        ps.setString(3, "DEFAULT");
    });
    messageStore.expireMessageGroups(2000);
    group = messageStore.getMessageGroup(groupId);
    assertEquals(0, group.size());
    assertTrue(groupRemovalLatch.await(10, TimeUnit.SECONDS));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) Assert.assertNotSame(org.junit.Assert.assertNotSame) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) MessageHistory(org.springframework.integration.history.MessageHistory) DataSource(javax.sql.DataSource) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Message(org.springframework.messaging.Message) Before(org.junit.Before) Properties(java.util.Properties) Assert.assertNotNull(org.junit.Assert.assertNotNull) Timestamp(java.sql.Timestamp) MessageGroup(org.springframework.integration.store.MessageGroup) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) UUIDConverter(org.springframework.integration.util.UUIDConverter) UUID(java.util.UUID) InputStreamReader(java.io.InputStreamReader) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) PayloadAndHeaderMatcher.sameExceptIgnorableHeaders(org.springframework.integration.test.matcher.PayloadAndHeaderMatcher.sameExceptIgnorableHeaders) ContextConfiguration(org.springframework.test.context.ContextConfiguration) BufferedReader(java.io.BufferedReader) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) Assert.assertEquals(org.junit.Assert.assertEquals) Transactional(org.springframework.transaction.annotation.Transactional) MessageGroup(org.springframework.integration.store.MessageGroup) CountDownLatch(java.util.concurrent.CountDownLatch) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 88 with MessageGroup

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

the class JdbcMessageStoreTests method testMessagePollingFromTheGroup.

@Test
public void testMessagePollingFromTheGroup() throws Exception {
    String groupId = "X";
    messageStore.addMessagesToGroup(groupId, MessageBuilder.withPayload("foo").setCorrelationId(groupId).build());
    messageStore.addMessagesToGroup(groupId, MessageBuilder.withPayload("bar").setCorrelationId(groupId).build());
    messageStore.addMessagesToGroup(groupId, MessageBuilder.withPayload("baz").setCorrelationId(groupId).build());
    messageStore.addMessagesToGroup("Y", MessageBuilder.withPayload("barA").setCorrelationId(groupId).build(), MessageBuilder.withPayload("bazA").setCorrelationId(groupId).build());
    MessageGroup group = messageStore.getMessageGroup("X");
    assertEquals(3, group.size());
    Message<?> message1 = messageStore.pollMessageFromGroup("X");
    assertNotNull(message1);
    assertEquals("foo", message1.getPayload());
    group = messageStore.getMessageGroup("X");
    assertEquals(2, group.size());
    Message<?> message2 = messageStore.pollMessageFromGroup("X");
    assertNotNull(message2);
    assertEquals("bar", message2.getPayload());
    group = messageStore.getMessageGroup("X");
    assertEquals(1, group.size());
}
Also used : MessageGroup(org.springframework.integration.store.MessageGroup) Test(org.junit.Test)

Example 89 with MessageGroup

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

the class JdbcMessageStoreTests method testRemoveMessageGroup.

@Test
public void testRemoveMessageGroup() throws Exception {
    JdbcTemplate template = new JdbcTemplate(this.dataSource);
    template.afterPropertiesSet();
    String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(groupId).build();
    messageStore.addMessagesToGroup(groupId, message);
    messageStore.removeMessageGroup(groupId);
    MessageGroup group = messageStore.getMessageGroup(groupId);
    assertEquals(0, group.size());
    String uuidGroupId = UUIDConverter.getUUID(groupId).toString();
    assertTrue(template.queryForList("SELECT * from INT_GROUP_TO_MESSAGE where GROUP_KEY = ?", uuidGroupId).size() == 0);
}
Also used : MessageGroup(org.springframework.integration.store.MessageGroup) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 90 with MessageGroup

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

the class JdbcMessageStoreTests method testExpireMessageGroupOnIdleOnly.

@Test
public void testExpireMessageGroupOnIdleOnly() throws Exception {
    String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(groupId).build();
    messageStore.setTimeoutOnIdle(true);
    messageStore.addMessagesToGroup(groupId, message);
    messageStore.registerMessageGroupExpiryCallback((messageGroupStore, group) -> messageGroupStore.removeMessageGroup(group.getGroupId()));
    JdbcTemplate template = new JdbcTemplate(this.dataSource);
    template.afterPropertiesSet();
    updateMessageGroup(template, groupId, 1000);
    messageStore.expireMessageGroups(2000);
    MessageGroup group = messageStore.getMessageGroup(groupId);
    assertEquals(1, group.size());
    updateMessageGroup(template, groupId, 2000);
    messageStore.addMessagesToGroup(groupId, MessageBuilder.withPayload("bar").setCorrelationId(groupId).build());
    group = messageStore.getMessageGroup(groupId);
    assertEquals(2, group.size());
    updateMessageGroup(template, groupId, 2000);
    messageStore.expireMessageGroups(2000);
    group = messageStore.getMessageGroup(groupId);
    assertEquals(0, group.size());
}
Also used : MessageGroup(org.springframework.integration.store.MessageGroup) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) 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