Tuesday 6 October 2020

Passing parameters in Job and Step level in Spring Batch


1. Job level

Besides passing parameters when building a job, using Job Listener to pass extra job level parameters which can be used through the job running. Specifically, create a class who implements JobExecutionListener, override below method: 

@Override
public void beforeJob(JobExecution jobExecution)

In the method, add more parameters into job context as below:

ExecutionContext jobExecutionContext = jobExecution.getExecutionContext();
jobExecutionContext.put(someKey, someValue);

In Job's steps, using below code to introduce the parameter:

@Value("#{jobExecutionContext[someKey]}")
private SomeCalss someClassInstance; // the value will be someValue

2. Step level

How to pass parameters between Steps within a job?

It is the same as Job level, the Step should implement a Listener StepExecutionListener.

Say if you want to passing a parameter from step1 to step2. Step 1 should implements the above Listener, in afterStep method, add the parameter into execution context:

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
// put a number into job execution context for future use
stepExecution.getJobExecution().getExecutionContext().putLong("aNumber", aNumber);
return null;
}

Step 2 also implements the Listener but in before step method to get the parameter out.

@Override
public void beforeStep(StepExecution stepExecution) {
aNumber = stepExecution.getJobExecution().getExecutionContext().getLong("aNumber");
}

Via this way you can passing parameters from one step to another.








2 comments:

  1. Breakup is certainly painful and no one wants to go through it. But still if you had a breakup due to any reason, you can still get your ex back, I just got my husband back through the help of genuine love spell caster called dr unity. My name is Emily Sarah am from Tx,USA. My husband left me for another woman, This was just 3 years of our marriage. The most painful thing is that I was pregnant with our second baby. I wanted him back. I did everything within my reach to bring him back but all was in vain, I wanted him back so badly because of the love I had for him, I begged him with everything, I made promises but he refused. I explained my problem to my friend and she suggested that I should rather contact a spell caster that could help me cast a spell to bring him back , I had no choice than to try it. I messaged the spell caster called dr unity, and he assured me there was no problem and that everything will be okay before 11 hours. He cast the spell and surprisingly 11 hours later my husband called me. I was so surprised, I answered the call and all he said was that he was so sorry for everything that had happened He wanted me to return to him. He also said he loved me so much. I was so happy and went to him that was how we started living together happily again.thanks to dr unity . if you are here and your Lover is turning you down, or your husband moved to another woman, do not cry anymore, contact Dr.Unity for help now..Here his contact..WhatsApp him: +2348055361568 ,
    Email him at: Unityspelltemple@gmail.com
    ,Watch this YouTube channel to know how grateful I am https://youtube.com/shorts/rBPtBktMmtA?feature=share

    ReplyDelete
  2. All thanks to Mr Anderson Carl for helping with my profits and making my fifth withdrawal possible. I'm here to share an amazing life changing opportunity with you. its called Bitcoin / Forex trading options. it is a highly lucrative business which can earn you as much as $2,570 in a week from an initial investment of just $200. I am living proof of this great business opportunity. If anyone is interested in trading on bitcoin or any cryptocurrency and want a successful trade without losing notify Mr Anderson Carl now on Whatsapp: +1(252)285-2093 Email: andersoncarlassettrade@gmail.com

    ReplyDelete