Skip to content

[ISSUE #723] Fix warning log info #724

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Expand Up @@ -16,33 +16,44 @@
*/
package org.apache.rocketmq.spring.annotation;

import java.lang.reflect.AnnotatedElement;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import org.apache.rocketmq.spring.support.RocketMQMessageListenerContainerRegistrar;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.OrderComparator;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.AnnotatedElement;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class RocketMQMessageListenerBeanPostProcessor implements ApplicationContextAware, BeanPostProcessor, InitializingBean, SmartLifecycle {

private ApplicationContext applicationContext;
public class RocketMQMessageListenerBeanPostProcessor implements BeanPostProcessor, SmartLifecycle {

private AnnotationEnhancer enhancer;

private RocketMQMessageListenerContainerRegistrar listenerContainerRegistrar;
private final ObjectProvider<RocketMQMessageListenerContainerRegistrar> registrarObjectProvider;

private boolean running = false;

public RocketMQMessageListenerBeanPostProcessor(List<AnnotationEnhancer> enhancers,
ObjectProvider<RocketMQMessageListenerContainerRegistrar> provider) {
List<AnnotationEnhancer> sortedEnhancers = enhancers
.stream()
.sorted(new OrderComparator())
.collect(Collectors.toList());
this.enhancer = (attrs, element) -> {
Map<String, Object> newAttrs = attrs;
for (AnnotationEnhancer enh : sortedEnhancers) {
newAttrs = enh.apply(newAttrs, element);
}
return attrs;
};
registrarObjectProvider = provider;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
Expand All @@ -54,9 +65,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
RocketMQMessageListener ann = targetClass.getAnnotation(RocketMQMessageListener.class);
if (ann != null) {
RocketMQMessageListener enhance = enhance(targetClass, ann);
if (listenerContainerRegistrar != null) {
listenerContainerRegistrar.registerContainer(beanName, bean, enhance);
}
registrarObjectProvider.ifAvailable(registrar -> registrar.registerContainer(beanName, bean, enhance));
}
return bean;
}
Expand All @@ -70,7 +79,7 @@ public int getPhase() {
public void start() {
if (!isRunning()) {
this.setRunning(true);
listenerContainerRegistrar.startContainer();
registrarObjectProvider.ifAvailable(RocketMQMessageListenerContainerRegistrar::startContainer);
}
}

Expand All @@ -83,49 +92,18 @@ public void setRunning(boolean running) {
this.running = running;
}


@Override
public boolean isRunning() {
return running;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

@Override
public void afterPropertiesSet() throws Exception {
buildEnhancer();
this.listenerContainerRegistrar = this.applicationContext.getBean(RocketMQMessageListenerContainerRegistrar.class);
}

private void buildEnhancer() {
if (this.applicationContext != null) {
Map<String, AnnotationEnhancer> enhancersMap =
this.applicationContext.getBeansOfType(AnnotationEnhancer.class, false, false);
if (enhancersMap.size() > 0) {
List<AnnotationEnhancer> enhancers = enhancersMap.values()
.stream()
.sorted(new OrderComparator())
.collect(Collectors.toList());
this.enhancer = (attrs, element) -> {
Map<String, Object> newAttrs = attrs;
for (AnnotationEnhancer enh : enhancers) {
newAttrs = enh.apply(newAttrs, element);
}
return attrs;
};
}
}
}

private RocketMQMessageListener enhance(AnnotatedElement element, RocketMQMessageListener ann) {
if (this.enhancer == null) {
return ann;
} else {
}
else {
return AnnotationUtils.synthesizeAnnotation(
this.enhancer.apply(AnnotationUtils.getAnnotationAttributes(ann), element), RocketMQMessageListener.class, null);
this.enhancer.apply(AnnotationUtils.getAnnotationAttributes(ann), element), RocketMQMessageListener.class, null);
}
}

Expand Down