Commit 35126a1d by huangfusuper

修正Spring后置处理获取的是代理对象问题 修复API调用失败 修复运行结果错误无返回值问题 修复数据库字段长度问题

parent 848b5d07
......@@ -6,6 +6,9 @@ import com.byit.server.PluginServer;
import com.byit.task.annotations.TaskHandler;
import com.byit.task.handler.interfaces.IJobHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
......@@ -13,6 +16,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
......@@ -49,14 +53,24 @@ public class RpcSpringPluginServerFactory extends PluginServerFactory implements
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(TaskHandler.class);
System.out.println(beansWithAnnotation);
beansWithAnnotation.forEach((key,value) ->{
Object target = null;
if(value instanceof IJobHandler){
TaskHandler annotation = value.getClass().getAnnotation(TaskHandler.class);
String taskName = annotation.taskName();
super.addService(taskName,value);
String expand = annotation.expand();
if (StringUtils.isNotBlank(expand)) {
taskNameExpand.put(taskName,expand);
try {
target = getTarget(value);
} catch (Exception e) {
e.printStackTrace();
}
if(target != null){
TaskHandler annotation = target.getClass().getAnnotation(TaskHandler.class);
System.out.println("注解:"+annotation);
String taskName = annotation.taskName();
super.addService(taskName,value);
String expand = annotation.expand();
if (StringUtils.isNotBlank(expand)) {
taskNameExpand.put(taskName,expand);
}
}
}else{
System.err.println("警告!bean"+key+"不是【com.byit.task.handler.interfaces.IJobHandler】类型!忽略该bean!");
......@@ -64,7 +78,51 @@ public class RpcSpringPluginServerFactory extends PluginServerFactory implements
});
}
public RpcSpringPluginServerFactory(Map<String, String> taskNameExpand) {
this.taskNameExpand = taskNameExpand;
public static Object getTarget(Object proxy) throws Exception {
if(!AopUtils.isAopProxy(proxy)) {
return proxy;//不是代理对象
}
if(AopUtils.isJdkDynamicProxy(proxy)) {
return getJdkDynamicProxyTargetObject(proxy);
} else { //cglib
return getCglibProxyTargetObject(proxy);
}
}
private static Object getCglibProxyTargetObject(Object proxy) throws Exception {
System.out.println(proxy+"-------该对象为cglib代理对象-------");
Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
h.setAccessible(true);
Object dynamicAdvisedInterceptor = h.get(proxy);
Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
advised.setAccessible(true);
Object target = ((AdvisedSupport)advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
return target;
}
private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
System.out.println(proxy+"-------该对象为jdk代理对象-------");
Field h = proxy.getClass().getSuperclass().getDeclaredField("h");
h.setAccessible(true);
AopProxy aopProxy = (AopProxy) h.get(proxy);
Field advised = aopProxy.getClass().getDeclaredField("advised");
advised.setAccessible(true);
Object target = ((AdvisedSupport)advised.get(aopProxy)).getTargetSource().getTarget();
return target;
}
public Map<String, String> getTaskNameExpand() {
return taskNameExpand;
}
}
......@@ -3,6 +3,7 @@ package com.byit.server.netty.handler;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.byit.dto.web.ReturnResult;
import com.byit.enums.JobResultEnum;
import com.byit.enums.ResponseTyEnum;
import com.byit.factory.PluginServerFactory;
import com.byit.packet.request.PluginRpcRequestPacket;
......@@ -59,21 +60,20 @@ public class NettyPluginServerHandler extends SimpleChannelInboundHandler<Plugin
rpcResponsePacket.setStatus(true);
rpcResponsePacket.setRunTime(endTime-startTime);
rpcResponsePacket.setExtension(msg.getExtension());
sendMsg(rpcResponsePacket,msg);
}catch (Throwable e){
rpcResponsePacket.setCode("500000");
rpcResponsePacket.setMsg(e.getMessage());
rpcResponsePacket.setStatus(false);
rpcResponsePacket.setExtension(msg.getExtension());
ReturnResult<String> execute = new ReturnResult<>();
execute.setMsg(e.getMessage());
execute.setCode(JobResultEnum.FAIL.getRes());
rpcResponsePacket.setResult(execute);
sendMsg(rpcResponsePacket,msg);
throw new RuntimeException(e);
}
rpcResponsePacket.setType(ResponseTyEnum.RESPONSE.getType());
rpcResponsePacket.setRequestId(msg.getRequestId());
String responseStr = JSON.toJSONString(rpcResponsePacket);
HttpRequest post = HttpRequest.post(msg.getCallbackUrl());
post.header("token", "Token");
post.header("contentType","application/json");
post.body(responseStr).execute();
System.out.println("-------消息回复成功-----------");
});
transferPluginRpcResponse.setRequestId(msg.getRequestId());
......@@ -85,6 +85,18 @@ public class NettyPluginServerHandler extends SimpleChannelInboundHandler<Plugin
ctx.channel().writeAndFlush(transferPluginRpcResponse);
}
private void sendMsg(PluginRpcResponsePacket rpcResponsePacket, PluginRpcRequestPacket msg ){
rpcResponsePacket.setType(ResponseTyEnum.RESPONSE.getType());
rpcResponsePacket.setRequestId(msg.getRequestId());
String responseStr = JSON.toJSONString(rpcResponsePacket);
HttpRequest post = HttpRequest.post(msg.getCallbackUrl());
post.header("token", "Token");
post.header("contentType","application/json");
post.body(responseStr).execute();
System.out.println("-------消息回复成功-----------");
}
/**
* 异常处理
* @param ctx 上下文对象
......
......@@ -14,28 +14,5 @@ public class TestController {
@Autowired
private TestService testService;
@RequestMapping("test")
public PluginRpcResponsePacket test(){
PluginRpcRequestPacket pluginRpcRequestPacket = new PluginRpcRequestPacket();
pluginRpcRequestPacket.setJobName("01Test");
pluginRpcRequestPacket.setParam("123");
pluginRpcRequestPacket.setExtension("13213213");
pluginRpcRequestPacket.setResultCallback(new DefaultResultCallback());
return testService.test(pluginRpcRequestPacket);
}
public static void main(String[] args) {
PluginRpcRequestPacket pluginRpcRequestPacket = new PluginRpcRequestPacket();
pluginRpcRequestPacket.setJobName("01Test");
pluginRpcRequestPacket.setParam("123");
pluginRpcRequestPacket.setExtension("13213213");
pluginRpcRequestPacket.setResultCallback(new DefaultResultCallback());
String x = JSON.toJSONString(pluginRpcRequestPacket);
System.out.println(x);
PluginRpcRequestPacket pluginRpcRequestPacket1 = JSON.parseObject(x, PluginRpcRequestPacket.class);
PluginRpcResponsePacket pluginRpcResponsePacket = new PluginRpcResponsePacket();
pluginRpcResponsePacket.setCode("0000000");
pluginRpcRequestPacket.getResultCallback().resultCallback(null, pluginRpcResponsePacket);
}
}
package com.byit.controller;
import com.byit.server.TestEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class TestController {
@Autowired
private TestEmail testEmail;
@RequestMapping("test")
public Map<String,String> getMap(){
return testEmail.test();
}
}
package com.byit.server;
import org.springframework.stereotype.Component;
@Component
public class CglibServerTest {
public void test(){
System.out.println("----a-a-a-a-aa-a-a-----s");
}
}
package com.byit.server;
import com.byit.dto.web.ReturnResult;
import com.byit.factory.RpcSpringPluginServerFactory;
import com.byit.param.CommunicationParam;
import com.byit.task.annotations.TaskHandler;
import com.byit.task.handler.BaseJobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author huangfu
*/
@Component
@TaskHandler(cron = "0 0/1 * * * ?",taskName = "sentEmailServer",autoPublish = true,publishUrl = "http://127.0.0.1:8081/myth-job-admin/api/node/autoAddJavaTask")
@TaskHandler(expand = "{sadasdadasdasdasd}",cron = "0 0/1 * * * ?",taskName = "sentEmailServer",autoPublish = true,publishUrl = "http://127.0.0.1:8081/myth-job-admin/api/node/autoAddJavaTask")
@Slf4j
public class SentEmailServer extends BaseJobHandler {
@Autowired
private CglibServerTest cglibServerTest;
@Override
public ReturnResult<String> execute(CommunicationParam param) throws Exception {
cglibServerTest.test();
System.out.println("-------------SentEmailServer-被调度执行------------"+param);
return ReturnResult.SUCCESS;
}
......
package com.byit.server;
import com.byit.factory.RpcSpringPluginServerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class TestEmail {
@Autowired
RpcSpringPluginServerFactory rpcSpringPluginServerFactory;
public Map<String,String> test(){
return rpcSpringPluginServerFactory.getTaskNameExpand();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment