Skip to content

Commit

Permalink
fix(halo & wormhole & pillar & ice): Fixed @Async on method listene…
Browse files Browse the repository at this point in the history
…r of handler caused application startup error which condition on `proxyTargetClass = true`.

[Halo] Removing `async` attr from `HaloListener`, using `@Async` for instead.
  • Loading branch information
yizzuide committed Jul 17, 2020
1 parent e55a493 commit d71b99a
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Milkomeda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<java.version>1.8</java.version>
<project.release.version>3.11.3-SNAPSHOT</project.release.version>
<project.release.version>3.11.4-SNAPSHOT</project.release.version>
<spring-boot.version>2.2.4</spring-boot.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<mybatis.starter.version>2.1.1</mybatis.starter.version>
Expand Down Expand Up @@ -67,7 +67,7 @@
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>3.11.3</project.release.version>
<project.release.version>3.11.4</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.yizzuide.milkomeda.halo;

import com.github.yizzuide.milkomeda.pulsar.EnablePulsar;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;
Expand All @@ -10,14 +9,13 @@
*
* @author yizzuide
* @since 2.5.0
* @version 2.7.5
* @version 3.11.4
* Create at 2020/01/30 18:42
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnablePulsar
@Import(HaloConfig.class)
public @interface EnableHalo {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.lang.NonNull;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -27,10 +26,6 @@ public class HaloContext implements ApplicationListener<ContextRefreshedEvent> {
* 监听类型的方法属性名
*/
public static final String ATTR_TYPE = "type";
/**
* 调用处理异步方式的方法属性名
*/
public static final String ATTR_ASYNC = "async";

private static Map<String, List<HandlerMetaData>> tableNameMap = new HashMap<>();

Expand All @@ -42,11 +37,9 @@ public class HaloContext implements ApplicationListener<ContextRefreshedEvent> {
public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
tableNameMap = AopContextHolder.getHandlerMetaData(HaloHandler.class, HaloListener.class, (annotation, handlerAnnotation, metaData) -> {
HaloListener haloListener = (HaloListener) annotation;
boolean isAsyncPresentOn = metaData.getMethod().isAnnotationPresent(Async.class);
// 设置其它属性方法的值
Map<String, Object> attrs = new HashMap<>(4);
attrs.put(ATTR_TYPE, haloListener.type());
attrs.put(ATTR_ASYNC, isAsyncPresentOn || haloListener.async());
metaData.setAttributes(attrs);
String value = haloListener.value();
cacheWithType(haloListener.type() == HaloType.PRE ? preTableNameMap : postTableNameMap, value, metaData);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.yizzuide.milkomeda.halo;

import com.github.yizzuide.milkomeda.pulsar.PulsarHolder;
import com.github.yizzuide.milkomeda.universe.metadata.HandlerMetaData;
import com.github.yizzuide.milkomeda.util.MybatisUtil;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -35,7 +34,7 @@
*
* @author yizzuide
* @since 2.5.0
* @version 3.11.1
* @version 3.11.4
* Create at 2020/01/30 20:38
*/
@Slf4j
Expand Down Expand Up @@ -164,13 +163,7 @@ private void invokeWithTable(String tableName, String matchTableName, String sql
if (CollectionUtils.isEmpty(metaDataList)) {
return;
}
metaDataList.forEach(handlerMetaData -> {
if ((boolean) handlerMetaData.getAttributes().get(HaloContext.ATTR_ASYNC)) {
PulsarHolder.getPulsar().post(() -> invokeHandler(tableName, handlerMetaData, sql, mappedStatement, param, result));
} else {
invokeHandler(tableName, handlerMetaData, sql, mappedStatement, param, result);
}
});
metaDataList.forEach(handlerMetaData -> invokeHandler(tableName, handlerMetaData, sql, mappedStatement, param, result));
}

private void invokeHandler(String tableName, HandlerMetaData handlerMetaData, String sql, MappedStatement mappedStatement, Object param, Object result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.yizzuide.milkomeda.halo;

import org.springframework.core.annotation.AliasFor;
import org.springframework.scheduling.annotation.Async;

import java.lang.annotation.*;

Expand All @@ -20,7 +19,7 @@
*
* @author yizzuide
* @since 2.5.0
* @version 3.11.1
* @version 3.11.4
* Create at 2020/01/30 22:36
*/
@Documented
Expand All @@ -46,12 +45,4 @@
* @return 默认为后置监听
*/
HaloType type() default HaloType.POST;

/**
* 是否异步处理
* @return 默认同步处理
* @deprecated since 3.11.1, using {@link Async} for instead.
*/
@Deprecated
boolean async() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author yizzuide
* @since 1.13.4
* @version 3.11.1
* @version 3.11.4
* Create at 2019/10/24 21:17
*/
public class AopContextHolder {
Expand Down Expand Up @@ -83,9 +83,10 @@ public static Map<String, List<HandlerMetaData>> getHandlerMetaData(
for (String key : beanMap.keySet()) {
Object target = beanMap.get(key);
// 查找AOP切面(通过Proxy.isProxyClass()判断类是否是代理的接口类,AopUtils.isAopProxy()判断对象是否被代理),可以通过AopUtils.getTargetClass()获取原Class
Method[] methods = ReflectionUtils.getAllDeclaredMethods(AopUtils.isAopProxy(target) ?
AopUtils.getTargetClass(target) : target.getClass());
Annotation handlerAnnotation = target.getClass().getAnnotation(handlerAnnotationClazz);
Class<?> targetClass = AopUtils.isAopProxy(target) ?
AopUtils.getTargetClass(target) : target.getClass();
Method[] methods = ReflectionUtils.getAllDeclaredMethods(targetClass);
Annotation handlerAnnotation = targetClass.getAnnotation(handlerAnnotationClazz);
for (Method method : methods) {
// 获取指定方法上的注解的属性
final Annotation executeAnnotation = AnnotationUtils.findAnnotation(method, executeAnnotationClazz);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.yizzuide.milkomeda.wormhole;

import com.github.yizzuide.milkomeda.pulsar.EnablePulsar;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;
Expand All @@ -10,13 +9,13 @@
*
* @author yizzuide
* @since 3.3.0
* @version 3.11.4
* Create at 2020/05/05 13:57
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnablePulsar
@Import(WormholeConfig.class)
public @interface EnableWormhole {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.yizzuide.milkomeda.wormhole;

import com.github.yizzuide.milkomeda.pulsar.PulsarHolder;
import com.github.yizzuide.milkomeda.universe.metadata.HandlerMetaData;
import com.github.yizzuide.milkomeda.util.ReflectUtil;
import lombok.Data;
Expand All @@ -18,7 +17,7 @@
*
* @author yizzuide
* @since 3.3.0
* @version 3.11.1
* @version 3.11.4
* Create at 2020/05/05 14:30
*/
@Slf4j
Expand Down Expand Up @@ -60,7 +59,7 @@ public <T> void publish(WormholeEvent<T> event, String action,
}
boolean isAsync = (boolean) handler.getAttributes().get(WormholeRegistration.ATTR_ASYNC);
WormholeTransactionHangType hangType = (WormholeTransactionHangType) handler.getAttributes().get(WormholeRegistration.ATTR_HANG_TYPE);
// 非事件回调执行
// 非事务回调执行
if (hangType == WormholeTransactionHangType.NONE) {
execute(isAsync, handler, event, action, callback);
} else {
Expand Down Expand Up @@ -108,19 +107,14 @@ private <T> void execute(boolean isAsync, HandlerMetaData handler, WormholeEvent
Object result = null;
Exception e = null;
try {
if (isAsync) {
result = PulsarHolder.getPulsar().postForResult(() -> ReflectUtil.invokeWithWrapperInject(handler.getTarget(), handler.getMethod(),
Collections.singletonList(event), WormholeEvent.class, WormholeEvent::getData, WormholeEvent::setData));
} else {
result = ReflectUtil.invokeWithWrapperInject(handler.getTarget(), handler.getMethod(),
Collections.singletonList(event), WormholeEvent.class, WormholeEvent::getData, WormholeEvent::setData);
}
result = ReflectUtil.invokeWithWrapperInject(handler.getTarget(), handler.getMethod(),
Collections.singletonList(event), WormholeEvent.class, WormholeEvent::getData, WormholeEvent::setData);
} catch (Exception ex) {
e = ex;
}

if (callback != null) {
if (isAsync && result != null) {
if (isAsync && result instanceof Future) {
try {
callback.callback(event, action, ((Future) result).get(), e);
} catch (Exception exception) {
Expand Down
2 changes: 1 addition & 1 deletion MilkomedaDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<milkomeda.version>3.11.3-SNAPSHOT</milkomeda.version>
<milkomeda.version>3.11.4-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
<redission.version>3.12.5</redission.version>
<zookeeper.version>3.4.14</zookeeper.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class AuditController {
@Resource
private CreditAuditApplicationService creditAuditApplicationService;

// http://localhost:8091/audit/callback?callbackId=123&orderId=12432434&state=0
@RequestMapping("callback")
public Object audit(AuditCommand auditCommand) {
creditAuditApplicationService.audit(auditCommand);
Expand Down

0 comments on commit d71b99a

Please sign in to comment.