diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractMessageChannelBinder.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractMessageChannelBinder.java index ced59706e..3914b2ed7 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractMessageChannelBinder.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/AbstractMessageChannelBinder.java @@ -28,13 +28,11 @@ import org.springframework.cloud.stream.provisioning.ProvisioningProvider; import org.springframework.context.Lifecycle; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.integration.channel.FixedSubscriberChannel; +import org.springframework.integration.channel.AbstractMessageChannel; import org.springframework.integration.channel.PublishSubscribeChannel; import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.core.MessageProducer; -import org.springframework.integration.endpoint.EventDrivenConsumer; import org.springframework.integration.handler.AbstractMessageHandler; -import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.handler.BridgeHandler; import org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer; import org.springframework.integration.support.ErrorMessageStrategy; @@ -43,6 +41,7 @@ import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.SubscribableChannel; +import org.springframework.messaging.support.ChannelInterceptorAdapter; import org.springframework.util.Assert; import org.springframework.util.MimeType; @@ -60,6 +59,7 @@ import org.springframework.util.MimeType; * @author Marius Bogoevici * @author Ilayaperumal Gopinathan * @author Soby Chacko + * @author Oleg Zhurakousky * @since 1.1 */ public abstract class AbstractMessageChannelBinder> @@ -156,7 +156,7 @@ public abstract class AbstractMessageChannelBinder(name, group, inputChannel, - endpoint instanceof Lifecycle ? (Lifecycle) endpoint : null) { + consumerEndpoint instanceof Lifecycle ? (Lifecycle) consumerEndpoint : null) { @Override protected void afterUnbind() { try { - if (endpoint instanceof DisposableBean) { - ((DisposableBean) endpoint).destroy(); + if (getEndpoint() instanceof DisposableBean) { + ((DisposableBean) getEndpoint()).destroy(); } } catch (Exception e) { AbstractMessageChannelBinder.this.logger - .error("Exception thrown while unbinding " + this.toString(), e); + .error("Exception thrown while unbinding " + toString(), e); } afterUnbindConsumer(destination, this.group, properties); destroyErrorInfrastructure(destination, group, properties); @@ -276,6 +270,30 @@ public abstract class AbstractMessageChannelBinder preSend(Message message, MessageChannel channel) { + if (message.getPayload() instanceof byte[]) { + if (!message.getHeaders().containsKey(BinderHeaders.NATIVE_HEADERS_PRESENT) + && EmbeddedHeaderUtils.mayHaveEmbeddedHeaders((byte[]) message.getPayload())) { + MessageValues messageValues; + try { + messageValues = EmbeddedHeaderUtils.extractHeaders((Message) message, true); + } + catch (Exception e) { + logger.debug(EmbeddedHeaderUtils.decodeExceptionMessage(message),e); + messageValues = new MessageValues(message); + } + return messageValues.toMessage(); + } + } + return message; + } + }); + } + /** * Creates {@link MessageProducer} that receives data from the consumer destination. * will be started and stopped by the binder. @@ -523,49 +541,6 @@ public abstract class AbstractMessageChannelBinder requestMessage) { - if (!(requestMessage.getPayload() instanceof byte[])) { - return requestMessage; - } - if (this.extractEmbeddedHeaders - && !requestMessage.getHeaders().containsKey(BinderHeaders.NATIVE_HEADERS_PRESENT) - && EmbeddedHeaderUtils.mayHaveEmbeddedHeaders((byte[]) requestMessage.getPayload())) { - MessageValues messageValues; - try { - messageValues = EmbeddedHeaderUtils.extractHeaders((Message) requestMessage, - true); - } - catch (Exception e) { - AbstractMessageChannelBinder.this.logger.debug( - EmbeddedHeaderUtils.decodeExceptionMessage( - requestMessage), - e); - messageValues = new MessageValues(requestMessage); - } - return messageValues.toMessage(); - } - else { - return requestMessage; - } - } - - @Override - protected boolean shouldCopyRequestHeaders() { - // prevent the message from being copied again in superclass - return false; - } - } - private final class SendingHandler extends AbstractMessageHandler implements Lifecycle { private final boolean embedHeaders; @@ -578,7 +553,7 @@ public abstract class AbstractMessageChannelBinder serializeAndEmbedHeadersIfApplicable(Message message) throws Exception { MessageValues transformed = serializePayloadIfNecessary(message); byte[] payload; diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinding.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinding.java index bab7864aa..a756fda91 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinding.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinding.java @@ -42,7 +42,7 @@ public class DefaultBinding implements Binding { /** * Creates an instance that associates a given name, group and binding target with an * optional {@link Lifecycle} component, which will be stopped during unbinding. - * + * * @param name the name of the binding target * @param group the group (only for input targets) * @param target the binding target @@ -73,6 +73,10 @@ public class DefaultBinding implements Binding { afterUnbind(); } + Lifecycle getEndpoint() { + return this.lifecycle; + } + /** * Listener method that executes after unbinding. Subclasses can implement their own * behaviour on unbinding by overriding this method.