首页 技术 正文
技术 2022年11月19日
0 收藏 370 点赞 2,924 浏览 3726 个字

Spring框架提供了TaskExcutor的异步执行和TashScheduler的任务定时执行接口,同样spring也提供了线程池或者CommonJ的代理。

TaskExecutor的类型

SimpleAsyncTaskExecutor,没有复用线程,当触发时仅仅启动一个新的线程。支持并发。

SyncTaskExecutor,同步触发,主要用来不需要多线程的情况,例如测试用例

ConcurrentTaskExecutor,java.util.concurrent.Executor的适配器,作为ThreadPoolTaskExecutor的替代品,很少使用,但如果ThreadPoolTaskExecutor不能满足你的需求,可以考虑使用它。

SimpleThreadPoolTaskExecutor,Quartz的SimpleThreadPool子类实现,它监听spring生命周期的callback,主要用来在quartz和非quartz组件共享线程池时。

ThreadPoolTaskExecutor,常用的TaskExecutor实现类。

WorkManagerTaskExecutor,使用了CommJ的WorkMannager作为它的实现,它提供了在spring contxt中建立一个CommonJ WorkManger的便利主类,类似于SimpleThreadPoolTaskExecutor。

注:CommonJ是BEA和IBM开发的

TaskExecutor使用示例:

import org.springframework.core.task.TaskExecutor;public class TaskExecutorExample {    private class MessagePrinterTask implements Runnable {        private String message;        public MessagePrinterTask(String message) {
this.message = message;
} public void run() {
System.out.println(message);
} } private TaskExecutor taskExecutor; public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
} public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask("Message" + i));
}
}}

配置文件:

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="25" />
</bean><bean id="taskExecutorExample" class="TaskExecutorExample">
<constructor-arg ref="taskExecutor" />
</bean>

使用Quartz Scheduler

1. 使用JobDetailFactoryBean

<bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="example.ExampleJob"/>
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="5"/>
</map>
</property>
</bean>

测试类

package example;public class ExampleJob extends QuartzJobBean {    private int timeout;    /**
* Setter called after the ExampleJob is instantiated
* with the value from the JobDetailFactoryBean (5)
*/
public void setTimeout(int timeout) {
this.timeout = timeout;
} protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
// do the actual work
}}

2. 使用MethodInvokingJobDetailFactoryBean

配置文件

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="exampleBusinessObject"/>
<property name="targetMethod" value="doIt"/>
</bean>

对应的方法

public class ExampleBusinessObject {    // properties and collaborators    public void doIt() {
// do the actual work
}
}
<bean id="exampleBusinessObject" class="examples.ExampleBusinessObject"/>

有状态的job,一个任务没完成之前,相同任务的下个不会执行

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="exampleBusinessObject"/>
<property name="targetMethod" value="doIt"/>
<property name="concurrent" value="false"/>
</bean>

3. 使用triggers 和SchedulerFactoryBean包装job

两种类型的TriggerFactoryBean,分别是SimpleTriggerFactoryBean和CronTriggerFactoryBean

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<!-- see the example of method invoking job above -->
<property name="jobDetail" ref="jobDetail"/>
<!-- 10 seconds -->
<property name="startDelay" value="10000"/>
<!-- repeat every 50 seconds -->
<property name="repeatInterval" value="50000"/>
</bean><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="exampleJob"/>
<!-- run every morning at 6 AM -->
<property name="cronExpression" value="0 0 6 * * ?"/>
</bean>

建立SchedulerFactoryBean

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
<ref bean="simpleTrigger"/>
</list>
</property>
</bean>

参考文献:

【1】http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

【2】http://www.cnblogs.com/davidwang456/p/4237895.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,129
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,601
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,444
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,218
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,852
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,940