added NamedComponent interface, continuing simplification of MessageHistory

This commit is contained in:
Mark Fisher
2010-04-30 20:11:18 +00:00
parent 6ef9eece71
commit 43dd81ff45
20 changed files with 131 additions and 76 deletions

View File

@@ -56,13 +56,6 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport im
private final ChannelInterceptorList interceptors = new ChannelInterceptorList();
/**
* Return the name of this channel.
*/
public String getName() {
return this.getBeanName();
}
@Override
public String getComponentType() {
return "channel";
@@ -169,7 +162,7 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport im
Assert.notNull(message, "message must not be null");
Assert.notNull(message.getPayload(), "message payload must not be null");
message = this.convertPayloadIfNecessary(message);
message.getHeaders().getHistory().addEvent(this.getBeanName(), this.getComponentType());
message.getHeaders().getHistory().addEvent(this);
message = this.interceptors.preSend(message, this);
if (message == null) {
return false;
@@ -188,7 +181,7 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport im
throw (MessagingException) e;
}
throw new MessageDeliveryException(message,
"failed to send Message to channel '" + this.getName() + "'", e);
"failed to send Message to channel '" + this.getComponentName() + "'", e);
}
}
@@ -209,7 +202,7 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport im
}
}
}
throw new MessageDeliveryException(message, "Channel '" + this.getName() +
throw new MessageDeliveryException(message, "Channel '" + this.getComponentName() +
"' expected one of the following datataypes [" +
StringUtils.arrayToCommaDelimitedString(this.datatypes) +
"], but received [" + message.getPayload().getClass() + "]");

View File

@@ -24,7 +24,6 @@ import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NamedBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.integration.channel.BeanFactoryChannelResolver;
import org.springframework.integration.channel.ChannelResolver;
@@ -42,7 +41,7 @@ import org.springframework.util.Assert;
*
* @author Mark Fisher
*/
public abstract class IntegrationObjectSupport implements BeanNameAware, NamedBean, BeanFactoryAware, InitializingBean {
public abstract class IntegrationObjectSupport implements BeanNameAware, NamedComponent, BeanFactoryAware, InitializingBean {
/** Logger that is available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
@@ -62,7 +61,8 @@ public abstract class IntegrationObjectSupport implements BeanNameAware, NamedBe
this.beanName = beanName;
}
public final String getBeanName() {
public final String getComponentName() {
return this.beanName;
}
@@ -129,7 +129,7 @@ public abstract class IntegrationObjectSupport implements BeanNameAware, NamedBe
this.conversionService = IntegrationContextUtils.getConversionService(this.beanFactory);
if (this.conversionService == null && logger.isDebugEnabled()) {
logger.debug("Unable to attempt conversion of Message payload types. Component '" +
this.getBeanName() + "' has no explicit ConversionService reference, " +
this.getComponentName() + "' has no explicit ConversionService reference, " +
"and there is no 'integrationConversionService' bean within the context.");
}
}

View File

@@ -0,0 +1,29 @@
/*
* 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.context;
/**
* @author Mark Fisher
* @since 2.0
*/
public interface NamedComponent {
String getComponentName();
String getComponentType();
}

View File

@@ -50,7 +50,7 @@ public class EventDrivenConsumer extends AbstractEndpoint {
protected void doStart() {
synchronized (this.initializationMonitor) {
if (this.handlerInvocationChain == null) {
this.handlerInvocationChain = new HandlerInvocationChain(this.handler, this.getBeanName());
this.handlerInvocationChain = new HandlerInvocationChain(this.handler, this.getComponentName());
}
}
this.inputChannel.subscribe(this.handlerInvocationChain);

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.endpoint;
import org.springframework.core.Ordered;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.context.NamedComponent;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.MessageHandler;
@@ -33,16 +34,15 @@ class HandlerInvocationChain implements MessageHandler, Ordered {
private final MessageHandler handler;
private final String endpointName;
private final String handlerType;
private final EndpointNamedComponent namedComponent;
public HandlerInvocationChain(MessageHandler handler, String endpointName) {
this.handler = handler;
this.endpointName = endpointName;
this.handlerType = (handler instanceof IntegrationObjectSupport) ?
String handlerType = (handler instanceof IntegrationObjectSupport) ?
((IntegrationObjectSupport) this.handler).getComponentType() : null;
this.namedComponent = (endpointName != null) ?
new EndpointNamedComponent(endpointName, handlerType) : null;
}
@@ -52,10 +52,31 @@ class HandlerInvocationChain implements MessageHandler, Ordered {
}
public void handleMessage(Message<?> message) {
if (message != null && this.endpointName != null) {
message.getHeaders().getHistory().addEvent(this.endpointName, this.handlerType);
if (message != null && this.namedComponent != null) {
message.getHeaders().getHistory().addEvent(this.namedComponent);
}
this.handler.handleMessage(message);
}
private static class EndpointNamedComponent implements NamedComponent {
private final String componentName;
private final String componentType;
private EndpointNamedComponent(String componentName, String componentType) {
this.componentName = componentName;
this.componentType = componentType;
}
public String getComponentName() {
return this.componentName;
}
public String getComponentType() {
return this.componentType;
}
}
}

View File

@@ -50,7 +50,7 @@ public abstract class MessageProducerSupport extends AbstractEndpoint implements
protected boolean sendMessage(Message<?> message) {
if (message != null) {
message.getHeaders().getHistory().addEvent(this.getBeanName(), this.getComponentType());
message.getHeaders().getHistory().addEvent(this);
}
return this.channelTemplate.send(message, this.outputChannel);
}

View File

@@ -58,7 +58,7 @@ public class PollingConsumer extends AbstractPollingEndpoint {
protected void onInit() {
synchronized (this.initializationMonitor) {
if (!this.initialized) {
this.handlerInvocationChain = new HandlerInvocationChain(this.handler, this.getBeanName());
this.handlerInvocationChain = new HandlerInvocationChain(this.handler, this.getComponentName());
}
this.initialized = true;
}

View File

@@ -253,10 +253,11 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
private SimpleMessagingGateway createGatewayForMethod(Method method) {
SimpleMessagingGateway gateway = new SimpleMessagingGateway(
new ArgumentArrayMessageMapper(method, this.getBeanName()), new SimpleMessageMapper());
new ArgumentArrayMessageMapper(method), new SimpleMessageMapper());
if (this.getTaskScheduler() != null) {
gateway.setTaskScheduler(this.getTaskScheduler());
}
gateway.setBeanName(this.getComponentName());
Gateway gatewayAnnotation = method.getAnnotation(Gateway.class);
MessageChannel requestChannel = this.defaultRequestChannel;
MessageChannel replyChannel = this.defaultReplyChannel;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* 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.
@@ -70,7 +70,11 @@ public class SimpleMessagingGateway extends AbstractMessagingGateway {
@Override
protected Message<?> toMessage(Object object) {
try {
return this.inboundMapper.toMessage(object);
Message<?> message = this.inboundMapper.toMessage(object);
if (message != null) {
message.getHeaders().getHistory().addEvent(this);
}
return message;
}
catch (Exception e) {
if (e instanceof RuntimeException) {

View File

@@ -122,15 +122,11 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
private final List<MethodParameter> parameterList;
private final String gatewayName;
public ArgumentArrayMessageMapper(Method method, String gatewayName) {
public ArgumentArrayMessageMapper(Method method) {
Assert.notNull(method, "method must not be null");
Assert.notNull(gatewayName, "gatewayName must not be null");
this.method = method;
this.parameterList = this.getMethodParameterList(method);
this.gatewayName = gatewayName;
}
public Message<?> toMessage(Object[] arguments) {
@@ -140,11 +136,7 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
throw new IllegalArgumentException(prefix + " parameters provided for method [" + method +
"], expected " + this.parameterList.size() + " but received " + arguments.length + ".");
}
Message<?> message = this.mapArgumentsToMessage(arguments);
if (message != null) {
message.getHeaders().getHistory().addEvent(this.gatewayName, "gateway");
}
return message;
return this.mapArgumentsToMessage(arguments);
}
@SuppressWarnings("unchecked")

View File

@@ -22,6 +22,7 @@ import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.springframework.integration.context.NamedComponent;
import org.springframework.util.StringUtils;
/**
@@ -40,7 +41,9 @@ public class MessageHistory implements Iterable<MessageHistoryEvent>, Serializab
/**
* Add a new event with the provided component metadata.
*/
public MessageHistoryEvent addEvent(String name, String type) {
public MessageHistoryEvent addEvent(NamedComponent component) {
String name = component.getComponentName();
String type = component.getComponentType();
if (name != null && !StringUtils.startsWithIgnoreCase(name, "org.springframework")) {
MessageHistoryEvent event = new MessageHistoryEvent(name, type);
this.events.add(event);

View File

@@ -73,6 +73,7 @@ public class SimpleMessagingGatewayTests {
@Test
public void sendMessage() {
expect(messageMock.getHeaders()).andReturn(new MessageHeaders(null));
expect(requestChannel.send(messageMock)).andReturn(true);
replay(allmocks);
this.simpleMessagingGateway.send(messageMock);
@@ -81,6 +82,7 @@ public class SimpleMessagingGatewayTests {
@Test(expected=MessageDeliveryException.class)
public void sendMessage_failure() {
expect(messageMock.getHeaders()).andReturn(new MessageHeaders(null));
expect(requestChannel.send(messageMock)).andReturn(false);
replay(allmocks);
this.simpleMessagingGateway.send(messageMock);

View File

@@ -39,7 +39,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
@Test
public void toMessageWithPayload() throws Exception {
Method method = TestService.class.getMethod("sendPayload", String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> message = mapper.toMessage(new Object[] { "test" });
assertEquals("test", message.getPayload());
}
@@ -47,14 +47,14 @@ public class ArgumentArrayMessageMapperToMessageTests {
@Test(expected = IllegalArgumentException.class)
public void toMessageWithTooManyParameters() throws Exception {
Method method = TestService.class.getMethod("sendPayload", String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
mapper.toMessage(new Object[] { "test" , "oops" });
}
@Test(expected = IllegalArgumentException.class)
public void toMessageWithEmptyParameterArray() throws Exception {
Method method = TestService.class.getMethod("sendPayload", String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
mapper.toMessage(new Object[] {});
}
@@ -62,7 +62,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
public void toMessageWithPayloadAndHeader() throws Exception {
Method method = TestService.class.getMethod(
"sendPayloadAndHeader", String.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> message = mapper.toMessage(new Object[] { "test", "bar" });
assertEquals("test", message.getPayload());
assertEquals("bar", message.getHeaders().get("foo"));
@@ -72,7 +72,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
public void toMessageWithPayloadAndRequiredHeaderButNullValue() throws Exception {
Method method = TestService.class.getMethod(
"sendPayloadAndHeader", String.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
mapper.toMessage(new Object[] { "test", null });
}
@@ -80,7 +80,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
public void toMessageWithPayloadAndOptionalHeaderWithValueProvided() throws Exception {
Method method = TestService.class.getMethod(
"sendPayloadAndOptionalHeader", String.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> message = mapper.toMessage(new Object[] { "test", "bar" });
assertEquals("test", message.getPayload());
assertEquals("bar", message.getHeaders().get("foo"));
@@ -90,7 +90,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
public void toMessageWithPayloadAndOptionalHeaderWithNullValue() throws Exception {
Method method = TestService.class.getMethod(
"sendPayloadAndOptionalHeader", String.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> message = mapper.toMessage(new Object[] { "test", null });
assertEquals("test", message.getPayload());
assertNull(message.getHeaders().get("foo"));
@@ -100,7 +100,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
public void toMessageWithPayloadAndHeadersMap() throws Exception {
Method method = TestService.class.getMethod(
"sendPayloadAndHeadersMap", String.class, Map.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("abc", 123);
headers.put("def", 456);
@@ -114,7 +114,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
public void toMessageWithPayloadAndNullHeadersMap() throws Exception {
Method method = TestService.class.getMethod(
"sendPayloadAndHeadersMap", String.class, Map.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> message = mapper.toMessage(new Object[] { "test", null });
assertEquals("test", message.getPayload());
}
@@ -123,7 +123,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
public void toMessageWithPayloadAndHeadersMapWithNonStringKey() throws Exception {
Method method = TestService.class.getMethod(
"sendPayloadAndHeadersMap", String.class, Map.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Map<Integer, String> headers = new HashMap<Integer, String>();
headers.put(123, "abc");
mapper.toMessage(new Object[] { "test", headers });
@@ -132,7 +132,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
@Test
public void toMessageWithMessageParameter() throws Exception {
Method method = TestService.class.getMethod("sendMessage", Message.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> inputMessage = MessageBuilder.withPayload("test message").build();
Message<?> message = mapper.toMessage(new Object[] { inputMessage });
assertEquals("test message", message.getPayload());
@@ -141,7 +141,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
@Test
public void toMessageWithMessageParameterAndHeader() throws Exception {
Method method = TestService.class.getMethod("sendMessageAndHeader", Message.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> inputMessage = MessageBuilder.withPayload("test message").build();
Message<?> message = mapper.toMessage(new Object[] { inputMessage, "bar" });
assertEquals("test message", message.getPayload());
@@ -151,7 +151,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
@Test(expected = IllegalArgumentException.class)
public void toMessageWithMessageParameterAndRequiredHeaderButNullValue() throws Exception {
Method method = TestService.class.getMethod("sendMessageAndHeader", Message.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> inputMessage = MessageBuilder.withPayload("test message").build();
mapper.toMessage(new Object[] { inputMessage, null });
}
@@ -159,7 +159,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
@Test
public void toMessageWithMessageParameterAndOptionalHeaderWithValue() throws Exception {
Method method = TestService.class.getMethod("sendMessageAndOptionalHeader", Message.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> inputMessage = MessageBuilder.withPayload("test message").build();
Message<?> message = mapper.toMessage(new Object[] { inputMessage, "bar" });
assertEquals("test message", message.getPayload());
@@ -169,7 +169,7 @@ public class ArgumentArrayMessageMapperToMessageTests {
@Test
public void toMessageWithMessageParameterAndOptionalHeaderWithNull() throws Exception {
Method method = TestService.class.getMethod("sendMessageAndOptionalHeader", Message.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
Message<?> inputMessage = MessageBuilder.withPayload("test message").build();
Message<?> message = mapper.toMessage(new Object[] { inputMessage, null });
assertEquals("test message", message.getPayload());
@@ -179,14 +179,14 @@ public class ArgumentArrayMessageMapperToMessageTests {
@Test(expected = IllegalArgumentException.class)
public void noArgs() throws Exception {
Method method = TestService.class.getMethod("noArgs", new Class<?>[] {});
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
mapper.toMessage(new Object[] {});
}
@Test(expected = IllegalArgumentException.class)
public void onlyHeaders() throws Exception {
Method method = TestService.class.getMethod("onlyHeaders", String.class, String.class);
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method, "testGateway");
ArgumentArrayMessageMapper mapper = new ArgumentArrayMessageMapper(method);
mapper.toMessage(new Object[] { "abc", "def" });
}

View File

@@ -22,6 +22,8 @@ import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.integration.context.NamedComponent;
/**
* @author Oleg Zhurakousky
* @since 2.0
@@ -36,11 +38,19 @@ public class MessageHistoryTests {
public void testConcurrentModificationsOnObjectMethods() throws Exception{
final MessageHistory history = new MessageHistory();
final MessageHistory otherHistory = new MessageHistory();
final NamedComponent component = new NamedComponent() {
public String getComponentType() {
return "testType";
}
public String getComponentName() {
return "testName";
}
};
executor.execute(new Runnable() {
public void run() {
for (int i = 0; i < times; i++) {
history.addEvent("testName", "testType");
otherHistory.addEvent("testName", "testType");
history.addEvent(component);
otherHistory.addEvent(component);
}
}
});

View File

@@ -31,10 +31,10 @@ import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.channel.BeanFactoryChannelResolver;
import org.springframework.integration.channel.MessagePublishingErrorHandler;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.context.NamedComponent;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.endpoint.AbstractEndpoint;
@@ -129,12 +129,12 @@ public abstract class TestUtils {
}
public void registerChannel(String channelName, MessageChannel channel) {
if (channel instanceof AbstractMessageChannel && ((AbstractMessageChannel) channel).getName() != null) {
if (channel instanceof NamedComponent && ((NamedComponent) channel).getComponentName() != null) {
if (channelName == null) {
channelName = ((AbstractMessageChannel) channel).getName();
channelName = ((NamedComponent) channel).getComponentName();
}
else {
Assert.isTrue(((AbstractMessageChannel) channel).getName().equals(channelName),
Assert.isTrue(((NamedComponent) channel).getComponentName().equals(channelName),
"channel name has already been set with a conflicting value");
}
}