木头人的gravatar头像
木头人 2017-01-18 09:40:55

spring batch+quartz模拟定时将数据库表中的数据过滤备份(入门篇)

spring-batch的处理过程包括:1.读取数据;2.处理数据;3.输出数据

(1)数据映射:

@Component("userRowMapper")
public class UserRowMapper implements RowMapper<User>{

	public User mapRow(ResultSet result, int rowNum) throws SQLException {
		User user=new User();
		user.setName(result.getString("username"));
		user.setBirthday(result.getDate("birthday"));
		user.setSex(result.getString("sex"));
		user.setEmail(result.getString("email"));
		return user;
	}

}

(2)数据处理:

@Component("userProcessor")
public class UserProcessor implements ItemProcessor<User,User>{
	
	public User process(User user) throws Exception {
		//只保存90后的数据
		int year=user.getBirthday().getYear()+1900;
		if(year>=1990&&year<2000)
		{
			return user;
		}
		return null;
	}

}

(3)输出数据:

@Component("userWriter")
public class UserWriter implements ItemWriter<User> {
	
	@Autowired
	private IUserDao userDao;

	public void write(List<? extends User> users) throws Exception {
		for (User user : users) {
			userDao.saveUser(user);
		}
	}
}

(4)dao层的实现:

@Repository
public class UserDaoImpl implements IUserDao {

	private static final String SAVE_SQL = "insert into tuser_temp(username,sex,birthday,email)" + "values(?,?,?,?)";
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public void saveUser(final User user) {
		jdbcTemplate.update(SAVE_SQL, new PreparedStatementSetter() {
			public void setValues(PreparedStatement pst) throws SQLException {
				pst.setString(1, user.getName());
				pst.setString(2, user.getSex());
				pst.setDate(3, new java.sql.Date(user.getBirthday().getTime()));
				pst.setString(4, user.getEmail());
			}
		});
	}
}

(5)quartz-context.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/batch 
    http://www.springframework.org/schema/batch/spring-batch-2.1.xsd  
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<import resource="data-source-context.xml" />

	<!-- JOB REPOSITORY - WE USE IN-MEMORY REPOSITORY FOR OUR EXAMPLE -->
	<bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
	</bean>

	<!-- batch config -->
	<bean id="jobLauncher"
		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
		<property name="jobRepository" ref="jobRepository" />
	</bean>

	<!-- FINALLY OUR JOB DEFINITION. THIS IS A 1 STEP JOB -->
	<batch:job id="userJob">
		<batch:listeners>
			<batch:listener ref="appJobExecutionListener" />
		</batch:listeners>
		<batch:step id="step1">
			<batch:tasklet>
				<batch:listeners>
					<batch:listener ref="itemFailureLoggerListener" />
				</batch:listeners>
				<batch:chunk reader="userReader"  writer="userWriter" processor="userProcessor"
					commit-interval="10000" /> <!-- 1万条进行一次commit -->
			</batch:tasklet>
		</batch:step>
	</batch:job>

	<bean id="userReader"
		class="org.springframework.batch.item.database.JdbcCursorItemReader">
		<property name="dataSource" ref="dataSource" />
		<property name="sql" value="select * from tuser" />
		<property name="rowMapper" ref="userRowMapper" />
	</bean>

	<!-- Spring Batch Job同一个job instance,成功执行后是不允许重新执行的【失败后是否允许重跑,可通过配置Job的restartable参数来控制,默认是true】,如果需要重新执行,可以变通处理, 
		添加一个JobParameters构建类,以当前时间作为参数,保证其他参数相同的情况下却是不同的job instance -->
	<bean id="jobParameterBulider" class="org.springframework.batch.core.JobParametersBuilder" />

	<!-- 定时任务 开始 -->
	<bean id="userJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<!-- 定时执行的类 -->
			<ref bean="quartzUserJob" />
		</property>
		<property name="targetMethod">
			<!-- 定时执行的类方法 -->
			<value>execute</value>
		</property>
	</bean>

	<bean id="userCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<!-- 这里不可以直接在属性jobDetail中引用taskJob,因为他要求的是一个jobDetail类型的对象,所以我们得通过MethodInvokingJobDetailFactoryBean来转一下 -->
		<property name="jobDetail">
			<ref bean="userJobDetail" />
		</property>
		
		<property name="cronExpression">
			<value>0 51 9 * * ? * </value>
		</property>

	</bean>

	<!-- 触发器工厂,将所有的定时任务都注入工厂 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<!-- 添加触发器 -->
		<property name="triggers">
			<list>
				<!-- 将上面定义的测试定时任务注入(可以定义多个定时任务,同时注入) -->
				<ref local="userCronTrigger" />
			</list>
		</property>
	</bean>
	<!-- 定时任务 结束 -->
</beans>

(6)数据库表的数据:

spring batch+quartz模拟定时将数据库表中的数据过滤备份(入门篇)

(7)输出到备份表中的数据:

spring batch+quartz模拟定时将数据库表中的数据过滤备份(入门篇)


打赏

文件名:springBatch2.zip,文件大小:40.392K 下载
最代码最近下载分享源代码列表最近下载
changup  LV6 2022年2月2日
雾岛听风  LV7 2021年10月20日
v512345  LV10 2020年6月19日
zhwang  LV19 2020年6月17日
zhangdd  LV10 2020年6月4日
lovejing  LV7 2020年5月16日
dcowl123  LV1 2020年3月17日
liudeshuai970926  LV6 2020年3月2日
laowantong260  LV7 2020年1月10日
苏小六  LV1 2019年6月1日
最代码最近浏览分享源代码列表最近浏览
Gin19960217  LV4 3月18日
lironggang  LV38 2023年3月28日
F丶S丶H  LV7 2023年1月29日
xuexizhuanyong23  LV16 2022年10月18日
pxqtsht  LV15 2022年3月21日
fangen0005  LV25 2022年3月9日
changup  LV6 2022年2月2日
huaua7676  LV30 2022年1月23日
雾岛听风  LV7 2021年10月20日
cc900118  LV17 2021年8月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友