下面给大家详细的介绍下 定时任务的部署:
第一种方式
一. spring-context.xml配置加入
xmlns:task=”http://www.springframework.org/schema/task”
xsi:schemaLocation下面:http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
二. 继续spring-context.xml配置加入
<!– 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = “0 0 2 * * ?”)标注方法 –>
<task:executor id=”executor” pool-size=”10″/> <task:scheduler id=”scheduler” pool-size=”10″/>
<task:annotation-driven scheduler=”scheduler” executor=”executor” proxy-target-class=”true”/>
代码基于注解形式的task
package com.thinkgem.jeesite.modules.sys.service;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2016/6/22.
*/
@Service
@Lazy(false)
public class TaskJob2 {
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void job1() {
System.out.println("spring task 注解使用。。。任务进行中");
}
}
发布项目即可在输出行里看到输出结果
**
@Async // 异步
//@Scheduled(cron = "0 15 5 * * ?") //每天早上5:15触发
//@Scheduled(cron = "0/5 * * * * ? ")
public void testSpringTimer() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("开始执行 => " + simpleDateFormat.format(new Date()));}
package com.thinkgem.jeesite.modules.yanlink.duizhang.task;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 闫文超宝宝
*/ //http://blog.lunhui.ren/archives/280
//<!– 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = “0 0 2 * * ?”)标注方法 –>
//<task:executor id=”executor” pool-size=”10″/> <task:scheduler id=”scheduler” pool-size=”10″/>
//<task:annotation-driven scheduler=”scheduler” executor=”executor” proxy-target-class=”true”/>
@EnableScheduling()/*控制定时器是否生效*/
@Service
@Lazy(false)
@Controller
public class taksJob {
@Scheduled(cron = "0/5 * * * * ? ") //每5秒执行一次
public void job1() {
System.out.println("spring task 注解使用。。。任务进行中");
}
@Scheduled(cron = "0/5 * * * * ? ")
public void testSpringTimer(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("开始执行 => " + simpleDateFormat.format(new Date()));
}
public static void main(String[] args) {
taksJob taksJob = new taksJob();
taksJob.testSpringTimer();
taksJob.job1();
return;
}
}
这个启动tomcat才可启动 因为注解
上面这些都是比较常见的我记录下自己操作
我的配置路径在配置文件 想用定时任务 获取操作
html
@Value("${logPath.Log.address}")
private String htmlu;
@RequestMapping("/ts")
@ResponseBody
public String test(){
return htmlu;
}
@Async // 异步
//@Scheduled(cron = "0 15 5 * * ?") //每天早上5:15触发
@Scheduled(cron = "0/5 * * * * ? ")
public void testSpringTimer() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("开始执行 => " + simpleDateFormat.format(new Date()));
//
try {
System.out.println("配置文件路径:---" + htmlu);
ImportExcel ei = new ImportExcel(htmlu, 0, 0);
<!-- 加载配置属性文件 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath:jeesite.properties" />
<!-- 使用Annotation自动注册Bean,只扫描@Controller -->
<context:component-scan base-package="com.thinkgem.jeesite"
use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
logPath.Log.address=C:\\Users\\75763\\Desktop\\商户对账单\\shanghuduizhang.xls
这样基本就可以了 只是粗糙记录下 防止自己忘掉 后期会优化博客,如您有此方面有需求 希望可以帮助您 可以联系我