我是新手使用Spring批处理,我正试图掌握它 SystemCommandTasklet函数。 我在Windows 8上使用STS。 我要做的是使用SystemCommandTasklet启动一个简单的Java程序“HelloWorld”,并将输出String重定向到txt文件中。
我的HelloWorld程序位于C:\ Users \ nicholas.pizzini \,当我启动时
java -cp C:\Users\nicholas.pizzini\ HelloWorld
我没有遇到任何问题。
但是当我尝试使用SystemCommandTasklet启动它时,它没有产生结果,虽然它告诉我作业已经完成。
这是我的申请背景。
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<!-- JOB AND STEPS BEAN -->
<batch:job id="helloWorldJob">
<batch:step id="executeSystemCommandStep" >
<batch:tasklet ref="executeSystemCommand" />
</batch:step>
</batch:job>
<bean id="executeSystemCommand"
class="org.springframework.batch.core.step.tasklet.SystemCommandTasklet">
<property name="command" value="java -cp C:\Users\nicholas.pizzini HelloWorld > C:\hello\e.txt" />
<!-- 3 second timeout for the command to complete -->
<property name="timeout" value="100000" />
<property name="environmentParams"
value="JAVA_HOME=C:\Program Files\Java\jdk1.8.0_45,PATH=C:\Users\nicholas.pizzini\AppData\Roaming\npm;C:\Program Files\Java\jdk1.8.0_45\bin\"/>
</bean>
并且控制台上的输出是:
nov 04, 2015 4:33:09 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFORMAZIONI: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@439f5b3d: startup date [Wed Nov 04 16:33:09 CET 2015]; root of context hierarchy
nov 04, 2015 4:33:10 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFORMAZIONI: Loading XML bean definitions from class path resource [spring/batch/jobs/job-read-files.xml]
nov 04, 2015 4:33:10 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFORMAZIONI: Overriding bean definition for bean 'helloWorldJob': replacing [Generic bean: class [org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.batch.core.configuration.xml.JobParserJobFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
nov 04, 2015 4:33:10 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFORMAZIONI: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1753acfe: defining beans [jobRepository,transactionManager,jobLauncher,org.springframework.batch.core.scope.internalStepScope,org.springframework.beans.factory.config.CustomEditorConfigurer,org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor,executeSystemCommandStep,helloWorldJob,executeSystemCommand]; root of factory hierarchy
nov 04, 2015 4:33:10 PM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFORMAZIONI: No TaskExecutor has been set, defaulting to synchronous executor.
nov 04, 2015 4:33:10 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFORMAZIONI: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}]
nov 04, 2015 4:33:10 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFORMAZIONI: Executing step: [executeSystemCommandStep]
nov 04, 2015 4:33:11 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFORMAZIONI: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
Exit Status : COMPLETED
Exit Status : []
Done
答案 0 :(得分:0)
SystemCommeandTasklet
不会将生成的进程的输出传递给您的日志,因此您不会在那里看到它。如果要创建一个简单的测试脚本来验证它是否正常工作,请创建一个在某处写入文件的脚本。
答案 1 :(得分:0)
您可以编写自己的SystemCommandTasklet。以现有代码为基础,然后简单地重新路由outputStream和Process的errorStream。
对于诸如&#34;如何重定向输出流&#34;等问题有几个答案,例如