Created
July 21, 2018 16:29
-
-
Save fmbenhassine/56ff18002301997c2187e793a5e8839d to your computer and use it in GitHub Desktop.
Custom exit code with boot #SpringBatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demoexitcode; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
@SpringBootApplication | |
public class DemoExitCodeApplication { | |
public static void main(String[] args) { | |
System.exit( | |
SpringApplication.exit( | |
SpringApplication.run(DemoExitCodeApplication.class, args) | |
) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example.demoexitcode; | |
import java.util.Arrays; | |
import org.springframework.batch.core.ExitStatus; | |
import org.springframework.batch.core.Job; | |
import org.springframework.batch.core.JobExecution; | |
import org.springframework.batch.core.Step; | |
import org.springframework.batch.core.StepExecution; | |
import org.springframework.batch.core.annotation.BeforeStep; | |
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | |
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | |
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | |
import org.springframework.batch.core.listener.JobExecutionListenerSupport; | |
import org.springframework.batch.item.ItemProcessor; | |
import org.springframework.batch.item.support.ListItemReader; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.ExitCodeGenerator; | |
import org.springframework.boot.autoconfigure.batch.JobExecutionEvent; | |
import org.springframework.context.ApplicationListener; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
@EnableBatchProcessing | |
public class MyJob { | |
@Autowired | |
private JobBuilderFactory jobs; | |
@Autowired | |
private StepBuilderFactory steps; | |
@Bean | |
public Step step() { | |
return steps.get("step") | |
.<Integer, Integer>chunk(2) | |
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4))) | |
.processor(new ItemProcessor<Integer, Integer>() { | |
private StepExecution stepExecution; | |
@BeforeStep | |
public void beforeStep(StepExecution stepExecution) { | |
this.stepExecution = stepExecution; | |
} | |
@Override | |
public Integer process(Integer item) { | |
if (item.equals(3)) { | |
stepExecution.getJobExecution().getExecutionContext().put("failure", "item not accepted"); | |
throw new IllegalArgumentException("3 is not accepted"); | |
} | |
return item; | |
} | |
}) | |
.writer(items -> { | |
for (Integer item : items) { | |
System.out.println("item = " + item); | |
} | |
}) | |
.build(); | |
} | |
@Bean | |
public Job job() { | |
return jobs.get("job") | |
.start(step()) | |
.listener(new JobExecutionListenerSupport() { | |
@Override | |
public void afterJob(JobExecution jobExecution) { | |
String failure = jobExecution.getExecutionContext().getString("failure"); | |
if (failure != null && failure.equalsIgnoreCase("item not accepted")) { | |
jobExecution.setExitStatus(new ExitStatus("Not Accepted")); | |
} | |
} | |
}) | |
.build(); | |
} | |
@Bean | |
public ExitCodeGenerator exitCodeGenerator () { | |
return new MyExitCodeGenerator(); | |
} | |
static class MyExitCodeGenerator implements ExitCodeGenerator, ApplicationListener<JobExecutionEvent> { | |
private JobExecution jobExecution; | |
@Override | |
public int getExitCode() { | |
if (jobExecution.getExitStatus().getExitCode().equalsIgnoreCase("Not Accepted")) { | |
return 42; | |
} | |
return 0; | |
} | |
@Override | |
public void onApplicationEvent(JobExecutionEvent jobExecutionEvent) { | |
this.jobExecution = jobExecutionEvent.getJobExecution(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment