NutzCN Logo
问答 nutz集成的quartz,定时用的是配置文件,我现在需要从数据库取配置的时间,重写了NutQuartzCronJobFactory里面的init方法,为什么不起作用?
发布于 2616天前 作者 qq_41c55c4d 2243 次浏览 复制 上一个帖子 下一个帖子
标签:
public class MainSetup implements Setup {

	@Inject("refer:mainConfig")
	private MainConfig mainConfig;
	
	@Inject("refer:cryptBean")
	private CryptBean cryptBean;
	
	private static final Log log = Logs.get();
	
	public void init(NutConfig conf) {
		Ioc ioc = conf.getIoc();

		log.debug("clean Non-Active User , start");
		ioc.get(MyNutQuartzCronJobFactory.class);
	}
	
	public void destroy(NutConfig conf) {
	}

}
public class MyNutQuartzCronJobFactory extends NutQuartzCronJobFactory {
	private static final Log log = Logs.get();

	@Inject
	protected SqlKeyBiz sqlKeyBiz;
	
	@SuppressWarnings("unchecked")
	@Override
	public void init() throws Exception {
		InputApi inputApi = new InputApi();
		List<Record> rds = sqlKeyBiz.list(inputApi, "#quartzJobQuery");
		String prefix = "cron.";
		for(Record r :rds) {
			String key=r.getString("TIMING_KEY");
			if (key.length() >= prefix.length() + 1 && key.startsWith(prefix)) {
				String name = key.substring(prefix.length());
				if ("pkgs".equals(name)) {
					log.debug((new StringBuilder()).append("found cron job packages = ")
							.append(r.getString("TIMING_TIME")).toString());
					String as[] = Strings.splitIgnoreBlank(key, ",");
					int i = as.length;
					int j = 0;
					while (j < i) {
						String pkg = as[j];
						addPackage(pkg);
						j++;
					}
				}else{
					log.debugf("job define name=%s cron=%s", new Object[] {name, key});
					Class klass = null;
					if (name.contains("."))
						klass = Lang.loadClass(name);
					else
						klass = Lang.loadClass((new StringBuilder())
								.append(getClass().getPackage().getName())
								.append(".job.").append(name).toString());
					cron(key, klass);
					
				}
			}
		}
	}

}
31 回复

没写@IocBean?

@IocBean
public class MyNutQuartzCronJobFactory extends NutQuartzCronJobFactory {}是这样吗?

@IocBean(create="init")

好了,厉害,谢谢!

能说一下原理吗?不太懂

去看看ioc的文档

org.nutz.ioc.IocException: CronExpression 'cron.com.xyt.quartz.b2800QuartzJob' is invalid. FAIL to create Ioc Bean name=[myNutQuartzCronJobFactory]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:147)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:204)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:229)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:148)
at com.xyt.controller.MainSetup.init(MainSetup.java:27)
at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:276)
at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:120)
at org.nutz.mvc.ActionHandler.(ActionHandler.java:19)
at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
at org.nutz.mvc.NutFilter.init(NutFilter.java:65)

报这个错是什么原因呢?

贴完整报错信息

org.nutz.ioc.IocException: CronExpression 'cron.com.xyt.quartz.b2800QuartzJob' is invalid. FAIL to create Ioc Bean name=[myNutQuartzCronJobFactory]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:147)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:204)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:229)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:148)
at com.xyt.controller.MainSetup.init(MainSetup.java:27)
at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:276)
at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:120)
at org.nutz.mvc.ActionHandler.(ActionHandler.java:19)
at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:105)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4855)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5549)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.RuntimeException: CronExpression 'cron.com.xyt.quartz.b2800QuartzJob' is invalid.
at org.quartz.CronScheduleBuilder.cronSchedule(CronScheduleBuilder.java:111)
at org.nutz.integration.quartz.NutQuartzCronJobFactory.cron(NutQuartzCronJobFactory.java:108)
at com.xyt.controller.MyNutQuartzCronJobFactory$$NUTZAOP._aop_invoke(MyNutQuartzCronJobFactory.java:4)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.LoggingMethodInterceptor.filter(LoggingMethodInterceptor.java:48)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.xyt.controller.MyNutQuartzCronJobFactory$$NUTZAOP.cron(MyNutQuartzCronJobFactory.java:1)
at com.xyt.controller.MyNutQuartzCronJobFactory.init(MyNutQuartzCronJobFactory.java:54)
at com.xyt.controller.MyNutQuartzCronJobFactory$$NUTZAOP._aop_invoke(MyNutQuartzCronJobFactory.java:1)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.LoggingMethodInterceptor.filter(LoggingMethodInterceptor.java:48)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.xyt.controller.MyNutQuartzCronJobFactory$$NUTZAOP.init(MyNutQuartzCronJobFactory.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:72)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
... 21 more
Caused by: java.text.ParseException: Illegal characters for this position: 'CRO'
at org.quartz.CronExpression.storeExpressionVals(CronExpression.java:588)
at org.quartz.CronExpression.buildExpression(CronExpression.java:487)
at org.quartz.CronExpression.(CronExpression.java:276)
at org.quartz.CronScheduleBuilder.cronSchedule(CronScheduleBuilder.java:107)
... 42 more

cron(key, klass);

这是啥方法??? 最新版的NutQuartzCronJobFactory里面并无这个方法, 是Quartzs.cron??

public void cron(String cron, Class klass)
throws SchedulerException
{
String name = klass.getName();
org.quartz.JobDetail job = JobBuilder.newJob(klass).withIdentity(name).build();
CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().withIdentity(name).withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
scheduler.scheduleJob(job, trigger);
}
这个是NutQuartzCronJobFactory里面的方法

那就是你的cron变量没传对啊...

cron表达式不对

恩,不报错了,但我设置的一分钟执行一次,怎么不执行?

写错cron表达式吧

0 0/1 * * * ?这样写的

你确定已经被加入scheduler了吗? debug 一下scheduler.scheduleJob 看看

SimpleScheduleBuilder schedule = SimpleScheduleBuilder.simpleSchedule();这里报空指针异常

klass是null?

klass = Lang.loadClass(name);是不是这里路径的问题啊??

class com.xyt.quartz.b2800QuartzJob是这个值

name的值是什么

你可以改成Class.forName试试

//klass=Thread.currentThread().getContextClassLoader().loadClass(name);
//klass=Class.forName(className);
klass = Lang.loadClass(name);
这三种我都试了,还是报
Caused by: java.lang.NullPointerException
at org.nutz.integration.quartz.NutQuartzCronJobFactory.cron(NutQuartzCronJobFactory.java:110)
at com.xyt.controller.MyNutQuartzCronJobFactory$$NUTZAOP._aop_invoke(MyNutQuartzCronJobFactory.java:4)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.LoggingMethodInterceptor.filter(LoggingMethodInterceptor.java:48)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.xyt.controller.MyNutQuartzCronJobFactory$$NUTZAOP.cron(MyNutQuartzCronJobFactory.java:1)
at com.xyt.controller.MyNutQuartzCronJobFactory.init(MyNutQuartzCronJobFactory.java:56)
at com.xyt.controller.MyNutQuartzCronJobFactory$$NUTZAOP._aop_invoke(MyNutQuartzCronJobFactory.java:1)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.LoggingMethodInterceptor.filter(LoggingMethodInterceptor.java:48)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.xyt.controller.MyNutQuartzCronJobFactory$$NUTZAOP.init(MyNutQuartzCronJobFactory.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:72)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
... 21 more

name是com.xyt.quartz.b2800QuartzJob

org.nutz.integration.quartz.NutQuartzCronJobFactory.cron(NutQuartzCronJobFactory.java:110)

这110行的代码是什么? 什么变量为null呢?

哦, 我貌似知道了, 你没注入scheduler

SimpleScheduleBuilder schedule = SimpleScheduleBuilder.simpleSchedule();就是这个

@IocBean(create="init", fields={"scheduler"})

哇!,可以了,谢谢!

添加回复
请先登陆
回到顶部