1

I am trying to process a series of files using Spring Integration in a batch fashion. I have this very old xml which tries to convert the messages into jobs

    <int:transformer ref="messageToJobTransformer"/>
    <batch-int:job-launching-gateway job-launcher="jobLauncher"/>

The messageToJobTransformer is a class which can convert a Message into a Job. The problem is I don't know where this file is now neither I want a xml config. I want it to be pure Java DSL. Here is my simple config.

return IntegrationFlows.from(Files.inboundAdapter(directory)
                    .preventDuplicates()
                    .patternFilter("*.txt")))
                    .handle(jobLaunchingGw())                       
                    .get();

And here is my bean for the gateway.

@Autowired
private JobLauncher jobLauncher;

@Bean
public MessageHandler jobLaunchingGw() {
    return new JobLaunchingGateway(jobLauncher);   
}

EDIT:Updating the Batch Config class.

@Configuration
    @EnableBatchProcessing
public class BatchConfig

{

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;




@Bean
        public ItemReader<String> reader(@Value({jobParameters['input.file.name']}") String filename) throws MalformedURLException
    {
        FlatFileItemReader<String> reader = new FlatFileItemReader<String>();       
        return reader;
    }
@Bean
public Job job() throws MalformedURLException
{
    return jobs.get("job").start(step()).build();
}

@Bean
public Step step() throws MalformedURLException
{
    return steps.get("step").<String, String> chunk(5).reader(reader())
    .writer(writer()).build();
}

@Bean
public ItemWriter<String> writer(@Value("#{jobParameters['input.file.name']}")
{
    FlatFileItemWriter writer = new FlatFileItemWriter();
    return writer;
}



}
Harish
  • 3,343
  • 15
  • 54
  • 75

1 Answers1

2

Your question isn't clear. The JobLaunchingGateway expects JobLaunchRequest as a payload.

Since your Integration Flow begins from the Files.inboundAdapter(directory), I can assume that you that you have there some Job definitions. So, what you need here is some class which can parse the file and return JobLaunchRequest.

Something like this from the Spring Batch Reference Manual:

public class FileMessageToJobRequest {
    private Job job;
    private String fileParameterName;

    public void setFileParameterName(String fileParameterName) {
        this.fileParameterName = fileParameterName;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    @Transformer
    public JobLaunchRequest toRequest(Message<File> message) {
        JobParametersBuilder jobParametersBuilder =
            new JobParametersBuilder();

        jobParametersBuilder.addString(fileParameterName,
            message.getPayload().getAbsolutePath());

        return new JobLaunchRequest(job, jobParametersBuilder.toJobParameters());
    }
}

After the definition that class as a @Bean you can use it from the .transform() EIP-method just before your .handle(jobLaunchingGw()).

UPDATE

@Bean
public FileMessageToJobRequest fileMessageToJobRequest(Job job) {
     FileMessageToJobRequest fileMessageToJobRequest = new FileMessageToJobRequest();
     fileMessageToJobRequest.setJob(job);
     fileMessageToJobRequest.setfileParameterName("file");
     return fileMessageToJobRequest;
}
...

@Bean
public IntegrationFlow flowToBatch(FileMessageToJobRequest fileMessageToJobRequest) {

      return IntegrationFlows
            .from(Files.inboundAdapter(directory)
                    .preventDuplicates()
                    .patternFilter("*.txt")))
            .transform(fileMessageToJobRequest)
            .handle(jobLaunchingGw())                       
                    .get();

}
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • oh that did not help.Basically I am polling a directory and processing the files in batch.The problem with the above code is I am not able to see the job because the job would be created in the BatchConfig class.If it happens before I am getting an exception unable to read exception, if it happens after, Job is not visible.I think I am at a point that there is noway I can do this with Spring Integration Java DSL.Either I have to choose batch and XML for Spring DSL or completely drop Spring Integration – Harish Mar 16 '16 at 20:40
  • ??? Not sure what is `BatchConfig page`, but you always can inject one bean into another - `@Autowired`. Share more config on the matter, e.g. for that `messageToJobTransformer`. – Artem Bilan Mar 16 '16 at 20:46
  • My bad should have written it as BatchConfig class.Updated the same – Harish Mar 16 '16 at 20:55
  • OK. What is the problem to inject that your `job()` bean in to the `FileMessageToJobRequest` component ? – Artem Bilan Mar 16 '16 at 20:57
  • Where should the transformer method go? Should it be in my config class? Initially I kept it in the seperate class as in FileMessageToJobRequest but since job was not visible I moved the transformer method to my config class.For some reason I am getting exceptions. – Harish Mar 16 '16 at 21:10
  • 1
    See an UPDATE in my answer. If everything is going to be a single application context, you can inject and autowire everything what you need. – Artem Bilan Mar 16 '16 at 21:15
  • Everything looks good except now I need to access the file name in the batch for which I am accessing from the job parameter EL1008E:(pos 0): Property or field 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' – Harish Mar 16 '16 at 23:02