配置文件
var ioc = {
jedisPoolConfig : {
type : "redis.clients.jedis.JedisPoolConfig",
fields : {
maxTotal : 10,
maxIdle : 5,
maxWaitMillis : 30000,
testOnBorrow : true
}
},
jedisConfig:{
type: "com.xy.base.JedisConfig",
fields:{
poolConfig:{
refer : "jedisPoolConfig"
},
port: 6379,
host: "127.0.0.1",
timeout: 30000,
password:123456
}
},
jedisPooler : {
type : "com.xy.base.JedisPooler",
args : [ {
refer : "jedisConfig"
}]
}
}
package com.xy.base;
import org.apache.commons.lang.StringUtils;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import redis.clients.jedis.JedisPool;
@IocBean(create = "getPool", depose = "onDepose")
public class JedisPooler {
private static final Log log = Logs.getLog(JedisPooler.class);
private JedisPool pool;
JedisConfig config;
public JedisPooler(JedisConfig config) {
this.config = config;
}
public synchronized JedisPool getPool() {
log.debug("JedisPooler onCreate getPool...");
if (pool == null) {
log.debug("即将连接redis:\r\n"+Json.toJson(config));
if (StringUtils.isNotEmpty(config.password)) {
pool = new JedisPool(config.poolConfig, config.host, config.port, config.timeout, config.password);
} else {
pool = new JedisPool(config.poolConfig, config.host, config.port, config.timeout);
}
}
return pool;
}
public void onDepose() {
pool.destroy();
}
}
定时任务
package com.xy.quartz;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xy.base.JedisPooler;
import com.xy.biz.QuartzBiz;
import com.xy.utils.DateUtils;
import redis.clients.jedis.Jedis;
@IocBean
public class TestJob implements Job {
private static final Log log = Logs.get();
@Inject
protected QuartzBiz quartzBiz;
@Inject("refer:jedisPooler")
private JedisPooler jedisPooler;
public void execute(JobExecutionContext context)
throws JobExecutionException {
try {
log.info("-----------TestJob---准备开始!----------------"+ Times.sDTms(new Date()));
Jedis jedis = jedisPooler.getPool().getResource();
String date = DateUtils.date2Str(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
JSONArray array = new JSONArray();
if(StringUtils.isNotEmpty(jedis.get("data"))){
array = JSONObject.parseArray(jedis.get("data"));
array.add(date);
}else{
array.add(date);
}
jedis.set("data", JSONObject.toJSONString(array));
log.info("-----------TestJob---结束----------------"+ Times.sDTms(new Date()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行定时任务会报下面的错误
```
org.nutz.ioc.IocException: IocBean[testJob] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:271)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:161)
at org.nutz.integration.quartz.NutQuartzJobFactory.newJob(NutQuartzJobFactory.java:27)
at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375)
Caused by: java.lang.RuntimeException: IocBean[testJob] fail at field=[jedisPooler]
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
... 6 more
Caused by: org.nutz.ioc.IocException: IocBean[jedisPooler -> jedisConfig] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:64)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
... 8 more
Caused by: java.lang.RuntimeException: IocBean[jedisConfig] fail at field=[poolConfig]
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
... 14 more
Caused by: org.nutz.ioc.IocException: IocBean[jedisPoolConfig] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
... 16 more
Caused by: java.lang.NoClassDefFoundError: net/sourceforge/cobertura/coveragedata/LightClassmapListener
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.privateGetPublicMethods(Class.java:2917)
at java.lang.Class.getMethods(Class.java:1615)
at org.nutz.lang.reflect.FastClassFactory.create(FastClassFactory.java:75)
at org.nutz.lang.reflect.FastClassFactory.get(FastClassFactory.java:45)
at org.nutz.lang.reflect.FastClassFactory.get(FastClassFactory.java:63)
at org.nutz.lang.born.AbstractConstructorBorning.call(AbstractConstructorBorning.java:24)
at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:14)
at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:78)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:117)
... 19 more
Caused by: java.lang.ClassNotFoundException: net.sourceforge.cobertura.coveragedata.LightClassmapListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1722)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1573)
... 31 more
2020-05-19 15:02:30,006 [DefaultQuartzScheduler_Worker-10] INFO com.xy.quartz.TestJob - -----------TestJob---准备开始!----------------2020-5-19 15:2:30.6
java.lang.NullPointerException
at com.xy.quartz.TestJob.execute(TestJob.java:45)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)