0

When somebody calls my API, I want to pass some data from the API to a job.

inside my controller:

val parameters = JobParametersBuilder()
parameters.addString("somename", "somevalue")

The following methods are available:

methods

How can I pass an object to the job? Inside the job I want to perform some MongoDB calls and more

Do I need to serialize the data and pass them as a string? Or is there a better way to pass an object to the job from my controller

yooouuri
  • 2,578
  • 10
  • 32
  • 54
  • What about `addJobParameters`? It look like you to have pass each property in your object as string/long/double/date. – sagarr Oct 18 '18 at 17:59
  • Possible duplicate of [how to send a custom object as Job Parameter in Spring Batch?](https://stackoverflow.com/questions/33761730/how-to-send-a-custom-object-as-job-parameter-in-spring-batch) – Mahmoud Ben Hassine Oct 19 '18 at 08:48

1 Answers1

0

No, it's not possible.

I think it's good and simple to have primitive parameters. Because then you can mediate risk of having parameter object versioning issues, which leads then to deserialization issues. Imagine you have your object as a parameter and then you update it in next version. Then your current application will be no longer able to restart old failing job.

Also please note third boolean flag for parameters 'identifying'. From documentation: The identifying flag is used to indicate if the parameter is to be used as part of the identification of a job instance.

@Controller
public class JobLauncherController {

  @Autowired
  JobLauncher jobLauncher;

  @Autowired
  Job job;

  @RequestMapping("/jobLauncher.html")
  public void handle() throws Exception{
    JobParameters jobParameters = new JobParametersBuilder()
              .addString("MyParameter", "value", true)
              .toJobParameters();
    jobLauncher.run(job, jobParameters);
  }
}

More you can find in documentation: https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html

Igor Konoplyanko
  • 9,176
  • 6
  • 57
  • 100
  • For example, a job is started for inserting (Google Adwords) campaigns. When I have multiple campaigns and I want to insert them, how can I pass the data to my job? – yooouuri Oct 19 '18 at 07:48
  • Usual approach is to pass some kind of `ID` to the job via parameters, then your job should fetch all required data prior to this identifier – Igor Konoplyanko Oct 19 '18 at 07:50
  • But than using a job would not make any sense... Inside the job I want to save something in the MongoDB and after that I want to do something with the Google AdWords API. – yooouuri Oct 19 '18 at 07:53
  • The whole purpose is to pass some data (like an array or list with multiple possible campaigns) to the job and let the job do the rest – yooouuri Oct 19 '18 at 07:54
  • Then serialize manually your data into the string, but be aware that parameter field is restricted by size of your column in related DB – Igor Konoplyanko Oct 19 '18 at 07:56