spring Batch在SSH框架的应用
来源:互联网 发布:vi命令行编程 编辑:程序博客网 时间:2024/06/04 17:01
Spring Batch 是一个轻量级的、完善的批处理框架,旨在帮助企业建立健壮、高效的批处理应用。Spring Batch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使的已经使用 Spring 框架的开发者或者企业更容易访问和利用企业服务。 Spring Batch 供了大量可重用的组件,包括了日志、追踪、事务、任务作业统计、任务重启、跳过、重复、资源管理。对于大数据量和高性能的批处理任务,Spring Batch 同样ﰁ供了高级功能和特性来支持,比如分区功能、远程功能。总之,通过 Spring Batch 能够支持简单的、复杂的和大数据量的批处理作业。 Spring Batch 是一个批处理应用框架,不是调度框架,但需要和调度框架合作来构建完成的批处理任务。它只关注批处理任务相关的问题,如事务、并发、监控、执行等,并不提供相应的调度功能。
首先在spring配置文件中引入
applicationContext-dao.xml:
<import resource="batch.xml" />
<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"xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd "><bean id="jobLauncher"class="org.springframework.batch.core.launch.support.SimpleJobLauncher"><property name="jobRepository" ref="jobRepository" /></bean><bean id="jobRepository"class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"><property name="validateTransactionState" value="false" /></bean><bean id="itemProcessor" class="com.yc.batch.MessagesItemProcessor"scope="step"></bean> <batch:job id="writerclassJob"><batch:step id="stepwriter1"><batch:tasklet><batch:chunk reader="jdbcItemReader" writer="messagesItemWriter"processor="itemProcessor" commit-interval="10"></batch:chunk></batch:tasklet></batch:step></batch:job> <bean id="jdbcItemReader"class="org.springframework.batch.item.database.JdbcCursorItemReader"scope="step"><property name="dataSource" ref="dataSource" /><property name="sql"value="select distinct examineeclassid,classcount from work where checkdate >'${detail_startime}' and checkdate < '${detail_endtime}' " /><property name="rowMapper" ref="ledgerRowMapper"></property></bean> </beans>
这样配置就完成了。
下面就来完成后台Java代码的实现,主要原则是先进入一个mapper----》Processor----》write
下面就是这三个Java类的主要代码:
package com.yc.batch;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Component;import com.yc.vo.TeacherWorkdetail;import com.yc.vo.Workdetail;@Component("ledgerRowMapper") public class LedgerRowMapper implements RowMapper { @Overridepublic Object mapRow(ResultSet rs, int rowNum) throws SQLException { Workdetail twd=new Workdetail();twd.setClasscount(rs.getInt("classcount"));twd.setExamineeclassid(rs.getInt("examineeclassid")); return twd; }}
package com.yc.batch;import org.hibernate.engine.transaction.jta.platform.internal.SynchronizationRegistryBasedSynchronizationStrategy;import org.springframework.batch.item.ItemProcessor;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;import com.yc.vo.Workdetail;//业务层@Component("messagesItemProcessor")public class MessagesItemProcessor implements ItemProcessor<Workdetail, Workdetail> { public Workdetail process(Workdetail workdetail) throws Exception { return workdetail; } }
package com.yc.batch;import java.io.InputStream;import java.text.NumberFormat;import java.util.ArrayList;import java.util.List;import java.util.Properties;import javax.annotation.Resource;import org.springframework.batch.item.ItemWriter;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;import com.yc.biz.ExamineeClassBiz;import com.yc.biz.WorkBiz;import com.yc.utils.CsvUtils;import com.yc.vo.Workdetail;import net.sf.ehcache.util.PropertyUtil;//写@Component("messagesItemWriter")public class MessagesItemWriter implements ItemWriter<Workdetail>{@Resource(name = "workBiz")private WorkBiz workBiz;@Resource(name = "examineeClassBiz")private ExamineeClassBiz examineeClassBiz; public ExamineeClassBiz getExamineeClassBiz() {return examineeClassBiz;}public void setExamineeClassBiz(ExamineeClassBiz examineeClassBiz) {this.examineeClassBiz = examineeClassBiz;}public WorkBiz getWorkBiz() {return workBiz;}public void setWorkBiz(WorkBiz workBiz) {this.workBiz = workBiz;}public void write(List<? extends Workdetail> messages) throws Exception {//this.workBiz.updateWorkCheckcount();Properties props = new Properties();InputStream in= PropertyUtil.class.getClassLoader().getResourceAsStream("connectionConfig.properties");props.load(in);String startime=props.getProperty("detail_startime");String endtime=props.getProperty("detail_endtime");String time=props.getProperty("detail_time"); List<Object> works=new ArrayList<Object>();for(Workdetail work:messages){ Workdetail workdetail=new Workdetail(); Integer classid=work.getExamineeclassid(); int num=this.workBiz.getClassWorkNum(classid,startime,endtime); String classname=this.examineeClassBiz.findExamineeClassById(classid).getClassName(); int checkcount=this.workBiz.getWorkCheckcount(classid, startime, endtime); Double p=(double) Integer.parseInt(String.valueOf(checkcount/(num*(work.getClasscount()))*100)); workdetail.setExamineeclassid(classid); workdetail.setWorkcount(num); workdetail.setClasscount(work.getClasscount()); workdetail.setClassName(classname); workdetail.setCheckcount(checkcount); workdetail.setCompletionrate(p); works.add(workdetail);} CsvUtils cu=new CsvUtils(); String path=this.getClass().getResource("/").getPath(); //String path=Class.class.getClass().getResource("/").getPath(); path=path.substring(0,path.lastIndexOf("/")); path=path.substring(0,path.lastIndexOf("/")); path=path.substring(0,path.lastIndexOf("/")); path=path.substring(0,path.lastIndexOf("/")); cu.writeCsv(path+"/csv/class_"+time+".csv",works ); }}
通过这三个基本的类就基本跑起来了
在通过这个下面实现层的代码就可以获取到数据进行测试
CsvUtils cu=new CsvUtils();path=this.getClass().getResource("/").getPath();path=path.substring(0,path.lastIndexOf("/"));path=path.substring(0,path.lastIndexOf("/"));path=path.substring(0,path.lastIndexOf("/"));path=path.substring(0,path.lastIndexOf("/"));List<String[]> list=cu.readeCsv(path+"/csv/class_"+year+"-"+month+".csv");
也可以通过测试代码测试:
package com.yc.batch;import javax.annotation.Resource;import org.apache.commons.jexl2.Main;import org.springframework.batch.core.Job;import org.springframework.batch.core.JobExecution;import org.springframework.batch.core.JobParameters;import org.springframework.batch.core.JobParametersBuilder;import org.springframework.batch.core.JobParametersInvalidException;import org.springframework.batch.core.launch.JobLauncher;import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;import org.springframework.batch.core.repository.JobRestartException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class ClassBatch {private Job job;private JobLauncher launcher;@Resource(name="writerclassJob")public void setJob(Job job) {this.job = job;}@Autowired public void setLauncher(JobLauncher launcher) {this.launcher = launcher;}public void test() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException{JobParameters jobParameters =new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters();JobExecution result = launcher.run(job, jobParameters);}}
上面就是测试类
最后别忘记带入jar包:
<!-- spring batch --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-batch-core</artifactId> <version>2.1.8.RELEASE</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-batch-infrastructure</artifactId> <version>2.1.8.RELEASE</version></dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-batch-test</artifactId> <version>2.1.8.RELEASE</version></dependency>
通过上面的步骤我们就基本可以使用spring Batch进行我们的操作了。。。。。
阅读全文
1 0
- spring Batch在SSH框架的应用
- .Spring定时器在SSH框架中的应用
- Spring定时器在SSH框架中的应用
- Spring定时器在SSH框架中的应用
- Spring定时器在SSH框架中的应用
- Spring定时器在SSH框架中的应用
- Spring定时器在SSH框架中的应用
- ssh框架spring 简单应用
- Spring Batch框架介绍
- Spring Batch 批处理框架
- Spring Batch 批处理框架
- Spring batch批处理框架
- Spring Batch 批处理框架
- Spring Batch批处理框架
- JMS在Spring框架下的应用
- 在SSH框架中使用Spring的好处
- 在SSH框架中使用Spring的好处
- 在SSH框架中使用Spring的好处(面试总结)
- HDU6046-hash
- Please enter a commit message to explain why this merge is necessary
- dockertoolbox启动redis镜像
- LeetCode 217. Contains Duplicate
- mysql 修改某字段部分内容
- spring Batch在SSH框架的应用
- 基于ICE方式SIP信令穿透Symmetric NAT技术研究
- 点双联通分量模板
- Apache基本配置-3
- iOS_ UISearchBarDelegate
- 如何在springMVC 中对REST服务使用mockmvc 做测试
- springmvc+hibernate5 不能自动建表
- Log4Net(一):快速入门
- 【跟着stackoverflow学Pandas】