您的位置:首页 > 编程语言 > Java开发

spring3.0定时任务

2015-11-08 22:56 411 查看
上一篇文章写到使用java定时任务,其实,spring也实现了定时任务,使用也很简单,下面来学习一下。

Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种。

配置文件:

applicationContext-schedual.xml

<!-- spring扫描注解的配置 -->
<context:component-scan base-package="com.middle.jobtask" />
<!-- 定义调用对象和调用对象的方法 -->
<bean id="wordsWareHouseTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<span style="white-space:pre">	</span><property name="targetObject" ref="wordsWareHouseTask" />
<property name="targetMethod" value="run" />
</bean>
<!-- 定义触发时间 -->
<bean id="wordsWareHouseTaskCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="wordsWareHouseTaskJobDetail" />
<!--  cron表达式 -->
<property name="cronExpression" value="0 0 0/5 * * ?" />
</bean>

<!-- vproduct 重建索引 调度业务 -->
<bean id="vproductSearchIndexIngTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="vproductSearchIndexIngTask" />
<property name="targetMethod" value="run" />
</bean>
<!-- vproduct 重建索引 调度触发器 -->
<bean id="vproductSearchIndexIngTaskCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="vproductSearchIndexIngTaskJobDetail" />
<property name="cronExpression" value="0 30 1 * * ?" />
</bean>
<!-- 线程执行器配置,用于任务注册 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="queueCapacity" value="500" />
</bean>
<!-- 设置调度 --><span style="color: rgb(0, 130, 0); font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 13.75px; white-space: pre;"><!--  总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  --></span>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<span style="white-space:pre">	</span><ref bean="wordsWareHouseTaskCronTrigger" />
<ref bean="vproductSearchIndexIngTaskCronTrigger" />
</list>
</property>
<property name="taskExecutor" ref="taskExecutor" />
</bean>


CronExpression的说明:

字段允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
表达式意义
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
每天早上6点
0 6 * * *
每两个小时
0 */2 * * *
晚上11点到早上7点之间每两个小时,早上八点
0 23-7/2,8 * * *
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * 1-3
1月1日早上4点
0 4 1 1 *


定时任务所执行的类:

@Component
public class VproductSearchIndexIngTask
{

private static final Logger LOGGER = LoggerFactory
.getLogger(VproductSearchIndexIngTask.class);
/**
* 单个solr上需要增量更新的core
*/
@Resource(name="httpSolrClient")
private SolrClient solrClient;

public void run()
{
try
{
SolrRequest<QueryResponse> request = buildRequest(Command.FULL_IMPORT);
request.setPath("/dataimport");
NamedList<Object> resp = solrClient.request(request, "vproduct");
LOGGER.info("vproduct 重建索引成功,具体结果:"+resp.toString());
} catch (Exception e)
{
LOGGER.error("vproduct 重建索任务失败:异常信息:" + e.getMessage());
}
}

private SolrRequest<QueryResponse> buildRequest(Command command)
{
Map<String, String> map = new HashMap<>();
switch (command)
{
case FULL_IMPORT:
map.put("command", "full-import");
map.put("clean", "true");
map.put("commit", "true");
map.put("wt", "json");
map.put("indent", "false");
map.put("entity", "VProductInfo");
map.put("optimize", "false");
map.put("debug", "false");
return new QueryRequest(new MapSolrParams(map));
case DELTA_IMPORT:
map.put("command", "delta-import");
map.put("clean", "false");
map.put("commit", "true");
map.put("wt", "json");
map.put("indent", "false");
map.put("entity", "VProductInfo");
map.put("optimize", "false");
map.put("debug", "false");
return new QueryRequest(new MapSolrParams(map));
}
return null;
}
}


上面是一个solr搜索的定时全量导入数据类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: