我是Spring Batch的新手。我只是尝试运行一个简单的弹簧批处理示例,我从网上得到了徒劳。示例只是尝试从平面文件中读取并将内容写入单独的平面文件。
下面是我的xml,它被加载到上下文中(通过ContextLoaderListener)。
测试batch.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://172.18.7.57/spring-beans.dtd">
<beans>
<bean id="simpleJob"
class="org.springframework.batch.core.job.SimpleJo b">
<property name="name" value="simpleJob" />
<property name="steps">
<list>
<ref local="step" />
</list>
</property>
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.suppo rt.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.s upport.SimpleJobRepository">
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.d ao.MapJobInstanceDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.d ao.MapJobExecutionDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.d ao.MapStepExecutionDao" />
</constructor-arg>
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transacti on.ResourcelessTransactionManager" />
<bean id="step"
class="org.springframework.batch.core.step.item.Si mpleStepFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="jobRepository" ref="jobRepository" />
<property name="itemReader" ref="itemReader" />
<property name="itemWriter" ref="itemWriter" />
</bean>
<bean id="lineAggregator"
class="org.springframework.batch.item.file.transfo rm.DelimitedLineAggregator">
<property name="delimiter" value="|" />
</bean>
<bean id="itemWriter"
class="org.springframework.batch.item.file.FlatFil eItemWriter">
<property name="fieldSetCreator" ref="fieldSetMapper" />
<property name="lineAggregator" ref="lineAggregator" />
<property name="resource" value="file:/hello2.txt" />
</bean>
<bean id="lineTokenizer"
class="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer" />
<bean id="fieldSetMapper"
class="org.springframework.batch.item.file.mapping .PassThroughFieldSetMapper" />
<bean id="itemReader"
class="org.springframework.batch.item.file.FlatFil eItemReader">
<property name="resource" value="file:/hello1.txt" />
<property name="lineTokenizer" ref="lineTokenizer" />
<property name="fieldSetMapper" ref="fieldSetMapper" />
</bean>
</beans>
由于我没有maven,我尝试从我的java代码中启动这项工作,如
ApplicationContext context = new FileSystemXmlApplicationContext("D:\\jboss-4.0.5.GA\\server\\default\\deploy\\test.war\\WEB-INF\\xml\\test-batch.xml");
Job job;
job = (Job) context.getBean("simpleJob");
JobParameters jobParameters = jobParametersConverter.getJobParameters(StringUtils
.splitArrayElementsIntoProperties(new String[]{"D:\\jboss-4.0.5.GA\\server\\default\\deploy\\venki.war\\WEB-INF\\xml\\venki-batch.xml","simpleJob"}, "="));
JobExecution jobExecution = launcher.run(job, jobParameters);
- 但是,在eclipse中抛出了一些错误,说没有找到类加载器....下面是我得到的错误....
org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException: No ClassLoaders found for: org.springframework.batch.core.JobExecution
at org.apache.commons.lang.SerializationUtils.deseria lize(SerializationUtils.java:164)
at org.apache.commons.lang.SerializationUtils.deseria lize(SerializationUtils.java:191)
at org.springframework.batch.core.repository.dao.MapJ obExecutionDao.copy(MapJobExecutionDao.java:33)
at org.springframework.batch.core.repository.dao.MapJ obExecutionDao.saveJobExecution(MapJobExecutionDao .java:56)
at org.springframework.batch.core.repository.support. SimpleJobRepository.saveOrUpdate(SimpleJobReposito ry.java:216)
at org.springframework.batch.core.repository.support. SimpleJobRepository.createJobExecution(SimpleJobRe pository.java:192)
at org.springframework.batch.core.launch.support.Simp leJobLauncher.run(SimpleJobLauncher.java:79)
at com.venki.handler.TestHandler.testSpringBatch(Test Handler.java:111)
- 请帮助我这方面......我需要找到一个从平面文件中读取并插入数据库的代码....我需要将其集成到一个独立的模块中,我不应该和maven一起去....
答案 0 :(得分:0)
为什么不使用org.springframework.batch.core.launch.support.CommandLineJobRunner
来运行它
您可以在eclise的运行配置中将其定义为主类
在参数中给出
<your batch-job-xml-file><space><your Job (in your case its simpleJob)>
你也不需要maven。只需设置所需库的类路径。
答案 1 :(得分:0)
示例Spring Batch football项目从CSV文件读取并使用JDBC写入数据库。
您可以找到详细信息here,下载链接位于该页面的顶部。
就Maven而言,您不需要它,但您必须在类路径中手动包含所有必需的库。如果您通过Eclipse运行它,请触发CommandLineJobRunner
。