0

I'm new to use Spring-batch and I'm trying to get an handle of the SystemCommandTasklet function. I'm using STS on Windows 8. What I'm trying to do is launch a simple Java program "HelloWorld" using SystemCommandTasklet and redirect the output String in a txt file.

My HelloWorld program is situated in C:\Users\nicholas.pizzini\ and when I launch

java -cp C:\Users\nicholas.pizzini\ HelloWorld

I got no issues.

But when I try to launch it using SystemCommandTasklet it produces no result, although it tells me that the job has completed.

This is my application context.

<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>

and the output on the console is:

 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

2 Answers2

0

The SystemCommeandTasklet doesn't pipe the output of the spawned process to your logs so you won't see it there. If you want to create a simple test script to verify that it's working, create a script that writes a file somewhere.

Michael Minella
  • 20,843
  • 4
  • 55
  • 67
0

You could write your own SystemCommandTasklet. Take the existing code as a basis and then simply reroute the outputStream and the errorStream of the Process.

There are a couple of answer to questions like "how to redirect the outputstream", e.g.

Community
  • 1
  • 1
Hansjoerg Wingeier
  • 4,274
  • 4
  • 17
  • 25