什么不多说直接上Demo:
vo类:
package it.quartz.job.test;
import java.io.Serializable;
import java.util.Date;
public class SelVo implements Serializable{
private static final long serialVersionUID = 2746434425903993053L;
private String name;
private Date testDate;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getTestDate() {
return testDate;
}
public void setTestDate(Date testDate) {
this.testDate = testDate;
}
}
Job任务类:
package it.quartz.job.test;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyFirstJob {
private final String contextPath = PathHelper.getWEB_INF_Path()+"conf/quartz-test.xml";
private ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"file:"+contextPath});
public void excute(){
System.out.print(new SimpleDateFormat("YYYY-mm-dd HH:mm:ss").format(new Date())+"========Spring Job Hello World!");
SimpleJobLauncher launcher = (SimpleJobLauncher) ac.getBean("testLuncher");
Job job = (Job) ac.getBean("testJob");
JobParametersBuilder builder = (JobParametersBuilder) ac.getBean("paramsBuilder");
builder.addDate("date", new Date());
try {
launcher.run(job, builder.toJobParameters());
} catch (Exception e) {
e.printStackTrace();
}
}
}
RowMapper实现类:
package it.quartz.job.test;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class MyRowMapper implements RowMapper{
@Override
public SelVo mapRow(ResultSet rs, int i)
throws SQLException {
SelVo selVo = new SelVo();
selVo.setName(rs.getString("name"));
selVo.setTestDate(rs.getTimestamp("testDate"));
return selVo;
}
}
读到写的过程类:
package it.quartz.job.test;
import org.springframework.batch.item.ItemProcessor;
public class MyProcessor implements ItemProcessor{
@Override
public SelVo process(SelVo selVo) throws Exception {
return selVo;
}
}
获取web-inf路径的工具类:
package it.quartz.job.test;
public class PathHelper {
public static String getWEB_INF_Path(){
String osName = System.getProperty("os.name");
System.out.println("judging os system to be :"+osName);
String configPath = PathHelper.class.getResource("/").getPath();
if(configPath == null){
throw new RuntimeException("config path cannot be null");
}
if(osName.toLowerCase().contains("windows")){
configPath = configPath.replaceFirst("/", "");
configPath = configPath.replaceFirst("classes/", "");
}else{
configPath = configPath.replaceFirst("classes/", "");
}
return configPath;
}
}
web.xml配置:
SpringBatch
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
contextConfigLocation
/WEB-INF/conf/job-test.xml
org.springframework.web.context.ContextLoaderListener
定时任务配置文件:
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
spring批处理配置文件:
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">
cronExpression:* * * * * ?
* 代表任意
?代表年份
比如:0 */1 * * * ? 每一分钟的0秒执行一次
0 0 */1 * * ? 每一小时的0分0秒执行一次
0 0 23 L * ? 每月最后一天23点执行一次(L:最后的意思)
上面的spring批处理配置有点瑕疵哦,如果数据库是Oracle的话,JobRepositoryFactoryBean还要多加个属性'isolationLevelForCreate',完整的如下:
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">
事务隔离级别'ISOLATION_READ_COMMITTED'是要配的,因为Job工厂的默认事务隔离级别是'ISOLATION_SERIALIZABLE',这个事务级别在Job里很容易出现'无法连续访问此事务处理的异常'。
喜欢此文的朋友请点个赞哦!