spring 定时任务

博文摘自本人项目


1. code ,定时执行的方法

package com.thunisoft.webservice.service;


import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import com.thunisoft.summer.util.cache.CacheManager;
import com.thunisoft.webservice.cache.cacheManager;
import com.thunisoft.webservice.dao.Ify2000;
import com.thunisoft.webservice.daoImpl.Fy2000Impl;
import com.thunisoft.webservice.service.util.getBean;

public class Fy2000Service  extends ServletEndpointSupport  implements IFy2000Service{

private Ify2000 fyimpl;
private Logger log = Logger.getLogger(Fy2000Service.class);

private String queryParams;//参数

/**
* 将客户端请求的查询结果返回
* @return
*/
public  String ajNum(String queryParam){
Map<String,String> csmap = new HashMap<String,String>();
Map<Integer,Integer> jcmap = new HashMap<Integer,Integer>();
Map<Integer,Integer> xsmap = new HashMap<Integer,Integer>();
Map<Integer,Integer> yjmap = new HashMap<Integer,Integer>();
Map<Integer,Integer> wjmap = new HashMap<Integer,Integer>();
ApplicationContext springcontext = getBean.getYouBean();
fyimpl= (Fy2000Impl) springcontext.getBean("fy2000");
csmap = fyimpl.readXml(queryParam);
String kssj = csmap.get("kssj");
String jssj = csmap.get("jssj");
jcmap = fyimpl.getJcMap(kssj,jssj);
xsmap = fyimpl.getXsMap(kssj, jssj);
yjmap = fyimpl.getYjMap(kssj, jssj);   
wjmap = fyimpl.getWjMap(kssj, jssj);
Map<Integer,Integer> fymap=null;
try {
fymap = ((cacheManager)CacheManager.getCache("fycache")).getFyvalueMap();
} catch (Exception e) {
log.error("获取法院缓存数据失败",e);
}
StringBuffer sb  = new StringBuffer();
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
sb.append("<result>");
for (Entry<Integer, Integer> enter:fymap.entrySet()) {
Integer fydm = enter.getKey();
if(null!=fydm){
Integer fyid = enter.getValue();
sb.append("<fy>");
sb.append("<fybh>"+fydm+"</fybh>");
sb.append("<xsajs>"+xsmap.get(fyid)+"</xsajs>");
sb.append("<yjajs>"+yjmap.get(fyid)+"</yjajs>");
sb.append("<wjajs>"+wjmap.get(fyid)+"</wjajs>");
sb.append("<jcajs>"+jcmap.get(fyid)+"</jcajs>");
sb.append("</fy>");
}
}
sb.append("</result>");

return sb.toString();
}
public Ify2000 getFyimpl() {
return fyimpl;
}
public void setFyimpl(Ify2000 fyimpl) {
this.fyimpl = fyimpl;
}

public void backFyajxx(){

String queryParam = this.queryParam;

ajNum(queryParam);

}

public String queryParam(){

return queryParam;

}

public void setQueryParam(String queryParam){

this.queryParam = queryParam;

}
}






2. applicationContext.xml 配置文件

<!-- 实现类 -->
<bean id = "fy2000" class="com.thunisoft.webservice.daoImpl.Fy2000Impl">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property> 
</bean>
<!-- 服务类 -->
<bean id = "fy2000service" class="com.thunisoft.webservice.service.Fy2000Service">
<property name="fyimpl">
<ref bean="fy2000"></ref>
</property>

<property name ="queryParam">

<value>${service.queryParam}</value>
</bean>

<!-- 定时任务 service-->
<bean id ="fy2000ServiceTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="fy2000service"></property>

<!--根据spring定时任务规范,定时执行方法应为无参无返回值方法! -->

<property name="targetMethod" value="backFyajxx"></property>
</bean>
<bean id ="fy2000ServiceTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="fy2000ServiceTask"></ref>
</property>
<property name="cronExpression">
<value>${service.cron}</value>
</property>
</bean>

<!-- 定时任务单 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="fy2000ServiceTrigger" />
            </list>
        </property>
    </bean>


3.配置文件 config.properties   

#webservice接口参数
service.queryParam=select * from .....
#司法公开今日动态cron表达式,每天早7点到晚7点每小时执行一次
service.cron=0 0 0 * * ?


4.所需jar包

quartz-1.6.*.jar 

spring-2.*.jar

.......

摘自网络;

cron表达式详解 

一个cron表达式有至少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下: 
1.秒(0-59) 
2.分钟(0-59) 
3.小时(0-23) 
4.月份中的是期(1-31) 
5.月份(1-12或SUN-DEC) 
6.星期中的日期(1-7或SUN-SAT) 
7.年份(1970-2099) 
例子: 
0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点 
0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟 
30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时 
0 0 8-5 ? * MON-FRI 每个工作日的工作时间 
- 区间 
* 通配符 
? 你不想设置那个字段


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值