Skip to content

Fix DSL to deal with beanNames for handlers #2707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2019 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 @@ -34,16 +34,18 @@
* <b>Note: Stopping ({@link #unsubscribe(MessageHandler)}) the subscribed ({@link MessageHandler}) has no effect.</b>
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 4.0
*
*/
public final class FixedSubscriberChannel implements SubscribableChannel, BeanNameAware, NamedComponent {

private final Log logger = LogFactory.getLog(FixedSubscriberChannel.class);
private static final Log LOGGER = LogFactory.getLog(FixedSubscriberChannel.class);

private final MessageHandler handler;

private volatile String beanName;
private String beanName;

public FixedSubscriberChannel() {
throw new IllegalArgumentException("Cannot instantiate a " + this.getClass().getSimpleName()
Expand All @@ -59,9 +61,14 @@ public void setBeanName(String name) {
this.beanName = name;
}

@Override
public String getBeanName() {
return this.beanName;
}

@Override
public boolean send(Message<?> message) {
return this.send(message, 0);
return send(message, 0);
}

@Override
Expand All @@ -83,23 +90,23 @@ public boolean send(Message<?> message, long timeout) {

@Override
public boolean subscribe(MessageHandler handler) {
if (handler != this.handler && this.logger.isDebugEnabled()) {
this.logger.debug(this.getComponentName() + ": cannot be subscribed to (it has a fixed single subscriber).");
if (handler != this.handler && LOGGER.isDebugEnabled()) {
LOGGER.debug(getComponentName() + ": cannot be subscribed to (it has a fixed single subscriber).");
}
return false;
}

@Override
public boolean unsubscribe(MessageHandler handler) {
if (this.logger.isDebugEnabled()) {
this.logger.debug(this.getComponentName() + ": cannot be unsubscribed from (it has a fixed single subscriber).");
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(getComponentName() + ": cannot be unsubscribed from (it has a fixed single subscriber).");
}
return false;
}

@Override
public String getComponentType() {
return "Fixed Subscriber Channel";
return "fixed-subscriber-channel";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public void setLoggingEnabled(boolean loggingEnabled) {
this.managementOverrides.loggingConfigured = true;
}

@Override
@Nullable
public String getBeanName() {
return this.beanName;
}

@Override
@Nullable
public String getComponentName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ protected final H createHandlerInternal() {
else if (this.logger.isDebugEnabled()) {
String name = this.componentName;
if (name == null && actualHandler instanceof NamedComponent) {
name = ((NamedComponent) actualHandler).getComponentName();
name = ((NamedComponent) actualHandler).getBeanName();
}
this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler"
+ (name == null ? "" : (", " + name)) + ".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ && methodIsHandleMessageOrEmpty(targetMethodName)) {
private void checkReuse(AbstractMessageProducingHandler replyHandler) {
Assert.isTrue(!referencedReplyProducers.contains(replyHandler),
"An AbstractMessageProducingMessageHandler may only be referenced once (" +
replyHandler.getComponentName() + ") - use scope=\"prototype\"");
replyHandler.getBeanName() + ") - use scope=\"prototype\"");
referencedReplyProducers.add(replyHandler);
this.replyHandler = replyHandler;
}
Expand Down Expand Up @@ -213,7 +213,7 @@ protected void postProcessReplyProducer(AbstractMessageProducingHandler handler)
else {
if (this.requiresReply && logger.isDebugEnabled()) {
logger.debug("requires-reply can only be set to AbstractReplyProducingMessageHandler " +
"or its subclass, " + handler.getComponentName() + " doesn't support it.");
"or its subclass, " + handler.getBeanName() + " doesn't support it.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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 @@ -70,7 +70,7 @@ public abstract class IntegrationObjectSupport implements BeanNameAware, NamedCo

protected static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();

private static final IdGenerator idGenerator = new AlternativeJdkIdGenerator(); // NOSONAR lower case
private static final IdGenerator ID_GENERATOR = new AlternativeJdkIdGenerator();

/**
* Logger that is available to subclasses
Expand Down Expand Up @@ -106,6 +106,11 @@ public final void setBeanName(String beanName) {
this.beanName = beanName;
}

@Override
public String getBeanName() {
return this.beanName;
}

/**
* Will return the name of this component identified by {@link #componentName} field.
* If {@link #componentName} was not set this method will default to the 'beanName' of this component;
Expand Down Expand Up @@ -224,7 +229,7 @@ public ConversionService getConversionService() {
this.conversionService = IntegrationUtils.getConversionService(this.beanFactory);
if (this.conversionService == null && this.logger.isDebugEnabled()) {
this.logger.debug("Unable to attempt conversion of Message payload types. Component '" +
this.getComponentName() + "' has no explicit ConversionService reference, " +
getComponentName() + "' has no explicit ConversionService reference, " +
"and there is no 'integrationConversionService' bean within the context.");
}
}
Expand Down Expand Up @@ -301,7 +306,7 @@ public static <T> T extractTypeIfPossible(@Nullable Object targetObject, Class<T
}

public static UUID generateId() {
return idGenerator.generateId();
return ID_GENERATOR.generateId();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ private void invokeBeanInitializationHooks(final String beanName, final Object b
@SuppressWarnings("unchecked")
private boolean noBeanPresentForComponent(Object instance, String parentBeanName) {
if (instance instanceof NamedComponent) {
String beanName = ((NamedComponent) instance).getComponentName();
String beanName = ((NamedComponent) instance).getBeanName();
if (beanName != null) {
if (this.beanFactory.containsBean(beanName)) {
BeanDefinition existingBeanDefinition = this.beanFactory.getBeanDefinition(beanName);
Expand Down Expand Up @@ -445,10 +445,11 @@ private String generateBeanName(Object instance, String prefix) {
}

private String generateBeanName(Object instance, String prefix, String fallbackId, boolean useFlowIdAsPrefix) {
if (instance instanceof NamedComponent && ((NamedComponent) instance).getComponentName() != null) {
if (instance instanceof NamedComponent && ((NamedComponent) instance).getBeanName() != null) {
String beanName = ((NamedComponent) instance).getBeanName();
return useFlowIdAsPrefix
? prefix + ((NamedComponent) instance).getComponentName()
: ((NamedComponent) instance).getComponentName();
? prefix + beanName
: beanName;
}
else if (fallbackId != null) {
return useFlowIdAsPrefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,13 @@ public Map<String, IntegrationFlowRegistration> getRegistry() {
}

private String generateBeanName(Object instance, String parentName) {
if (instance instanceof NamedComponent && ((NamedComponent) instance).getComponentName() != null) {
return ((NamedComponent) instance).getComponentName();
if (instance instanceof NamedComponent) {
String beanName = ((NamedComponent) instance).getBeanName();
if (beanName != null) {
return beanName;
}
}

String generatedBeanName = (parentName != null ? parentName : "") + instance.getClass().getName();
String id = generatedBeanName;
int counter = -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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 @@ -17,13 +17,13 @@
package org.springframework.integration.endpoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.expression.Expression;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.expression.ExpressionEvalMap;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
import org.springframework.integration.support.context.NamedComponent;
import org.springframework.integration.support.management.IntegrationManagedResource;
Expand Down Expand Up @@ -60,9 +60,9 @@ public abstract class AbstractMessageSource<T> extends AbstractExpressionEvaluat

private String managedName;

private volatile boolean countsEnabled;
private boolean countsEnabled;

private volatile boolean loggingEnabled = true;
private boolean loggingEnabled = true;

private MetricsCaptor metricsCaptor;

Expand All @@ -83,6 +83,11 @@ public void setBeanName(String name) {
this.beanName = name;
}

@Override
public String getBeanName() {
return this.beanName;
}

@Override
public void setManagedType(String managedType) {
this.managedType = managedType;
Expand Down Expand Up @@ -216,14 +221,9 @@ private void incrementReceiveCounter() {
}

private Map<String, Object> evaluateHeaders() {
Map<String, Object> results = new HashMap<>();
for (Map.Entry<String, Expression> entry : this.headerExpressions.entrySet()) {
Object headerValue = this.evaluateExpression(entry.getValue());
if (headerValue != null) {
results.put(entry.getKey(), headerValue);
}
}
return results;
return ExpressionEvalMap.from(this.headerExpressions)
.usingEvaluationContext(getEvaluationContext())
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.integration.filter;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.Lifecycle;
import org.springframework.core.convert.ConversionService;
Expand Down Expand Up @@ -47,11 +48,11 @@ public class MessageFilter extends AbstractReplyProducingPostProcessingMessageHa

private final MessageSelector selector;

private volatile boolean throwExceptionOnRejection;
private boolean throwExceptionOnRejection;

private volatile MessageChannel discardChannel;
private MessageChannel discardChannel;

private volatile String discardChannelName;
private String discardChannelName;

/**
* Create a MessageFilter that will delegate to the given {@link MessageSelector}.
Expand Down Expand Up @@ -109,13 +110,10 @@ public void setDiscardWithinAdvice(boolean discardWithinAdvice) {

@Override
public MessageChannel getDiscardChannel() {
if (this.discardChannelName != null) {
synchronized (this) {
if (this.discardChannelName != null) {
this.discardChannel = getChannelResolver().resolveDestination(this.discardChannelName);
this.discardChannelName = null;
}
}
String channelName = this.discardChannelName;
if (channelName != null) {
this.discardChannel = getChannelResolver().resolveDestination(channelName);
this.discardChannelName = null;
}
return this.discardChannel;
}
Expand All @@ -129,15 +127,16 @@ public String getComponentType() {
@Override
protected void doInit() {
Assert.state(!(this.discardChannelName != null && this.discardChannel != null),
"'discardChannelName' and 'discardChannel' are mutually exclusive.");
"'discardChannelName' and 'discardChannel' are mutually exclusive.");
if (this.selector instanceof AbstractMessageProcessingSelector) {
ConversionService conversionService = getConversionService();
if (conversionService != null) {
((AbstractMessageProcessingSelector) this.selector).setConversionService(conversionService);
}
}
if (this.selector instanceof BeanFactoryAware && this.getBeanFactory() != null) {
((BeanFactoryAware) this.selector).setBeanFactory(this.getBeanFactory());
BeanFactory beanFactory = getBeanFactory();
if (this.selector instanceof BeanFactoryAware && beanFactory != null) {
((BeanFactoryAware) this.selector).setBeanFactory(beanFactory);
}
}

Expand Down Expand Up @@ -178,8 +177,7 @@ public Object postProcess(Message<?> message, Object result) {
this.messagingTemplate.send(channel, message);
}
if (this.throwExceptionOnRejection) {
throw new MessageRejectedException(message, "MessageFilter '" + this.getComponentName()
+ "' rejected Message");
throw new MessageRejectedException(message, "MessageFilter '" + getBeanName() + "' rejected Message");
}
}
return result;
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-2019 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,6 +18,8 @@

/**
* @author Mark Fisher
* @author Artem Bilan
*
* @since 2.0
*/
public interface NamedComponent {
Expand All @@ -26,4 +28,8 @@ public interface NamedComponent {

String getComponentType();

default String getBeanName() {
return getComponentName();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 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 @@ -23,6 +23,8 @@
* Adds {@link TrackableComponent}.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 4.2
*/
@IntegrationManagedResource
Expand All @@ -37,6 +39,11 @@ public LifecycleTrackableMessageHandlerMetrics(Lifecycle lifecycle, MessageHandl
this.trackable = (TrackableComponent) delegate;
}

@Override
public String getBeanName() {
return this.trackable.getBeanName();
}

@Override
public String getComponentName() {
return this.trackable.getComponentName();
Expand Down
Loading