Skip to content

Commit 6eeec50

Browse files
artembilangaryrussell
authored andcommitted
INT-4572: Add MessageProducer.setOutputChannelName
JIRA: https://jira.spring.io/browse/INT-4572 * Add `setOutputChannelName()` contract into the `MessageProducer` to avoid proxy unwrapping and casting to the `MessageProducerSupport`
1 parent 53e3248 commit 6eeec50

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

spring-integration-core/src/main/java/org/springframework/integration/core/MessageProducer.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,20 +20,30 @@
2020

2121
/**
2222
* Base interface for any component that is capable of sending
23-
* Messages to a {@link MessageChannel}.
23+
* messages to a {@link MessageChannel}.
2424
*
2525
* @author Mark Fisher
26+
* @author Artem Bilan
27+
*
2628
* @since 2.0
2729
*/
2830
public interface MessageProducer {
2931

3032
/**
31-
* Specify the MessageChannel to which produced Messages should be sent.
32-
*
33+
* Specify the {@link MessageChannel} to which produced Messages should be sent.
3334
* @param outputChannel The output channel.
3435
*/
3536
void setOutputChannel(MessageChannel outputChannel);
3637

38+
/**
39+
* Specify the bean name of the {@link MessageChannel} to which produced Messages should be sent.
40+
* @param outputChannel The output channel bean name.
41+
* @since 5.1.2
42+
*/
43+
default void setOutputChannelName(String outputChannel) {
44+
throw new UnsupportedOperationException("This MessageProducer does not support setting the channel by name.");
45+
}
46+
3747
/**
3848
* Return the the output channel.
3949
* @return the channel.

spring-integration-core/src/main/java/org/springframework/integration/dsl/IntegrationFlowDefinition.java

+8-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -118,7 +118,7 @@ public abstract class IntegrationFlowDefinition<B extends IntegrationFlowDefinit
118118

119119
private static final Set<MessageProducer> REFERENCED_REPLY_PRODUCERS = new HashSet<>();
120120

121-
protected final Map<Object, String> integrationComponents = new LinkedHashMap<>();
121+
protected final Map<Object, String> integrationComponents = new LinkedHashMap<>(); //NOSONAR - final
122122

123123
private MessageChannel currentMessageChannel;
124124

@@ -3062,36 +3062,19 @@ private B registerOutputChannelIfCan(MessageChannel outputChannel) {
30623062
channelName = ((MessageChannelReference) outputChannel).getName();
30633063
}
30643064

3065-
Object currentComponent = this.currentComponent;
3066-
3067-
if (AopUtils.isAopProxy(currentComponent)) {
3068-
currentComponent = extractProxyTarget(currentComponent);
3069-
}
3070-
3071-
if (currentComponent instanceof MessageProducer) {
3072-
MessageProducer messageProducer =
3073-
(MessageProducer) currentComponent;
3065+
if (this.currentComponent instanceof MessageProducer) {
3066+
MessageProducer messageProducer = (MessageProducer) this.currentComponent;
30743067
checkReuse(messageProducer);
30753068
if (channelName != null) {
3076-
if (messageProducer instanceof AbstractMessageProducingHandler) {
3077-
((AbstractMessageProducingHandler) messageProducer).setOutputChannelName(channelName);
3078-
}
3079-
else {
3080-
throw new BeanCreationException("The 'currentComponent' (" + currentComponent
3081-
+ ") must extend 'AbstractMessageProducingHandler' "
3082-
+ "for message channel resolution by name.\n"
3083-
+ "Your handler should extend 'AbstractMessageProducingHandler', "
3084-
+ "its subclass 'AbstractReplyProducingMessageHandler', or you should "
3085-
+ "reference a 'MessageChannel' bean instead of its name.");
3086-
}
3069+
messageProducer.setOutputChannelName(channelName);
30873070
}
30883071
else {
30893072
messageProducer.setOutputChannel(outputChannel);
30903073
}
30913074
}
3092-
else if (currentComponent instanceof SourcePollingChannelAdapterSpec) {
3075+
else if (this.currentComponent instanceof SourcePollingChannelAdapterSpec) {
30933076
SourcePollingChannelAdapterFactoryBean pollingChannelAdapterFactoryBean =
3094-
((SourcePollingChannelAdapterSpec) currentComponent).get().getT1();
3077+
((SourcePollingChannelAdapterSpec) this.currentComponent).get().getT1();
30953078
if (channelName != null) {
30963079
pollingChannelAdapterFactoryBean.setOutputChannelName(channelName);
30973080
}
@@ -3100,7 +3083,7 @@ else if (currentComponent instanceof SourcePollingChannelAdapterSpec) {
31003083
}
31013084
}
31023085
else {
3103-
throw new BeanCreationException("The 'currentComponent' (" + currentComponent +
3086+
throw new BeanCreationException("The 'currentComponent' (" + this.currentComponent +
31043087
") is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. " +
31053088
"This is the end of the integration flow.");
31063089
}

spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageProducerSupport.java

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public void setOutputChannel(MessageChannel outputChannel) {
7373
* @param outputChannelName the channel name.
7474
* @since 4.3
7575
*/
76+
@Override
7677
public void setOutputChannelName(String outputChannelName) {
7778
Assert.hasText(outputChannelName, "'outputChannelName' must not be null or empty");
7879
this.outputChannelName = outputChannelName;

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageProducingHandler.java

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public void setOutputChannel(MessageChannel outputChannel) {
9191
this.outputChannel = outputChannel;
9292
}
9393

94+
@Override
9495
public void setOutputChannelName(String outputChannelName) {
9596
Assert.hasText(outputChannelName, "'outputChannelName' must not be empty");
9697
this.outputChannelName = outputChannelName; //NOSONAR (inconsistent sync)

0 commit comments

Comments
 (0)