Skip to content

Commit

Permalink
INT-2433: Get Rid of MessageProcessors wrapping
Browse files Browse the repository at this point in the history
Previously, provided `MessageProcessor` to create a `MessageHandler`
for endpoint was wrapped to `MethodInvokingMessageProcessor`,
e.g. `GroovyScriptExecutingMessageProcessor`.

Check the type of provided object in the constructor
of `MethodInvoking*` strategies and use it directly if it is `MessageProcessor`

JIRA: https://jira.springsource.org/browse/INT-2433
  • Loading branch information
Artem Bilan authored and garyrussell committed Sep 18, 2013
1 parent 417c848 commit 4d3bc36
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 29 deletions.
2 changes: 1 addition & 1 deletion publish-maven.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def customizePom(pom, gradleProject) {
developer {
id = 'markfisher'
name = 'Mark Fisher'
email = 'markfisher@gopivotal.com'
email = 'mfisher@gopivotal.com'
roles = ["project founder and lead emeritus"]
}
developer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @author Mark Fisher
* @author Alexander Peters
* @author Gary Russell
* @author Artem Bilan
*/
abstract class AbstractStandardMessageHandlerFactoryBean extends AbstractSimpleMessageHandlerFactoryBean<MessageHandler> {

Expand Down Expand Up @@ -135,7 +136,7 @@ MessageHandler createExpressionEvaluatingHandler(Expression expression) {
}

<T> MessageHandler createMessageProcessingHandler(MessageProcessor<T> processor) {
return this.createMethodInvokingHandler(processor, "processMessage");
return this.createMethodInvokingHandler(processor, null);
}

MessageHandler createDefaultHandler() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -20,13 +20,15 @@

import org.springframework.integration.annotation.Filter;
import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;
import org.springframework.util.Assert;

/**
* A method-invoking implementation of {@link MessageSelector}.
*
*
* @author Mark Fisher
* @author Artem Bilan
*/
public class MethodInvokingSelector extends AbstractMessageProcessingSelector {

Expand All @@ -42,8 +44,10 @@ public MethodInvokingSelector(Object object, String methodName) {
super(new MethodInvokingMessageProcessor<Boolean>(object, methodName));
}

@SuppressWarnings("unchecked")
public MethodInvokingSelector(Object object) {
super(new MethodInvokingMessageProcessor<Boolean>(object, Filter.class));
super(object instanceof MessageProcessor<?> ? (MessageProcessor<Boolean>) object :
new MethodInvokingMessageProcessor<Boolean>(object, Filter.class));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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.
Expand Down Expand Up @@ -28,16 +28,16 @@
/**
* A base class for Router implementations that delegate to a
* {@link MessageProcessor} instance.
*
*
* @author Mark Fisher
* @since 2.0
*/
class AbstractMessageProcessingRouter extends AbstractMappingMessageRouter {

private final MessageProcessor<Object> messageProcessor;
private final MessageProcessor<?> messageProcessor;


AbstractMessageProcessingRouter(MessageProcessor<Object> messageProcessor) {
AbstractMessageProcessingRouter(MessageProcessor<?> messageProcessor) {
Assert.notNull(messageProcessor, "messageProcessor must not be null");
this.messageProcessor = messageProcessor;
}
Expand All @@ -47,7 +47,7 @@ class AbstractMessageProcessingRouter extends AbstractMappingMessageRouter {
public final void onInit() {
super.onInit();
if (this.messageProcessor instanceof AbstractMessageProcessor) {
((AbstractMessageProcessor<Object>) this.messageProcessor).setConversionService(this.getConversionService());
((AbstractMessageProcessor<?>) this.messageProcessor).setConversionService(this.getConversionService());
}
if (this.messageProcessor instanceof BeanFactoryAware) {
((BeanFactoryAware) this.messageProcessor).setBeanFactory(this.getBeanFactory());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -19,6 +19,7 @@
import java.lang.reflect.Method;

import org.springframework.integration.annotation.Router;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;
import org.springframework.integration.support.channel.ChannelResolver;

Expand All @@ -28,8 +29,9 @@
* String to be interpreted as a channel name, or a Collection (or Array) of
* either type. If the method returns channel names, then a
* {@link ChannelResolver} is required.
*
*
* @author Mark Fisher
* @author Artem Bilan
*/
public class MethodInvokingRouter extends AbstractMessageProcessingRouter {

Expand All @@ -42,7 +44,8 @@ public MethodInvokingRouter(Object object, String methodName) {
}

public MethodInvokingRouter(Object object) {
super(new MethodInvokingMessageProcessor<Object>(object, Router.class));
super(object instanceof MessageProcessor<?> ? (MessageProcessor<?>) object :
new MethodInvokingMessageProcessor<Object>(object, Router.class));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -20,6 +20,7 @@
import java.util.Collection;

import org.springframework.integration.annotation.Splitter;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;

/**
Expand All @@ -28,8 +29,9 @@
* is a Collection or Array. If the return value is not a Collection or
* Array, then the single Object will be returned as the payload of a
* single reply Message.
*
*
* @author Mark Fisher
* @author Artem Bilan
*/
public class MethodInvokingSplitter extends AbstractMessageProcessingSplitter {

Expand All @@ -41,8 +43,10 @@ public MethodInvokingSplitter(Object object, String methodName) {
super(new MethodInvokingMessageProcessor<Collection<?>>(object, methodName));
}

@SuppressWarnings("unchecked")
public MethodInvokingSplitter(Object object) {
super(new MethodInvokingMessageProcessor<Collection<?>>(object, Splitter.class));
super(object instanceof MessageProcessor<?> ? (MessageProcessor<Collection<?>>) object :
new MethodInvokingMessageProcessor<Collection<?>>(object, Splitter.class));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -19,15 +19,17 @@
import java.lang.reflect.Method;

import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.handler.MethodInvokingMessageProcessor;

/**
* A Message Transformer implementation that invokes the specified method
* on the given object. The method's return value will be considered as
* the payload of a new Message unless the return value is itself already
* a Message.
*
*
* @author Mark Fisher
* @author Artem Bilan
*/
public class MethodInvokingTransformer extends AbstractMessageProcessingTransformer {

Expand All @@ -40,7 +42,8 @@ public MethodInvokingTransformer(Object object, String methodName) {
}

public MethodInvokingTransformer(Object object) {
super(new MethodInvokingMessageProcessor<Object>(object, Transformer.class));
super(object instanceof MessageProcessor<?> ? (MessageProcessor<?>) object :
new MethodInvokingMessageProcessor<Object>(object, Transformer.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<filter input-channel="referencedScriptInput">
<filter id="groovyFilter" input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovyFilterTests.groovy"/>
</filter>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -19,20 +19,30 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.filter.MessageFilter;
import org.springframework.integration.filter.MethodInvokingSelector;
import org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* @author Mark Fisher
* @author Artem Bilan
* @since 2.0
*/
@ContextConfiguration
Expand All @@ -45,6 +55,9 @@ public class GroovyFilterTests {
@Autowired
private MessageChannel inlineScriptInput;

@Autowired
@Qualifier("groovyFilter.handler")
private MessageHandler groovyFilterMessageHandler;

@Test
public void referencedScript() {
Expand All @@ -57,7 +70,7 @@ public void referencedScript() {
Message<?> message2 = MessageBuilder.withPayload("test-2")
.setReplyChannel(replyChannel)
.setHeader("type", "good")
.build();
.build();
this.referencedScriptInput.send(message1);
this.referencedScriptInput.send(message2);
assertEquals("test-2", replyChannel.receive(0).getPayload());
Expand All @@ -69,7 +82,7 @@ public void inlineScript() {
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("returnAddress");
Message<?> message1 = MessageBuilder.withPayload("bad").setReplyChannel(replyChannel).build();
Message<?> message2 = MessageBuilder.withPayload("good").setReplyChannel(replyChannel).build();
Message<?> message2 = MessageBuilder.withPayload("good").setReplyChannel(replyChannel).build();
this.inlineScriptInput.send(message1);
this.inlineScriptInput.send(message2);
Message<?> received = replyChannel.receive(0);
Expand All @@ -79,4 +92,14 @@ public void inlineScript() {
assertNull(replyChannel.receive(0));
}

@Test
public void testInt2433VerifyRiddingOfMessageProcessorsWrapping() {
assertTrue(this.groovyFilterMessageHandler instanceof MessageFilter);
MessageSelector selector = TestUtils.getPropertyValue(this.groovyFilterMessageHandler, "selector",
MethodInvokingSelector.class);
MessageProcessor messageProcessor = TestUtils.getPropertyValue(selector, "messageProcessor", MessageProcessor.class);
//before it was MethodInvokingMessageProcessor
assertTrue(messageProcessor instanceof GroovyScriptExecutingMessageProcessor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<queue/>
</channel>

<router input-channel="referencedScriptInput">
<router id="groovyRouter" input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovyRouterTests.groovy"/>
</router>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -18,20 +18,28 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.router.MethodInvokingRouter;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* @author Mark Fisher
* @author Artem Bilan
* @since 2.0
*/
@ContextConfiguration
Expand All @@ -50,6 +58,9 @@ public class GroovyRouterTests {
@Autowired
private PollableChannel shortStrings;

@Autowired
@Qualifier("groovyRouter.handler")
private MessageHandler groovyRouterMessageHandler;

@Test
public void referencedScript() { // long is > 3
Expand Down Expand Up @@ -93,4 +104,13 @@ public void inlineScript() { // long is > 5
assertNull(longStrings.receive(0));
}

@Test
public void testInt2433VerifyRiddingOfMessageProcessorsWrapping() {
assertTrue(this.groovyRouterMessageHandler instanceof MethodInvokingRouter);
MessageProcessor messageProcessor = TestUtils.getPropertyValue(this.groovyRouterMessageHandler,
"messageProcessor", MessageProcessor.class);
//before it was MethodInvokingMessageProcessor
assertTrue(messageProcessor instanceof GroovyScriptExecutingMessageProcessor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<splitter input-channel="referencedScriptInput">
<splitter id="groovySplitter" input-channel="referencedScriptInput">
<groovy:script location="org/springframework/integration/groovy/config/GroovySplitterTests.groovy"/>
</splitter>

Expand Down
Loading

0 comments on commit 4d3bc36

Please sign in to comment.