今天在写代码的时候需要实现用反射取调取quartz的Job的execute方法。
但是发现excute方法是默认带有一个JobExecutionContext
参数的
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("run job。。。。。。。");
}
}
研究之后发现倒是可以创建一个继承JobExecutionContext
接口的类传进去然后执行。
但是因为有强迫症不想写这种没有的代码。所以想着能不能传一个null进去。
比如像下面的写法
TestJob testJob = new TestJob();
testJob.execute(null);
百度了一番之后发现没有人这样做过,可能因为这样做太过于奇葩。但是没办法现在就是要实现。于是在博主的各种实验下,终于发现了如下的放法可以传一个null的参数进去
Class mc = Class.forName("com.xmhmyh.generalframework.framework.quartz.task.TestJob");
Method method = mc.getMethod("execute", JobExecutionContext.class);
method.invoke(mc.newInstance(),new Object[]{null});
传参数的地方使用new Object[]{null}
就可以了。
如果有大神知道为什么这样写可以欢迎评论区告诉小弟一下。谢谢🙏