INT-1114: Add MessageGroupStore methods to JdbcMessageStore

This commit is contained in:
David Syer
2010-05-05 17:33:29 +00:00
parent 341a5c02f8
commit 6b1f18723b
7 changed files with 453 additions and 153 deletions

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.store;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author Dave Syer
*
* @since 2.0
*
*/
public abstract class AbstractMessageGroupStore implements MessageGroupStore, Iterable<MessageGroup> {
protected final Log logger = LogFactory.getLog(getClass());
private Collection<MessageGroupCallback> expiryCallbacks = new LinkedHashSet<MessageGroupCallback>();
/**
*
*/
public AbstractMessageGroupStore() {
super();
}
/**
* Convenient injection point for expiry callbacks in the message store. Each of the callbacks provided will simply
* be registered with the store using {@link #registerExpiryCallback(MessageGroupCallback)}.
*
* @param expiryCallbacks the expiry callbacks to add
*/
public void setExpiryCallbacks(Collection<MessageGroupCallback> expiryCallbacks) {
for (MessageGroupCallback callback : expiryCallbacks) {
registerExpiryCallback(callback);
}
}
public void registerExpiryCallback(MessageGroupCallback callback) {
expiryCallbacks.add(callback);
}
public int expireMessageGroups(long timeout) {
int count = 0;
long threshold = System.currentTimeMillis() - timeout;
for (MessageGroup group : this) {
if (group.getTimestamp() < threshold) {
count++;
expire(group);
removeMessageGroup(group.getCorrelationKey());
}
}
return count;
}
public abstract Iterator<MessageGroup> iterator();
private void expire(MessageGroup group) {
RuntimeException exception = null;
for (MessageGroupCallback callback : expiryCallbacks) {
try {
callback.execute(group);
} catch (RuntimeException e) {
if (exception == null) {
exception = e;
}
logger.error("Exception in expiry callback", e);
}
}
if (exception != null) {
throw exception;
}
}
}

View File

@@ -14,15 +14,15 @@
package org.springframework.integration.store;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.springframework.integration.core.Message;
/**
* Represents a mutable group of correlated messages that is bound to a certain
* {@link MessageStore} and correlation key. The group will grow during its
* lifetime, when messages are <code>add</code>ed to it. <strong>This is not
* thread safe and should not be used for long running aggregations</strong>.
* Represents a mutable group of correlated messages that is bound to a certain {@link MessageStore} and correlation
* key. The group will grow during its lifetime, when messages are <code>add</code>ed to it. <strong>This is not thread
* safe and should not be used for long running aggregations</strong>.
*
* @author Iwein Fuld
* @author Oleg Zhurakousky
@@ -41,18 +41,24 @@ public class SimpleMessageGroup implements MessageGroup {
private final long timestamp;
public SimpleMessageGroup(Object correlationKey) {
this.correlationKey = correlationKey;
this.timestamp = System.currentTimeMillis();
this(Collections.<Message<?>>emptyList(), Collections.<Message<?>>emptyList(), correlationKey, System.currentTimeMillis());
}
public SimpleMessageGroup(Collection<? extends Message<?>> originalMessages,
Object correlationKey) {
this(correlationKey);
for (Message<?> message : originalMessages) {
add(message);
public SimpleMessageGroup(Collection<? extends Message<?>> unmarked, Object correlationKey) {
this(unmarked, Collections.<Message<?>>emptyList(), correlationKey, System.currentTimeMillis());
}
public SimpleMessageGroup(Collection<? extends Message<?>> unmarked, Collection<? extends Message<?>> marked, Object correlationKey, long timestamp) {
this.correlationKey = correlationKey;
this.timestamp = timestamp;
for (Message<?> message : unmarked) {
addUnmarked(message);
}
for (Message<?> message : marked) {
addMarked(message);
}
}
public SimpleMessageGroup(MessageGroup template) {
this.correlationKey = template.getCorrelationKey();
this.marked.addAll(template.getMarked());
@@ -65,6 +71,10 @@ public class SimpleMessageGroup implements MessageGroup {
}
public boolean add(Message<?> message) {
return addUnmarked(message);
}
private boolean addUnmarked(Message<?> message) {
if (isMember(message)) {
return false;
}
@@ -72,6 +82,14 @@ public class SimpleMessageGroup implements MessageGroup {
return true;
}
private boolean addMarked(Message<?> message) {
if (isMember(message)) {
return false;
}
this.marked.add(message);
return true;
}
public Collection<Message<?>> getUnmarked() {
return unmarked;
}
@@ -110,25 +128,21 @@ public class SimpleMessageGroup implements MessageGroup {
}
public Message<?> getOne() {
return unmarked.isEmpty() ? (marked.isEmpty() ? null : marked
.iterator().next()) : unmarked.iterator().next();
return unmarked.isEmpty() ? (marked.isEmpty() ? null : marked.iterator().next()) : unmarked.iterator().next();
}
/**
* This method determines whether messages have been added to this group
* that supersede the given message based on its sequence id. This can be
* helpful to avoid ending up with sequences larger than their required
* sequence size or sequences that are missing certain sequence numbers.
* This method determines whether messages have been added to this group that supersede the given message based on
* its sequence id. This can be helpful to avoid ending up with sequences larger than their required sequence size
* or sequences that are missing certain sequence numbers.
*/
private boolean isMember(Message<?> message) {
if (size() == 0) {
return false;
}
Integer messageSequenceNumber = message.getHeaders()
.getSequenceNumber();
Integer messageSequenceNumber = message.getHeaders().getSequenceNumber();
if (messageSequenceNumber != null && messageSequenceNumber > 0) {
Integer messageSequenceSize = message.getHeaders()
.getSequenceSize();
Integer messageSequenceSize = message.getHeaders().getSequenceSize();
if (!messageSequenceSize.equals(getSequenceSize())
|| containsSequenceNumber(unmarked, messageSequenceNumber)
|| containsSequenceNumber(marked, messageSequenceNumber)) {
@@ -138,11 +152,9 @@ public class SimpleMessageGroup implements MessageGroup {
return false;
}
private boolean containsSequenceNumber(Collection<Message<?>> messages,
Integer messageSequenceNumber) {
private boolean containsSequenceNumber(Collection<Message<?>> messages, Integer messageSequenceNumber) {
for (Message<?> member : messages) {
Integer memberSequenceNumber = member.getHeaders()
.getSequenceNumber();
Integer memberSequenceNumber = member.getHeaders().getSequenceNumber();
if (messageSequenceNumber.equals(memberSequenceNumber)) {
return true;
}

View File

@@ -13,14 +13,12 @@
package org.springframework.integration.store;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessagingException;
import org.springframework.integration.util.UpperBound;
@@ -36,20 +34,16 @@ import org.springframework.util.Assert;
*
* @since 2.0
*/
public class SimpleMessageStore implements MessageStore, MessageGroupStore {
private static final Log logger = LogFactory.getLog(SimpleMessageStore.class);
public class SimpleMessageStore extends AbstractMessageGroupStore implements MessageStore, MessageGroupStore {
private final ConcurrentMap<UUID, Message<?>> idToMessage;
private final ConcurrentMap<Object, SimpleMessageGroup> correlationToMessageGroup;
final ConcurrentMap<Object, SimpleMessageGroup> correlationToMessageGroup;
private final UpperBound individualUpperBound;
private final UpperBound groupUpperBound;
private Collection<MessageGroupCallback> expiryCallbacks = new LinkedHashSet<MessageGroupCallback>();
/**
* Creates a SimpleMessageStore with a maximum size limited by the given capacity, or unlimited size if the given
* capacity is less than 1. The capacities are applied independently to messages stored via
@@ -78,18 +72,6 @@ public class SimpleMessageStore implements MessageStore, MessageGroupStore {
this(0);
}
/**
* Convenient injection point for expiry callbacks in the message store. Each of the callbacks provided will simply
* be registered with the store using {@link #registerExpiryCallback(MessageGroupCallback)}.
*
* @param expiryCallbacks the expiry callbacks to add
*/
public void setExpiryCallbacks(Collection<MessageGroupCallback> expiryCallbacks) {
for (MessageGroupCallback callback : expiryCallbacks) {
registerExpiryCallback(callback);
}
}
public <T> Message<T> addMessage(Message<T> message) {
if (!individualUpperBound.tryAcquire(0)) {
throw new MessagingException(this.getClass().getSimpleName()
@@ -140,42 +122,9 @@ public class SimpleMessageStore implements MessageStore, MessageGroupStore {
correlationToMessageGroup.remove(correlationId);
}
public void registerExpiryCallback(MessageGroupCallback callback) {
expiryCallbacks.add(callback);
}
public int expireMessageGroups(long timeout) {
int count = 0;
long threshold = System.currentTimeMillis() - timeout;
for (MessageGroup group : correlationToMessageGroup.values()) {
if (group.getTimestamp() < threshold) {
count++;
expire(group);
removeMessageGroup(group.getCorrelationKey());
}
}
return count;
}
private void expire(MessageGroup group) {
RuntimeException exception = null;
for (MessageGroupCallback callback : expiryCallbacks) {
try {
callback.execute(group);
} catch (RuntimeException e) {
if (exception == null) {
exception = e;
}
logger.error("Exception in expiry callback", e);
}
}
if (exception != null) {
throw exception;
}
@Override
public Iterator<MessageGroup> iterator() {
return new HashSet<MessageGroup>(correlationToMessageGroup.values()).iterator();
}
private SimpleMessageGroup getMessageGroupInternal(Object correlationId) {

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.store;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.test.util.ReflectionTestUtils;
/**
* @author Dave Syer
*/
public class MessageStoreTests {
@Test
public void shouldRegisterCallbacks() throws Exception {
TestMessageStore store = new TestMessageStore();
store.setExpiryCallbacks(Arrays.<MessageGroupCallback>asList(new MessageGroupCallback() {
public void execute(MessageGroup group) {
}
}));
assertEquals(1, ((Collection<?>)ReflectionTestUtils.getField(store, "expiryCallbacks")).size());
}
@Test
public void shouldExpireMessageGroup() throws Exception {
TestMessageStore store = new TestMessageStore();
final List<String> list = new ArrayList<String>();
store.registerExpiryCallback(new MessageGroupCallback() {
public void execute(MessageGroup group) {
list.add(group.getOne().getPayload().toString());
}
});
store.expireMessageGroups(-10000);
assertEquals("[foo]", list.toString());
assertEquals(0, store.getMessageGroup("bar").size());
}
private static class TestMessageStore extends AbstractMessageGroupStore {
MessageGroup testMessages = new SimpleMessageGroup(Arrays.asList(new StringMessage("foo")), "bar");
private boolean removed = false;
@Override
public Iterator<MessageGroup> iterator() {
return Arrays.asList(testMessages).iterator();
}
public void addMessageToGroup(Object correlationKey, Message<?> message) {
}
public MessageGroup getMessageGroup(Object correlationKey) {
return removed ? new SimpleMessageGroup(correlationKey) : testMessages;
}
public void markMessageGroup(MessageGroup group) {
}
public void removeMessageGroup(Object correlationKey) {
if (correlationKey.equals(testMessages.getCorrelationKey())) {
removed = true;
}
}
}
}