Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
后端镜像
java镜像
Springside4
提交
7cc72a01
S
Springside4
项目概览
后端镜像
/
java镜像
/
Springside4
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Springside4
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
7cc72a01
编写于
7月 12, 2013
作者:
C
Calvin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#221 加入对Jedis的封装,代码改进与注释
上级
d842e552
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
61 addition
and
29 deletion
+61
-29
examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/job/JobListenerDemo.java
...de/examples/showcase/demos/redis/job/JobListenerDemo.java
+1
-1
modules/extension/src/main/java/org/springside/modules/nosql/redis/JedisScriptExecutor.java
...g/springside/modules/nosql/redis/JedisScriptExecutor.java
+10
-3
modules/extension/src/main/java/org/springside/modules/nosql/redis/scheduler/JobDispatcher.java
...ringside/modules/nosql/redis/scheduler/JobDispatcher.java
+27
-14
modules/extension/src/main/java/org/springside/modules/nosql/redis/scheduler/JobListener.java
...springside/modules/nosql/redis/scheduler/JobListener.java
+6
-6
modules/extension/src/main/java/org/springside/modules/nosql/redis/scheduler/JobManager.java
.../springside/modules/nosql/redis/scheduler/JobManager.java
+5
-5
modules/extension/src/main/java/org/springside/modules/nosql/redis/scheduler/Keys.java
...va/org/springside/modules/nosql/redis/scheduler/Keys.java
+12
-0
未找到文件。
examples/showcase/src/main/java/org/springside/examples/showcase/demos/redis/job/JobListenerDemo.java
浏览文件 @
7cc72a01
...
...
@@ -53,7 +53,7 @@ public class JobListenerDemo implements JobHandler {
if
(
c
==
'\n'
)
{
System
.
out
.
println
(
"Shutting down"
);
threadPool
.
shutdownNow
();
boolean
shutdownSucess
=
threadPool
.
awaitTermination
(
JobListener
.
POPUP_TIMEOUT
+
1
,
boolean
shutdownSucess
=
threadPool
.
awaitTermination
(
JobListener
.
DEFAULT_
POPUP_TIMEOUT
+
1
,
TimeUnit
.
SECONDS
);
if
(!
shutdownSucess
)
{
...
...
modules/extension/src/main/java/org/springside/modules/nosql/redis/JedisScriptExecutor.java
浏览文件 @
7cc72a01
...
...
@@ -58,6 +58,9 @@ public class JedisScriptExecutor {
*/
public
Object
execute
(
final
String
hash
,
final
List
<
String
>
keys
,
final
List
<
String
>
args
)
throws
IllegalArgumentException
{
final
long
start
=
System
.
currentTimeMillis
();
if
(!
hashScriptMap
.
containsKey
(
hash
))
{
throw
new
IllegalArgumentException
(
"Script hash "
+
hash
+
" is not loaded in executor。"
);
}
...
...
@@ -66,12 +69,16 @@ public class JedisScriptExecutor {
return
jedisTemplate
.
execute
(
new
JedisAction
<
Object
>()
{
@Override
public
Object
action
(
Jedis
jedis
)
{
return
jedis
.
evalsha
(
hash
,
keys
,
args
);
Object
result
=
jedis
.
evalsha
(
hash
,
keys
,
args
);
logger
.
debug
(
"Script hash {} execution time is {}ms"
,
hash
,
System
.
currentTimeMillis
()
-
start
);
return
result
;
}
});
}
catch
(
JedisDataException
e
)
{
logger
.
warn
(
"Lua execution error, try to reload the script."
,
e
);
return
reloadAndExecute
(
hash
,
keys
,
args
);
logger
.
warn
(
"Script hash {} is not loaded yet, try to reload and run it again"
,
hash
,
e
);
Object
result
=
reloadAndExecute
(
hash
,
keys
,
args
);
logger
.
debug
(
"Script hash {} reload and execution time is {}ms"
,
hash
,
System
.
currentTimeMillis
()
-
start
);
return
result
;
}
}
...
...
modules/extension/src/main/java/org/springside/modules/nosql/redis/scheduler/JobDispatcher.java
浏览文件 @
7cc72a01
...
...
@@ -11,7 +11,6 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.springframework.core.io.DefaultResourceLoader
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springside.modules.nosql.redis.JedisScriptExecutor
;
import
org.springside.modules.utils.Threads.WrapExceptionRunnable
;
...
...
@@ -19,12 +18,17 @@ import redis.clients.jedis.JedisPool;
import
com.google.common.collect.Lists
;
/**
* 定时分发任务。 启动线程定时从sleeping job sorted set 中取出到期的任务放入ready job list.
*
* @author calvin
*/
public
class
JobDispatcher
implements
Runnable
{
public
static
final
String
DEFAULT_DISPATCH_LUA_FILE
=
"classpath:/redis/dispatch.lua"
;
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
JobDispatcher
.
class
);
private
ScheduledExecutorService
t
hreadPool
;
private
ScheduledExecutorService
scheduledT
hreadPool
;
private
JedisScriptExecutor
scriptExecutor
;
...
...
@@ -37,31 +41,39 @@ public class JobDispatcher implements Runnable {
}
public
JobDispatcher
(
String
jobName
,
JedisPool
jedisPool
,
String
scriptPath
)
{
keys
=
Lists
.
newArrayList
(
Keys
.
getSleepingJobKey
(
jobName
),
Keys
.
getReadyJobKey
(
jobName
));
this
.
scriptExecutor
=
new
JedisScriptExecutor
(
jedisPool
);
keys
=
Lists
.
newArrayList
(
jobName
+
".job:sleeping"
,
jobName
+
".job:ready"
);
loadLuaScript
(
scriptPath
);
}
private
void
loadLuaScript
(
String
scriptPath
)
{
ResourceLoader
resourceLoader
=
new
DefaultResourceLoader
();
Resource
resource
=
resourceLoader
.
getResource
(
scriptPath
);
String
script
;
try
{
String
script
=
FileUtils
.
readFileToString
(
resource
.
getFile
()
);
script
Hash
=
scriptExecutor
.
load
(
script
);
Resource
resource
=
new
DefaultResourceLoader
().
getResource
(
scriptPath
);
script
=
FileUtils
.
readFileToString
(
resource
.
getFile
()
);
}
catch
(
IOException
e
)
{
throw
new
Illegal
StateException
(
DEFAULT_DISPATCH_LUA_FILE
+
"not exist
"
,
e
);
throw
new
Illegal
ArgumentException
(
scriptPath
+
" is not exist.
"
,
e
);
}
scriptHash
=
scriptExecutor
.
load
(
script
);
}
/**
* 启动分发线程, 自行创建scheduler线程池.
*/
public
void
start
(
long
periodMilliseconds
)
{
threadPool
=
Executors
.
newScheduledThreadPool
(
1
);
threadPool
.
scheduleAtFixedRate
(
new
WrapExceptionRunnable
(
this
),
0
,
periodMilliseconds
,
TimeUnit
.
MILLISECONDS
);
this
.
scheduledThreadPool
=
Executors
.
newScheduledThreadPool
(
1
);
scheduledThreadPool
.
scheduleAtFixedRate
(
new
WrapExceptionRunnable
(
this
),
0
,
periodMilliseconds
,
TimeUnit
.
MILLISECONDS
);
}
/**
* 停止分发任务, 默认最多延时10秒等候线程关闭.
*/
public
void
stop
()
{
t
hreadPool
.
shutdownNow
();
scheduledT
hreadPool
.
shutdownNow
();
try
{
if
(!
threadPool
.
awaitTermination
(
5
,
TimeUnit
.
SECONDS
))
{
if
(!
scheduledThreadPool
.
awaitTermination
(
10
,
TimeUnit
.
SECONDS
))
{
logger
.
error
(
"Job dispatcher terminate failed!"
);
}
}
catch
(
InterruptedException
e
)
{
...
...
@@ -69,12 +81,13 @@ public class JobDispatcher implements Runnable {
}
}
/**
* 以当前时间为参数执行Lua Script分发任务。
*/
@Override
public
void
run
()
{
long
currTime
=
System
.
currentTimeMillis
();
List
<
String
>
args
=
Lists
.
newArrayList
(
String
.
valueOf
(
currTime
));
scriptExecutor
.
execute
(
scriptHash
,
keys
,
args
);
long
luaExecTime
=
System
.
currentTimeMillis
()
-
currTime
;
logger
.
debug
(
"Execution Time={}ms."
,
luaExecTime
);
}
}
modules/extension/src/main/java/org/springside/modules/nosql/redis/scheduler/JobListener.java
浏览文件 @
7cc72a01
...
...
@@ -18,21 +18,21 @@ import redis.clients.jedis.Jedis;
import
redis.clients.jedis.JedisPool
;
/**
*
This is the Redis implementation of SchedulerManager
.
*
阻塞接收任务的Runnable
.
*/
public
class
JobListener
implements
Runnable
{
public
static
final
int
POPUP_TIMEOUT
=
5
;
public
static
final
int
DEFAULT_
POPUP_TIMEOUT
=
5
;
private
String
readyJob
Name
;
private
String
readyJob
Key
;
private
JedisTemplate
jedisTemplate
=
null
;
private
JedisTemplate
jedisTemplate
;
private
final
JobHandler
jobHandler
;
public
JobListener
(
String
jobName
,
JedisPool
jedisPool
,
JobHandler
jobHandler
)
{
jedisTemplate
=
new
JedisTemplate
(
jedisPool
);
readyJob
Name
=
jobName
+
".job:ready"
;
readyJob
Key
=
Keys
.
getReadyJobKey
(
jobName
)
;
this
.
jobHandler
=
jobHandler
;
}
...
...
@@ -42,7 +42,7 @@ public class JobListener implements Runnable {
@Override
public
void
action
(
Jedis
jedis
)
{
while
(!
Thread
.
currentThread
().
isInterrupted
())
{
List
<
String
>
nameValuePair
=
jedis
.
brpop
(
POPUP_TIMEOUT
,
readyJobName
);
List
<
String
>
nameValuePair
=
jedis
.
brpop
(
DEFAULT_POPUP_TIMEOUT
,
readyJobKey
);
if
((
nameValuePair
!=
null
)
&&
!
nameValuePair
.
isEmpty
())
{
String
job
=
nameValuePair
.
get
(
1
);
jobHandler
.
handleJob
(
job
);
...
...
modules/extension/src/main/java/org/springside/modules/nosql/redis/scheduler/JobManager.java
浏览文件 @
7cc72a01
...
...
@@ -24,13 +24,13 @@ public class JobManager {
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
JobManager
.
class
);
private
String
sleepingJob
Name
;
private
String
sleepingJob
Key
;
private
JedisTemplate
jedisTemplate
;
public
JobManager
(
String
jobName
,
JedisPool
jedisPool
)
{
jedisTemplate
=
new
JedisTemplate
(
jedisPool
);
sleepingJob
Name
=
jobName
+
".job:sleeping"
;
sleepingJob
Key
=
Keys
.
getSleepingJobKey
(
jobName
)
;
}
/**
...
...
@@ -38,14 +38,14 @@ public class JobManager {
*/
public
void
scheduleJob
(
final
String
job
,
final
long
delay
,
final
TimeUnit
timeUnit
)
{
final
long
delayTimeInMillisecond
=
System
.
currentTimeMillis
()
+
timeUnit
.
toMillis
(
delay
);
jedisTemplate
.
zadd
(
sleepingJob
Name
,
delayTimeInMillisecond
,
job
);
jedisTemplate
.
zadd
(
sleepingJob
Key
,
delayTimeInMillisecond
,
job
);
}
/**
* 取消任务,
如果任务不存在或已
触发返回false, 否则返回true.
* 取消任务,
如果任务不存在或已被
触发返回false, 否则返回true.
*/
public
boolean
cancelJob
(
final
String
job
)
{
boolean
removed
=
jedisTemplate
.
zrem
(
sleepingJob
Name
,
job
);
boolean
removed
=
jedisTemplate
.
zrem
(
sleepingJob
Key
,
job
);
if
(!
removed
)
{
logger
.
warn
(
"Can't cancel job by value {}"
,
job
);
...
...
modules/extension/src/main/java/org/springside/modules/nosql/redis/scheduler/Keys.java
0 → 100644
浏览文件 @
7cc72a01
package
org.springside.modules.nosql.redis.scheduler
;
public
class
Keys
{
public
static
String
getSleepingJobKey
(
String
jobName
)
{
return
new
StringBuilder
().
append
(
"job:"
).
append
(
jobName
).
append
(
":sleepingjob"
).
toString
();
}
public
static
String
getReadyJobKey
(
String
jobName
)
{
return
new
StringBuilder
().
append
(
"job:"
).
append
(
jobName
).
append
(
":sleepingjob"
).
toString
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录