我有来自外部配置web-service的jdbc属性文件在spring boot中为了设置mysql道具,将它们添加到application.properties很容易:
spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)
我怎么能在我的应用程序中重写那些程序?
Spring-batch道具同样如此:
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost/mydv
database.username=root
database.password=root
Run Code Online (Sandbox Code Playgroud) 我试图安全地将params从tasklet传递到同一个工作中的一个步骤.
我的工作包括3个tasklet(第1步,第2步,第3步),最后是step4(处理器,读者,作者)
这项工作正在多次并行执行.
在tasklet里面的第一步我通过web服务评估param(hashId),而不是把它传遍整个链,直到我的读者(在第4步)
在第3步中,我创建了一个名为:filePath的新参数,该文件基于hashid,我将其作为文件资源位置发送到step4(读取器)
我正在使用stepExecution传递此参数(hashId和filePath).
我通过tasklet尝试了3种方法:
传递param(hash1d从step1进入step2,从step2进入步骤3)我这样做:
chunkContext.getStepContext()
.getStepExecution()
.getExecutionContext()
.put("hashId", hashId);
Run Code Online (Sandbox Code Playgroud)
在步骤4中,我基于hashId填充filePath并将其传递给我的最后一步(读者处理器和编写器)
public class DownloadFileTasklet implements Tasklet, StepExecutionListener {
..
@Override
public RepeatStatus execute(ChunkContext chunkContext, ExecutionContext
executionContext) throws IOException {
String hashId = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("hashId");
...
filepath="...hashId.csv";
//I used here executionContextPromotionListener in order to promote those keys
chunkContext.getStepContext()
.getStepExecution()
.getExecutionContext()
.put("filePath", filePath);
}
logger.info("filePath + "for hashId=" + hashId);
}
@Override
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
Run Code Online (Sandbox Code Playgroud)
请注意我在完成该步骤之前打印hashId和filePath值(步骤3).通过日志,它们是按照预期一致和填充的
我还在我的读者中添加了日志,以查看我得到的参数.
@Bean
@StepScope
public ItemStreamReader<MyDTO> reader(@Value("#{jobExecutionContext[filePath]}") String filePath) …Run Code Online (Sandbox Code Playgroud) 我有一个使用Spring Batch和Spring MVC的应用程序.我能够将Spring Batch Admin作为一个单独的战争部署,并将其用于我的应用程序使用的同一个DB,尽管我想将它集成到我自己的应用程序中,也可能修改一些视图.
有没有一种简单的方法可以做到这一点,还是我必须分叉并从那里开始?
根据文档, spring批处理管理员很容易嵌入到现有的应用程序中.只需复制web.xml和index.jsp,然后添加所需的依赖项就足以让它工作.
但是,如果我想在现有的春季启动项目中使用它,它会变得更糟.根据这个例子,配置有点hacky但它的工作原理.我尝试@EnableBatchProcessing在配置bean中使用注释.然后我得到以下异常.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.batch.core.configuration.annotation.JobBuilderFactory org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.jobBuilders() throws java.lang.Exception] threw exception; nested exception is java.lang.ClassCastException: org.springframework.batch.core.repository.support.JobRepositoryFactoryBean$$EnhancerBySpringCGLIB$$49fa0273 cannot be cast to org.springframework.batch.core.repository.JobRepository
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) …Run Code Online (Sandbox Code Playgroud) 我只想知道春季批次中是否有"JOB"范围,如"STEP"范围?如果没有,我们应该开发自定义范围,还是有更好的选择?
提前致谢.
我想创建一个具有以下格式的平面文件:
Col1Name;Col2Name;Col3Name
one;23;20120912
two;28;20120712
Run Code Online (Sandbox Code Playgroud)
如图所示,平面文件中的第一行是列名.
如何通过头回调实现这一目标?
我看到如果输入文件的格式是以上格式,则有一个选项可以忽略第一行:
<property name="firstLineIsHeader" value="true"/>
Run Code Online (Sandbox Code Playgroud)
此外,这个Jira问题表明我想要的是实施和关闭.但是,我无法找到将第一行写为列名的任何示例.
<beans:bean id="MyFileItemWriter" class="com.nik.MyFileItemWriter" scope="step">
<beans:property name="delegate">
<beans:bean class="org.springframework.batch.item.file.FlatFileItemWriter">
<beans:property name="resource" value="file:MYFILE.dat" />
<beans:property name="lineAggregator">
<beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<beans:property name="delimiter" value=";" />
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<beans:property name="names" value="Col1Name, Col2Name, Col3Name" />
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
<beans:property name="headerCallback" ref="MyFileItemWriter" />
</beans:bean>
</beans:property>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)
我的项目编写者如下所示:
public class MyFileItemWriter implements ItemWriter<MyBean>, FlatFileHeaderCallback, ItemStream{
private FlatFileItemWriter<MyBean> delegate;
public void setDelegate(final FlatFileItemWriter<MyBean> delegate) {
this.delegate = delegate;
}
public void writeHeader(Writer writer) throws …Run Code Online (Sandbox Code Playgroud) 我试图在spring batch admin中使用mysql数据库而不是默认的HSQL.对于那个文件
http://docs.spring.io/spring-batch-admin/reference/reference.xhtml 和 使用jndi数据源与spring batch admin
我复制env-context.xml到src/main/resources/META-INF/batch/override/manager/env-context.xml 和改变其配置价值
<value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>
Run Code Online (Sandbox Code Playgroud)
至
<value>classpath:batch-mysql.properties</value>
Run Code Online (Sandbox Code Playgroud)
以下是我的完整配置.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<!-- Use this to set additional properties on beans at run time -->
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
<value>classpath:batch-default.properties</value>
<value>classpath:batch-mysql.properties</value>
</list>
</property>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="order" value="1" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我还尝试将data-source-context.xml复制到同一文件夹并将其配置更改为mysql
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean …Run Code Online (Sandbox Code Playgroud) 我正在使用FlatFileItemReader处理CSV文件.
有时我在输入文件中得到空行.
当那件事发生时,整个步骤就停止了 我想跳过那些线并继续正常.
我试图在步骤中添加异常处理程序,以便捕获execption而不是整个步骤变为stooped:
@Bean
public Step processSnidUploadedFileStep() {
return stepBuilderFactory.get("processSnidFileStep")
.<MyDTO, MyDTO>chunk(numOfProcessingChunksPerFile)
.reader(snidFileReader(OVERRIDDEN_BY_EXPRESSION))
.processor(manualUploadAsyncItemProcessor())
.writer(manualUploadAsyncItemWriter())
.listener(logProcessListener)
.throttleLimit(20)
.taskExecutor(infrastructureConfigurationConfig.taskJobExecutor())
.exceptionHandler((context, throwable) -> logger.error("Skipping record on file. cause="+ ((FlatFileParseException)throwable).getCause()))
.build();
}
Run Code Online (Sandbox Code Playgroud)
由于当空行到达时我正在使用块进行处理并且捕获到异常,因此跳过整个块(块可能包含CSV文件上的有效行,并且它们也被跳过)
知道如何在处理文件时正确地执行此操作吗?
谢谢,雷.
编辑我的代码后.仍然没有跳过:
public Step processSnidUploadedFileStep() {
SimpleStepBuilder<MyDTO, MyDTO> builder = new SimpleStepBuilder<MyDTO, MyDTO>(stepBuilderFactory.get("processSnidFileStep"));
return builder
.<PushItemDTO, PushItemDTO>chunk(numOfProcessingChunksPerFile)
.faultTolerant().skip(FlatFileParseException.class)
.reader(snidFileReader(OVERRIDDEN_BY_EXPRESSION))
.processor(manualUploadAsyncItemProcessor())
.writer(manualUploadAsyncItemWriter())
.listener(logProcessListener)
.throttleLimit(20)
.taskExecutor(infrastructureConfigurationConfig.taskJobExecutor())
.build();
}
Run Code Online (Sandbox Code Playgroud) 使用Spring Batch Admin时,它会尝试为dataSource,transactionManager等提供一些默认值.
如果要覆盖这些默认值,可以在META-INF/spring/batch/servlet/override /文件夹下创建自己的xml bean定义,并在引导期间保证将覆盖默认属性.
在spring-batch-admin中,dataSource缺省值在data-source-context.xml中使用此定义定义
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${batch.jdbc.driver}" />
<property name="url" value="${batch.jdbc.url}" />
<property name="username" value="${batch.jdbc.user}" />
<property name="password" value="${batch.jdbc.password}" />
<property name="testWhileIdle" value="${batch.jdbc.testWhileIdle}"/>
<property name="validationQuery" value="${batch.jdbc.validationQuery}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
现在,我想用JNDI数据源覆盖这个dataSource,所以我删除了属性行batch.jdbc.driver,batch.jdbc.url并具有以下jndi定义
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dbconn" />
</bean>
Run Code Online (Sandbox Code Playgroud)
您可能很容易猜到系统首先尝试初始化data-source-context.xml中定义的dataSource bean,因为它找不到属性值batch.jdbc.*的任何值,它会失败并出现异常.
无法在字符串值[$ {batch.jdbc.driver}]中解析占位符'batch.jdbc.driver'
由于我将使用JNDI并且不想处理这些属性值,所以我无法继续.
有关如何在这种情况下覆盖dataSource的想法?
这是我从他们的入门页面尝试过的...
SpringSource社区下载指向未提及Spring Batch Admin的页面的链接.看起来原始链接已死或现在重定向到其他内容.
我获得了S3Browse App链接的连接超时
源构建过程失败,每次修复都会导致新的失败.从缺少的工件/存储库开始,现在看起来与Maven 3和Maven 2有关?
maven构建过程似乎拉下了一个罐而不是战争
Google搜索下载链接,请不断回复此页面.我错过了什么?必须有一些公共可用的下载页面,其中包含prebuilts .war.