1

I made this spring batch project (csv to database, classic) - works fine: https://github.com/Tyvain/Spring-Batch-Generic-Bulk

On a separate project, I made an app with vaadin where I can upload a file works fine: https://github.com/Tyvain/vaadin-simple-upload-file

-> We need to trigger the job when a file is uploaded.

So I regrouped the 2 projects into one app, with these steps:

  1. disable batch: job.enabled: false (in application.yml)
  2. add required vaadin librairies to the pom
  3. add the view (MainView.java).

Sources of this modifications: https://github.com/Tyvain/Spring-Batch-Generic-Bulk/tree/include-vaadin-upload-page

At this point, I am still not sure how to launch the job, but I face another problem: when I launch the app, it stops. The server, that stay up in the vaadin app, does not stay up here.

Edit: when I remove those annotation from my job configuration, the server stays up:

//@Configuration
//@EnableBatchProcessing

1/ Is it possible to keep my server running with spring-batch enable?

2/ is this a wrong practice ?

Solution working: https://github.com/Tyvain/ProcessUploadedFile-Vaadin_SpringBatch

Tyvain
  • 2,640
  • 6
  • 36
  • 70
  • A standalone Spring Batch job would normally run, then stop. The vaadin server side support starts, then keeps running until you shut it down. So - it might be better to merge your Spring Batch project into your Vaadin project (which will already stay up) - then run an instance of your Spring Batch job within your Vaadin server, after each file upload occurs... – moilejter Aug 21 '18 at 01:37
  • Ok, but I don't think it would work. I would like to understand what is the difference between the 2 projects that make spring-batch project stop. I compared both configuration, and I dont understand. They are pretty much the same. – Tyvain Aug 21 '18 at 01:50
  • I am guessing a Vaadin project runs within a web server, so the browser can connect to it and download the vaading application. That web server might be started transparently - but it will be there, and will stay up. A Spring Batch app, on the other hand, will "just" run its job when it's started, then stop - since the job is done. – moilejter Aug 21 '18 at 01:53
  • yes. I find that when I remove spring-batch annotations, the server stays up. But then I might loose the advantages of spring batch I think (entries in job executions tables etc..) – Tyvain Aug 21 '18 at 02:01
  • It think it behaves as a 'spring-batch' project, as soon as a bean in the spring context has an import from from org.springframework.batch.* – Tyvain Aug 21 '18 at 02:14

2 Answers2

4

When you run a Spring Batch job from within a web server, you need to setup a JobLauncher that runs your job asynchronously in the background. This is explained in details here: https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html#runningJobsFromWebContainer

You would need to use an asynchronous TaskExecutor implementation (such as ThreadPoolTaskExecutor). See example here: https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html#configuringJobLauncher

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
-1

If you want to process uploaded file right after it has been uploaded, the spring-batch is not the right approach. I would recommend to process it in background thread after upload using @Async and CompletableFuture, see more info here: Spring @Async with CompletableFuture

Tatu Lund
  • 9,949
  • 1
  • 12
  • 26
  • but then I loose all the feature that spring-batch brings (steps, job execution table etc..). – Tyvain Aug 22 '18 at 02:11