我在ioc资源目录下加入了redis.js
var ioc = {
// 参考 https://github.com/xetorthio/jedis/wiki/Getting-started
jedisPoolConfig : {
type : "redis.clients.jedis.JedisPoolConfig",
fields : {
testWhileIdle : true, // 空闲时测试,免得redis连接空闲时间长了断线
maxTotal : {java : "$conf.getInt('redis.maxTotal', 100)"} // 一般都够了吧
}
},
jedisPool : {
type : "redis.clients.jedis.JedisPool",
args : [
{refer : "jedisPoolConfig"},
// 从配置文件中读取redis服务器信息
{java : "$conf.get('redis.host', 'test-38.dev.com')"},
{java : "$conf.getInt('redis.port', 6379)"},
{java : "$conf.getInt('redis.timeout', 2000)"},
{java : "$conf.get('redis.password','hellorokid')"},
{java : "$conf.getInt('redis.database', 0)"}
],
fields : {},
events : {
depose : "destroy" // 关闭应用时必须关掉呢
}
},
jedisClusterNodes : {
type : "java.util.HashSet",
args : [
[{
type : "redis.clients.jedis.HostAndPort",
args : [
{java : "$conf.get('redis.host', 'test-38.dev.com')"},
{java : "$conf.getInt('redis.port', 6379)"}
]
}]
]
},
jedisCluster : {
type : "redis.clients.jedis.JedisCluster",
args : [
{refer:"jedisClusterNodes"},
{refer:"jedisPoolConfig"}
],
events : {
depose : "close"
}
},
redis : {
type : "org.nutz.integration.jedis.RedisInterceptor",
fields : {
jedisAgent : {refer:"jedisAgent"}
}
},
redisService : {
type : "org.nutz.integration.jedis.RedisService"
},
pubSubService : {
type : "org.nutz.integration.jedis.pubsub.PubSubService",
fields : {
jedisAgent : {refer:"jedisAgent"}
},
events : {
depose : "depose"
}
},
jedisAgent : {
type : "org.nutz.integration.jedis.JedisAgent",
fields : {
ioc : {refer:"$ioc"},
conf : {refer:"conf"}
}
},
jedisClusterWrapper : {
type : "org.nutz.integration.jedis.JedisClusterWrapper",
args : [{refer:"jedisCluster"}]
}
};
然后在MainModule那边的IocBy里面也加入了"*jedis",但是在MainSetup那边使用
JedisAgent jedisAgent = ioc.get(JedisAgent.class);
try (Jedis jedis = jedisAgent.getResource()) { // Java7的语法
String re = jedis.set("_nutzbook_test_key", "http://nutzbook.wendal.net");
log.debug("redis say : " + re);
re = jedis.get("_nutzbook_test_key");
log.debug("redis say : " + re);
} finally {
}
报错了,错误如下
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:53)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:99)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:12)
at org.nutz.integration.jedis.JedisAgent.jedis(JedisAgent.java:49)
at org.nutz.integration.jedis.JedisAgent.getResource(JedisAgent.java:54)
at com.rokid.weather.api.common.core.Setup.init(Setup.java:42)
at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:277)
at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
at org.nutz.mvc.NutFilter.init(NutFilter.java:65)