OPEN - issue INT-1063: MessageStore: correlation and grouping API

https://jira.springsource.org/browse/INT-1063
This commit is contained in:
David Syer
2010-04-28 08:00:00 +00:00
parent d634a4a423
commit 548e81bb7c
8 changed files with 178 additions and 71 deletions

View File

@@ -16,7 +16,7 @@
package org.springframework.integration.aggregator;
import java.util.List;
import java.util.Collection;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
@@ -30,7 +30,6 @@ import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.Lifecycle;
import org.springframework.integration.channel.ChannelResolver;
import org.springframework.integration.channel.MessageChannelTemplate;
@@ -293,11 +292,11 @@ public class CorrelatingMessageHandler extends AbstractMessageHandler implements
protected final boolean forceComplete(Object key) {
try {
if (tracker.tryLockFor(key)) {
List<Message<?>> all = store.list(key);
Collection<Message<?>> all = store.list(key);
MessageGroup group = new MessageGroup(all, completionStrategy, key, deleteOrTrackCallback());
if (all.size() > 0) {
// last chance for normal completion
MessageChannel outputChannel = resolveReplyChannel(all.get(0), this.outputChannel);
MessageChannel outputChannel = resolveReplyChannel(all.iterator().next(), this.outputChannel);
boolean processed = false;
if (group.isComplete()) {
outputProcessor.processAndSend(group, channelTemplate, outputChannel);

View File

@@ -16,7 +16,7 @@
package org.springframework.integration.store;
import java.util.List;
import java.util.Collection;
import java.util.UUID;
import org.springframework.integration.core.Message;
@@ -63,6 +63,6 @@ public interface MessageStore {
* provided correlationId header value.
* @see org.springframework.integration.core.MessageHeaders#getCorrelationId()
*/
List<Message<?>> list(Object correlationId);
Collection<Message<?>> list(Object correlationId);
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright 2002-2008 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.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.ClassUtils;
/**
* Utility to help generate UUID instances from generic objects.
*
* @author Dave Syer
*
*/
public class UUIDConverter implements Converter<Object, UUID> {
public static final String DEFAULT_CHARSET = "UTF-8";
private final String charset;
public UUIDConverter() {
this(DEFAULT_CHARSET);
}
public UUIDConverter(String charset) {
this.charset = charset;
}
public UUID convert(Object source) {
return getUUID(source, charset);
}
public static UUID getUUID(Object input) {
return getUUID(input, DEFAULT_CHARSET);
}
public static UUID getUUID(Object input, String charset) {
if (input == null) {
return null;
}
if (input instanceof UUID) {
return (UUID) input;
}
if (input instanceof String) {
try {
return UUID.fromString((String) input);
}
catch (IllegalArgumentException e) {
try {
return UUID.nameUUIDFromBytes(((String) input).getBytes(charset));
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException("Cannot convert String using charset=" + charset, ex);
}
}
}
if (ClassUtils.isPrimitiveOrWrapper(input.getClass())) {
try {
return UUID.nameUUIDFromBytes(input.toString().getBytes(charset));
}
catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Cannot convert primitive using charset=" + charset, e);
}
}
byte[] bytes = serialize(input);
return UUID.nameUUIDFromBytes(bytes);
}
private static byte[] serialize(Object object) {
if (object == null) {
return null;
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
new ObjectOutputStream(stream).writeObject(object);
}
catch (IOException e) {
throw new IllegalArgumentException("Could not serialize object of type: " + object.getClass(), e);
}
return stream.toByteArray();
}
}