Skip to content
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 2002-2020 the original author or authors.
* Copyright 2002-2022 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,10 +70,12 @@ public class MessagePublishingInterceptor implements MethodInterceptor, BeanFact

private MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();

private boolean messageBuilderFactorySet;

private String defaultChannelName;

private volatile boolean messageBuilderFactorySet;

private volatile boolean templateInitialized;

public MessagePublishingInterceptor(PublisherMetadataSource metadataSource) {
Assert.notNull(metadataSource, "metadataSource must not be null");
this.metadataSource = metadataSource;
Expand All @@ -94,10 +96,6 @@ public void setChannelResolver(DestinationResolver<MessageChannel> channelResolv
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
this.messagingTemplate.setBeanFactory(beanFactory);
if (this.channelResolver == null) {
this.channelResolver = ChannelResolverUtils.getChannelResolver(this.beanFactory);
}
}

protected MessageBuilderFactory getMessageBuilderFactory() {
Expand All @@ -111,8 +109,9 @@ protected MessageBuilderFactory getMessageBuilderFactory() {
}

@Override
public final Object invoke(final MethodInvocation invocation) throws Throwable {
final StandardEvaluationContext context = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
public final Object invoke(MethodInvocation invocation) throws Throwable {
initMessagingTemplateIfAny();
StandardEvaluationContext context = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
Class<?> targetClass = AopUtils.getTargetClass(invocation.getThis());
final Method method = AopUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
String[] argumentNames = resolveArgumentNames(method);
Expand Down Expand Up @@ -143,6 +142,16 @@ public final Object invoke(final MethodInvocation invocation) throws Throwable {
}
}

private void initMessagingTemplateIfAny() {
if (!this.templateInitialized) {
this.messagingTemplate.setBeanFactory(this.beanFactory);
if (this.channelResolver == null) {
this.channelResolver = ChannelResolverUtils.getChannelResolver(this.beanFactory);
}
this.templateInitialized = true;
}
}

private String[] resolveArgumentNames(Method method) {
return this.parameterNameDiscoverer.getParameterNames(method);
}
Expand All @@ -163,20 +172,14 @@ private void publishMessage(Method method, StandardEvaluationContext context) {
}
Message<?> message = builder.build();
String channelName = this.metadataSource.getChannelName(method);
MessageChannel channel = null;
if (channelName != null) {
Assert.state(this.channelResolver != null, "ChannelResolver is required to resolve channel names.");
channel = this.channelResolver.resolveDestination(channelName);
}
if (channel != null) {
this.messagingTemplate.send(channel, message);
this.messagingTemplate.send(channelName, message);
}
else {
String channelNameToUse = this.defaultChannelName;
if (channelNameToUse != null && this.messagingTemplate.getDefaultDestination() == null) {
Assert.state(this.channelResolver != null, "ChannelResolver is required to resolve channel names.");
this.messagingTemplate.setDefaultChannel(
this.channelResolver.resolveDestination(channelNameToUse));
this.messagingTemplate.setDefaultChannel(this.channelResolver.resolveDestination(channelNameToUse));
this.defaultChannelName = null;
}
this.messagingTemplate.send(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public abstract class AbstractMethodAnnotationPostProcessor<T extends Annotation

private ConversionService conversionService;

private DestinationResolver<MessageChannel> channelResolver;
private volatile DestinationResolver<MessageChannel> channelResolver;

@SuppressWarnings(UNCHECKED)
public AbstractMethodAnnotationPostProcessor() {
Expand All @@ -154,10 +154,10 @@ public AbstractMethodAnnotationPostProcessor() {
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
this.definitionRegistry = (BeanDefinitionRegistry) beanFactory;
this.conversionService = this.beanFactory.getConversionService() != null
? this.beanFactory.getConversionService()
: DefaultConversionService.getSharedInstance();
this.channelResolver = ChannelResolverUtils.getChannelResolver(beanFactory);
this.conversionService =
this.beanFactory.getConversionService() != null
? this.beanFactory.getConversionService()
: DefaultConversionService.getSharedInstance();
}

protected ConfigurableListableBeanFactory getBeanFactory() {
Expand All @@ -173,6 +173,9 @@ protected ConversionService getConversionService() {
}

protected DestinationResolver<MessageChannel> getChannelResolver() {
if (this.channelResolver == null) {
this.channelResolver = ChannelResolverUtils.getChannelResolver(this.beanFactory);
}
return this.channelResolver;
}

Expand Down Expand Up @@ -569,7 +572,7 @@ protected AbstractEndpoint createEndpoint(MessageHandler handler, @SuppressWarni
if (StringUtils.hasText(inputChannelName)) {
MessageChannel inputChannel;
try {
inputChannel = this.channelResolver.resolveDestination(inputChannelName);
inputChannel = getChannelResolver().resolveDestination(inputChannelName);
}
catch (DestinationResolutionException e) {
if (e.getCause() instanceof NoSuchBeanDefinitionException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
*
* @author Artem Bilan
* @author Gary Russell
*
* @since 4.2
*/
@Configuration(proxyBeanMethods = false)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class IntegrationManagementConfiguration implements ImportAware, EnvironmentAware {

private AnnotationAttributes attributes;
Expand Down