Example usage for org.springframework.integration.store MessageGroup getGroupId

List of usage examples for org.springframework.integration.store MessageGroup getGroupId

Introduction

In this page you can find the example usage for org.springframework.integration.store MessageGroup getGroupId.

Prototype

Object getGroupId();

Source Link

Usage

From source file:org.springframework.integration.aggregator.AbstractAggregatingMessageGroupProcessor.java

/**
 * This default implementation simply returns all headers that have no conflicts among the group. An absent header
 * on one or more Messages within the group is not considered a conflict. Subclasses may override this method with
 * more advanced conflict-resolution strategies if necessary.
 *//* ww w. j  a  va2s  . c o  m*/
protected Map<String, Object> aggregateHeaders(MessageGroup group) {
    Map<String, Object> aggregatedHeaders = new HashMap<String, Object>();
    Set<String> conflictKeys = new HashSet<String>();
    for (Message<?> message : group.getMessages()) {
        MessageHeaders currentHeaders = message.getHeaders();
        for (String key : currentHeaders.keySet()) {
            if (MessageHeaders.ID.equals(key) || MessageHeaders.TIMESTAMP.equals(key)
                    || MessageHeaders.SEQUENCE_SIZE.equals(key) || MessageHeaders.SEQUENCE_NUMBER.equals(key)) {
                continue;
            }
            Object value = currentHeaders.get(key);
            if (!aggregatedHeaders.containsKey(key)) {
                aggregatedHeaders.put(key, value);
            } else if (!value.equals(aggregatedHeaders.get(key))) {
                conflictKeys.add(key);
            }
        }
    }
    for (String keyToRemove : conflictKeys) {
        if (logger.isDebugEnabled()) {
            logger.debug("Excluding header '" + keyToRemove + "' upon aggregation due to conflict(s) "
                    + "in MessageGroup with correlation key: " + group.getGroupId());
        }
        aggregatedHeaders.remove(keyToRemove);
    }
    return aggregatedHeaders;
}

From source file:org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler.java

private void forceComplete(MessageGroup group) {

    Object correlationKey = group.getGroupId();
    // UUIDConverter is no-op if already converted
    Lock lock = this.lockRegistry.obtain(UUIDConverter.getUUID(correlationKey).toString());
    boolean removeGroup = true;
    try {/*from  ww w  .j a  v  a2s  .c  o m*/
        lock.lockInterruptibly();
        try {
            /*
             * Refetch the group because it might have changed while we were waiting on
             * its lock. If the last modified timestamp changed, defer the completion
             * because the selection condition may have changed such that the group
             * would no longer be eligible.
             */
            MessageGroup groupNow = this.messageStore.getMessageGroup(group.getGroupId());
            long lastModifiedNow = groupNow.getLastModified();
            if (group.getLastModified() == lastModifiedNow) {
                if (groupNow.size() > 0) {
                    if (releaseStrategy.canRelease(groupNow)) {
                        this.completeGroup(correlationKey, groupNow);
                    } else {
                        this.expireGroup(correlationKey, groupNow);
                    }
                } else {
                    /*
                     * By default empty groups are removed on the same schedule as non-empty
                     * groups. A longer timeout for empty groups can be enabled by
                     * setting minimumTimeoutForEmptyGroups.
                     */
                    removeGroup = lastModifiedNow <= (System.currentTimeMillis()
                            - this.minimumTimeoutForEmptyGroups);
                    if (removeGroup && logger.isDebugEnabled()) {
                        logger.debug("Removing empty group: " + correlationKey);
                    }
                }
            } else {
                removeGroup = false;
                if (logger.isDebugEnabled()) {
                    logger.debug("Group expiry candidate (" + correlationKey
                            + ") has changed - it may be reconsidered for a future expiration");
                }
            }
        } finally {
            if (removeGroup) {
                this.remove(group);
            }
            lock.unlock();
        }
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new MessagingException("Thread was interrupted while trying to obtain lock");
    }
}

From source file:org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler.java

void remove(MessageGroup group) {
    Object correlationKey = group.getGroupId();
    messageStore.removeMessageGroup(correlationKey);
}

From source file:org.springframework.integration.aggregator.CorrelatingMessageHandler.java

private final boolean forceComplete(MessageGroup group) {

    Object correlationKey = group.getGroupId();
    Object lock = getLock(correlationKey);
    synchronized (lock) {

        if (group.size() > 0) {
            try {
                if (releaseStrategy.canRelease(group)) {
                    completeGroup(correlationKey, group);
                } else {
                    expireGroup(group, correlationKey);
                }//from  w ww  . jav  a  2s .co  m
            } finally {
                remove(group);
            }
            return true;
        }
        return false;
    }
}

From source file:org.springframework.integration.aggregator.CorrelatingMessageHandler.java

private void mark(MessageGroup group, Collection<Message> partialSequence) {
    Object id = group.getGroupId();
    for (Message message : partialSequence) {
        messageStore.markMessageFromGroup(id, message);
    }//from w w w.j a  v a 2s . c o  m
}

From source file:org.springframework.integration.aggregator.CorrelatingMessageHandler.java

private void remove(MessageGroup group) {
    Object correlationKey = group.getGroupId();
    messageStore.removeMessageGroup(correlationKey);
    locks.remove(correlationKey);/*from  w ww. j a va2  s .co m*/
}

From source file:org.springframework.integration.jdbc.JdbcMessageStoreTests.java

@Test
@Transactional/*from  w  ww. j a  v  a2 s.  c  om*/
public void testExpireMessageGroupOnCreateOnly() throws Exception {
    String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(groupId).build();
    messageStore.addMessageToGroup(groupId, message);
    messageStore.registerMessageGroupExpiryCallback(new MessageGroupCallback() {
        @Override
        public void execute(MessageGroupStore messageGroupStore, MessageGroup group) {
            messageGroupStore.removeMessageGroup(group.getGroupId());
        }
    });
    Thread.sleep(1000);
    messageStore.expireMessageGroups(2000);
    MessageGroup group = messageStore.getMessageGroup(groupId);
    assertEquals(1, group.size());
    messageStore.addMessageToGroup(groupId,
            MessageBuilder.withPayload("bar").setCorrelationId(groupId).build());
    Thread.sleep(2001);
    messageStore.expireMessageGroups(2000);
    group = messageStore.getMessageGroup(groupId);
    assertEquals(0, group.size());
}

From source file:org.springframework.integration.jdbc.JdbcMessageStoreTests.java

@Test
@Transactional/* ww w.  j ava  2s  .  c om*/
public void testExpireMessageGroupOnIdleOnly() throws Exception {
    String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(groupId).build();
    messageStore.setTimeoutOnIdle(true);
    messageStore.addMessageToGroup(groupId, message);
    messageStore.registerMessageGroupExpiryCallback(new MessageGroupCallback() {
        @Override
        public void execute(MessageGroupStore messageGroupStore, MessageGroup group) {
            messageGroupStore.removeMessageGroup(group.getGroupId());
        }
    });
    Thread.sleep(1000);
    messageStore.expireMessageGroups(2000);
    MessageGroup group = messageStore.getMessageGroup(groupId);
    assertEquals(1, group.size());
    Thread.sleep(2000);
    messageStore.addMessageToGroup(groupId,
            MessageBuilder.withPayload("bar").setCorrelationId(groupId).build());
    group = messageStore.getMessageGroup(groupId);
    assertEquals(2, group.size());
    Thread.sleep(2000);
    messageStore.expireMessageGroups(2000);
    group = messageStore.getMessageGroup(groupId);
    assertEquals(0, group.size());
}

From source file:org.springframework.integration.jdbc.JdbcMessageStoreTests.java

@Test
@Transactional//from w w w  .  ja va  2s  .c om
public void testCompletedNotExpiredGroupINT3037() throws Exception {
    /*
     * based on the aggregator scenario as follows;
     *
     * send three messages in
     * 1 of 2
     * 2 of 2
     * 2 of 2 (last again)
     *
     * expected behavior is that the LAST message (2 of 2 repeat) should be on the discard channel
     * (discard behavior performed by the AbstractCorrelatingMessageHandler.handleMessageInternal)
     */
    final JdbcMessageStore messageStore = new JdbcMessageStore(dataSource);
    //init
    String groupId = "group";
    //build the messages
    Message<?> oneOfTwo = MessageBuilder.withPayload("hello").setSequenceNumber(1).setSequenceSize(2)
            .setCorrelationId(groupId).build();
    Message<?> twoOfTwo = MessageBuilder.withPayload("world").setSequenceNumber(2).setSequenceSize(2)
            .setCorrelationId(groupId).build();
    //add to the messageStore
    messageStore.addMessageToGroup(groupId, oneOfTwo);
    messageStore.addMessageToGroup(groupId, twoOfTwo);
    //check that 2 messages are there
    assertTrue(messageStore.getMessageGroupCount() == 1);
    assertTrue(messageStore.getMessageCount() == 2);
    //retrieve the group (like in the aggregator)
    MessageGroup messageGroup = messageStore.getMessageGroup(groupId);
    //'complete' the group
    messageStore.completeGroup(messageGroup.getGroupId());
    //now clear the messages
    for (Message<?> message : messageGroup.getMessages()) {
        messageStore.removeMessageFromGroup(groupId, message);
    } //end for
      //'add' the other message --> emulated by getting the messageGroup
    messageGroup = messageStore.getMessageGroup(groupId);
    //should be marked 'complete' --> old behavior it would not
    assertTrue(messageGroup.isComplete());
}

From source file:org.springframework.integration.jdbc.mysql.MySqlJdbcMessageStoreTests.java

@Test
@Transactional//www. ja v  a2  s .com
public void testExpireMessageGroupOnCreateOnly() throws Exception {
    String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(groupId).build();
    messageStore.addMessageToGroup(groupId, message);
    messageStore.registerMessageGroupExpiryCallback(new MessageGroupCallback() {
        public void execute(MessageGroupStore messageGroupStore, MessageGroup group) {
            messageGroupStore.removeMessageGroup(group.getGroupId());
        }
    });
    Thread.sleep(1000);
    messageStore.expireMessageGroups(2000);
    MessageGroup group = messageStore.getMessageGroup(groupId);
    assertEquals(1, group.size());
    messageStore.addMessageToGroup(groupId,
            MessageBuilder.withPayload("bar").setCorrelationId(groupId).build());
    Thread.sleep(2001);
    messageStore.expireMessageGroups(2000);
    group = messageStore.getMessageGroup(groupId);
    assertEquals(0, group.size());
}