Skip to content

Commit

Permalink
Merge pull request #4 from zahidMed/master
Browse files Browse the repository at this point in the history
release 1.0.0
  • Loading branch information
zahidMed authored Apr 10, 2021
2 parents 314f04f + 1ac1310 commit 646cfee
Show file tree
Hide file tree
Showing 25 changed files with 291 additions and 274 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This module is used to propagate the spring security context throw the batch Ite
<dependency>
<groupId>com.digibooster.spring.batch</groupId>
<artifactId>spring-batch-security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
```

Expand All @@ -31,7 +31,7 @@ This module is used to propagate values stored in Slf4j's MDC context throw the
<dependency>
<groupId>com.digibooster.spring.batch</groupId>
<artifactId>spring-batch-mdc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
```

Expand All @@ -42,7 +42,7 @@ This module is used to propagate sleuth Span informations throw the batch Items
<dependency>
<groupId>com.digibooster.spring.batch</groupId>
<artifactId>spring-batch-sleuth</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
```

Expand All @@ -53,6 +53,6 @@ This module is used to propagate the locale context throw the batch Items
<dependency>
<groupId>com.digibooster.spring.batch</groupId>
<artifactId>spring-batch-locale</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
```
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.digibooster.spring.batch</groupId>
<artifactId>spring-batch-context</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0</version>
<description>Framework that allows the access to the spring batch context in order
to propagate other frameworks contexts like spring security,sleuth ....
</description>
Expand All @@ -14,13 +14,12 @@

<spring-batch.version>4.0.0.RELEASE</spring-batch.version>
<spring-framework.version>5.0.0.RELEASE</spring-framework.version>
<spring-cloud-sleuth.version>1.3.0.RELEASE</spring-cloud-sleuth.version>
<slf4j.version>1.6.1</slf4j.version>
<aspectjweaver.version>1.8.9</aspectjweaver.version>
<junit.version>4.12</junit.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<java.version>1.8</java.version>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<java.version>1.7</java.version>
</properties>

<modules>
Expand Down
2 changes: 1 addition & 1 deletion spring-batch-context-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.digibooster.spring.batch</groupId>
<artifactId>spring-batch-context</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0</version>
</parent>

<artifactId>spring-batch-context-commons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,36 @@
import org.aspectj.lang.annotation.Aspect;

/**
* Aspect that intercepts the call of job run method in order to allow the {@link JobExecutionContextListener} to insert
* the parameters they need for restoring their contexts.
* Aspect that intercepts the call of job run method in order to allow the
* {@link JobExecutionContextListener} to insert the parameters they need for
* restoring their contexts.
*
* @author Mohammed ZAHID <[email protected]>
*
*/
@Aspect
@Service
public class JobExecutionAspect {



protected List<JobExecutionContextListener> jobExecutionContextListeners;
public JobExecutionAspect(List<JobExecutionContextListener> jobExecutionContextListeners){
this.jobExecutionContextListeners= jobExecutionContextListeners;

public JobExecutionAspect(List<JobExecutionContextListener> jobExecutionContextListeners) {
this.jobExecutionContextListeners = jobExecutionContextListeners;
}



@Around("execution(* org.springframework.batch.core.launch.JobLauncher+.run(..))")
public JobExecution beforeRun(ProceedingJoinPoint joinPoint) throws Throwable
{
JobParameters jobParameters=(JobParameters) joinPoint.getArgs()[1];
JobParametersBuilder jobParametersBuilder= new JobParametersBuilder(jobParameters);
if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){
Iterator<JobExecutionContextListener> iter= jobExecutionContextListeners.iterator();
while(iter.hasNext()){
public JobExecution beforeRun(ProceedingJoinPoint joinPoint) throws Throwable {
JobParameters jobParameters = (JobParameters) joinPoint.getArgs()[1];
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(jobParameters);
if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) {
Iterator<JobExecutionContextListener> iter = jobExecutionContextListeners.iterator();
while (iter.hasNext()) {
iter.next().insertContextInfo(jobParametersBuilder);
}
}
return (JobExecution)joinPoint.proceed(new Object[] {joinPoint.getArgs()[0],jobParametersBuilder.toJobParameters()});

}
return (JobExecution) joinPoint
.proceed(new Object[] { joinPoint.getArgs()[0], jobParametersBuilder.toJobParameters() });


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,42 @@
import com.digibooster.spring.batch.listener.StepExecutionListenerContextSupport;

@Configuration
@ComponentScan(basePackages={"com.digibooster.spring.batch.aop"})
@ComponentScan(basePackages = { "com.digibooster.spring.batch.aop" })
@EnableAspectJAutoProxy
public class SpringBatchContextConfiguration {



@Bean
public JobExecutionListenerContextSupport jobExecutionListenerContextSupport(@Autowired List<JobExecutionContextListener> jobExecutionContextListeners)
{
public JobExecutionListenerContextSupport jobExecutionListenerContextSupport(
@Autowired List<JobExecutionContextListener> jobExecutionContextListeners) {
return new JobExecutionListenerContextSupport(jobExecutionContextListeners);
}

@Bean
public StepExecutionListenerContextSupport stepExecutionListenerContextSupport(@Autowired List<JobExecutionContextListener> jobExecutionContextListeners)
{
public StepExecutionListenerContextSupport stepExecutionListenerContextSupport(
@Autowired List<JobExecutionContextListener> jobExecutionContextListeners) {
return new StepExecutionListenerContextSupport(jobExecutionContextListeners);
}



/**
* Bean pre-processing that registers job and step listener for the created job in order to recover the context information
* injected as job parameters by the {@link JobExecutionAspect}
* Bean pre-processing that registers job and step listener for the created job
* in order to recover the context information injected as job parameters by the
* {@link JobExecutionAspect}
*
* @param jobExecutionListener
* @param stepExecutionListenerContextSupport
* @return
*/
@Bean
public BeanPostProcessor jobPostProcessor(@Autowired final JobExecutionListenerContextSupport jobExecutionListener,
@Autowired final StepExecutionListenerContextSupport stepExecutionListenerContextSupport){
return new BeanPostProcessor(){
@Autowired final StepExecutionListenerContextSupport stepExecutionListenerContextSupport) {
return new BeanPostProcessor() {

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof AbstractJob){
AbstractJob job=(AbstractJob) bean;
if (bean instanceof AbstractJob) {
AbstractJob job = (AbstractJob) bean;
job.registerJobExecutionListener(jobExecutionListener);
}
else if(bean instanceof AbstractStep) {
AbstractStep step= (AbstractStep) bean;
} else if (bean instanceof AbstractStep) {
AbstractStep step = (AbstractStep) bean;
step.registerStepExecutionListener(stepExecutionListenerContextSupport);
}
return bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,53 @@
import com.digibooster.spring.batch.aop.JobExecutionAspect;

/**
* Allow the restoring the context of the thread that runs the job inside the job it self.
* Allow the restoring the context of the thread that runs the job inside the
* job it self.
*
* @author Mohammed ZAHID <[email protected]>
*
*/
public interface JobExecutionContextListener {

/**
* Serializes the current context information and puts it in the the job parameter.
* This method called by the Aspect {@link JobExecutionAspect}
* Serializes the current context information and puts it in the the job
* parameter. This method called by the Aspect {@link JobExecutionAspect}
*
* @param jobParametersBuilder
*/
void insertContextInfo(JobParametersBuilder jobParametersBuilder);

/**
* Deserializes the context information from the job parameters and inserts it in the Job execution context.
* This method is called by the Job listener {@link JobExecutionListenerContextSupport}
* Deserializes the context information from the job parameters and inserts it
* in the Job execution context. This method is called by the Job listener
* {@link JobExecutionListenerContextSupport}
*
* @param jobExecution
*/
void fillJobExecutionContext(JobExecution jobExecution);

/**
* Removes the context information from job execution context when the job ends
* This method is called by the Job listener {@link JobExecutionListenerContextSupport}
* This method is called by the Job listener
* {@link JobExecutionListenerContextSupport}
*
* @param jobExecution
*/
void removeFromJobExecutionContext(JobExecution jobExecution);


/**
* Restore the context information from the job execution context before each step
* This method is called by the Step listener {@link StepExecutionListenerContextSupport}
* Restore the context information from the job execution context before each
* step This method is called by the Step listener
* {@link StepExecutionListenerContextSupport}
*
* @param stepExecution
*/
void restoreContext(StepExecution stepExecution);


/**
* Remove the context information when the step ends
* This method is called by the Step listener {@link StepExecutionListenerContextSupport}
* Remove the context information when the step ends This method is called by
* the Step listener {@link StepExecutionListenerContextSupport}
*
* @param stepExecution
*/
void clearContext(StepExecution stepExecution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,40 @@
import org.springframework.util.CollectionUtils;

/**
* Listener that call the registered {@link JobExecutionContextListener} before and after running the batch jobs.
* Listener that call the registered {@link JobExecutionContextListener} before
* and after running the batch jobs.
*
* @author Mohammed ZAHID <[email protected]>
*
*/
public class JobExecutionListenerContextSupport implements JobExecutionListener {

protected List<JobExecutionContextListener> jobExecutionContextListeners;
public JobExecutionListenerContextSupport(List<JobExecutionContextListener> jobExecutionContextListeners){
this.jobExecutionContextListeners= jobExecutionContextListeners;

public JobExecutionListenerContextSupport(List<JobExecutionContextListener> jobExecutionContextListeners) {
this.jobExecutionContextListeners = jobExecutionContextListeners;
}

@Override
public void beforeJob(JobExecution jobExecution) {
if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){
Iterator<JobExecutionContextListener> iter= jobExecutionContextListeners.iterator();
while(iter.hasNext()){
if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) {
Iterator<JobExecutionContextListener> iter = jobExecutionContextListeners.iterator();
while (iter.hasNext()) {
iter.next().fillJobExecutionContext(jobExecution);
}
}

}

@Override
public void afterJob(JobExecution jobExecution) {
if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){
Iterator<JobExecutionContextListener> iter= jobExecutionContextListeners.iterator();
while(iter.hasNext()){
if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) {
Iterator<JobExecutionContextListener> iter = jobExecutionContextListeners.iterator();
while (iter.hasNext()) {
iter.next().removeFromJobExecutionContext(jobExecution);
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@
import org.springframework.util.CollectionUtils;

/**
* Listener that call the registered {@link JobExecutionContextListener} before and after running the batch steps.
* Listener that call the registered {@link JobExecutionContextListener} before
* and after running the batch steps.
*
* @author Mohammed ZAHID <[email protected]>
*
*/
public class StepExecutionListenerContextSupport implements StepExecutionListener{
public class StepExecutionListenerContextSupport implements StepExecutionListener {


protected List<JobExecutionContextListener> jobExecutionContextListeners;

public StepExecutionListenerContextSupport(List<JobExecutionContextListener> jobExecutionContextListeners){
this.jobExecutionContextListeners= jobExecutionContextListeners;
protected List<JobExecutionContextListener> jobExecutionContextListeners;

public StepExecutionListenerContextSupport(List<JobExecutionContextListener> jobExecutionContextListeners) {
this.jobExecutionContextListeners = jobExecutionContextListeners;
}



@Override
public void beforeStep(StepExecution stepExecution) {
if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){
Iterator<JobExecutionContextListener> iter= jobExecutionContextListeners.iterator();
while(iter.hasNext()){
if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) {
Iterator<JobExecutionContextListener> iter = jobExecutionContextListeners.iterator();
while (iter.hasNext()) {
iter.next().restoreContext(stepExecution);
}
}

}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){
Iterator<JobExecutionContextListener> iter= jobExecutionContextListeners.iterator();
while(iter.hasNext()){
if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) {
Iterator<JobExecutionContextListener> iter = jobExecutionContextListeners.iterator();
while (iter.hasNext()) {
iter.next().clearContext(stepExecution);
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit 646cfee

Please sign in to comment.