diff --git a/README.md b/README.md index 7cfb1d1..c85234b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This module is used to propagate the spring security context throw the batch Ite com.digibooster.spring.batch spring-batch-security - 0.0.1-SNAPSHOT + 1.0.0 ``` @@ -31,7 +31,7 @@ This module is used to propagate values stored in Slf4j's MDC context throw the com.digibooster.spring.batch spring-batch-mdc - 0.0.1-SNAPSHOT + 1.0.0 ``` @@ -42,7 +42,7 @@ This module is used to propagate sleuth Span informations throw the batch Items com.digibooster.spring.batch spring-batch-sleuth - 0.0.1-SNAPSHOT + 1.0.0 ``` @@ -53,6 +53,6 @@ This module is used to propagate the locale context throw the batch Items com.digibooster.spring.batch spring-batch-locale - 0.0.1-SNAPSHOT + 1.0.0 ``` diff --git a/pom.xml b/pom.xml index 040c3e0..f413035 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.digibooster.spring.batch spring-batch-context pom - 0.0.1-SNAPSHOT + 1.0.0 Framework that allows the access to the spring batch context in order to propagate other frameworks contexts like spring security,sleuth .... @@ -14,13 +14,12 @@ 4.0.0.RELEASE 5.0.0.RELEASE - 1.3.0.RELEASE 1.6.1 1.8.9 4.12 - 1.8 - 1.8 - 1.8 + 1.7 + 1.7 + 1.7 diff --git a/spring-batch-context-commons/pom.xml b/spring-batch-context-commons/pom.xml index 5fd9360..1bc98e6 100644 --- a/spring-batch-context-commons/pom.xml +++ b/spring-batch-context-commons/pom.xml @@ -5,7 +5,7 @@ com.digibooster.spring.batch spring-batch-context - 0.0.1-SNAPSHOT + 1.0.0 spring-batch-context-commons diff --git a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/aop/JobExecutionAspect.java b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/aop/JobExecutionAspect.java index a825356..06a2a98 100644 --- a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/aop/JobExecutionAspect.java +++ b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/aop/JobExecutionAspect.java @@ -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 * */ @Aspect @Service public class JobExecutionAspect { - - + protected List jobExecutionContextListeners; - - public JobExecutionAspect(List jobExecutionContextListeners){ - this.jobExecutionContextListeners= jobExecutionContextListeners; + + public JobExecutionAspect(List 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 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 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() }); - + } } diff --git a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/config/SpringBatchContextConfiguration.java b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/config/SpringBatchContextConfiguration.java index cfb906f..2362a18 100644 --- a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/config/SpringBatchContextConfiguration.java +++ b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/config/SpringBatchContextConfiguration.java @@ -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 jobExecutionContextListeners) - { + public JobExecutionListenerContextSupport jobExecutionListenerContextSupport( + @Autowired List jobExecutionContextListeners) { return new JobExecutionListenerContextSupport(jobExecutionContextListeners); } - + @Bean - public StepExecutionListenerContextSupport stepExecutionListenerContextSupport(@Autowired List jobExecutionContextListeners) - { + public StepExecutionListenerContextSupport stepExecutionListenerContextSupport( + @Autowired List 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; diff --git a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/JobExecutionContextListener.java b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/JobExecutionContextListener.java index 2591b7b..d9d4f27 100644 --- a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/JobExecutionContextListener.java +++ b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/JobExecutionContextListener.java @@ -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 * */ 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); diff --git a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/JobExecutionListenerContextSupport.java b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/JobExecutionListenerContextSupport.java index f5a6b84..c993c59 100644 --- a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/JobExecutionListenerContextSupport.java +++ b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/JobExecutionListenerContextSupport.java @@ -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 * */ public class JobExecutionListenerContextSupport implements JobExecutionListener { protected List jobExecutionContextListeners; - - public JobExecutionListenerContextSupport(List jobExecutionContextListeners){ - this.jobExecutionContextListeners= jobExecutionContextListeners; + + public JobExecutionListenerContextSupport(List jobExecutionContextListeners) { + this.jobExecutionContextListeners = jobExecutionContextListeners; } - + @Override public void beforeJob(JobExecution jobExecution) { - if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){ - Iterator iter= jobExecutionContextListeners.iterator(); - while(iter.hasNext()){ + if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) { + Iterator iter = jobExecutionContextListeners.iterator(); + while (iter.hasNext()) { iter.next().fillJobExecutionContext(jobExecution); } } - + } @Override public void afterJob(JobExecution jobExecution) { - if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){ - Iterator iter= jobExecutionContextListeners.iterator(); - while(iter.hasNext()){ + if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) { + Iterator iter = jobExecutionContextListeners.iterator(); + while (iter.hasNext()) { iter.next().removeFromJobExecutionContext(jobExecution); } } - + } } diff --git a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/StepExecutionListenerContextSupport.java b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/StepExecutionListenerContextSupport.java index 7820f7d..ba19411 100644 --- a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/StepExecutionListenerContextSupport.java +++ b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/listener/StepExecutionListenerContextSupport.java @@ -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 * */ -public class StepExecutionListenerContextSupport implements StepExecutionListener{ +public class StepExecutionListenerContextSupport implements StepExecutionListener { - -protected List jobExecutionContextListeners; - - public StepExecutionListenerContextSupport(List jobExecutionContextListeners){ - this.jobExecutionContextListeners= jobExecutionContextListeners; + protected List jobExecutionContextListeners; + + public StepExecutionListenerContextSupport(List jobExecutionContextListeners) { + this.jobExecutionContextListeners = jobExecutionContextListeners; } - - + @Override public void beforeStep(StepExecution stepExecution) { - if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){ - Iterator iter= jobExecutionContextListeners.iterator(); - while(iter.hasNext()){ + if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) { + Iterator iter = jobExecutionContextListeners.iterator(); + while (iter.hasNext()) { iter.next().restoreContext(stepExecution); } } - + } @Override public ExitStatus afterStep(StepExecution stepExecution) { - if(!CollectionUtils.isEmpty(jobExecutionContextListeners)){ - Iterator iter= jobExecutionContextListeners.iterator(); - while(iter.hasNext()){ + if (!CollectionUtils.isEmpty(jobExecutionContextListeners)) { + Iterator iter = jobExecutionContextListeners.iterator(); + while (iter.hasNext()) { iter.next().clearContext(stepExecution); } } diff --git a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/util/CustomJobParameter.java b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/util/CustomJobParameter.java deleted file mode 100644 index 2245e13..0000000 --- a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/util/CustomJobParameter.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.digibooster.spring.batch.util; - -import java.io.Serializable; -import java.util.UUID; - -import org.springframework.batch.core.JobParameter; - -/** - * Customized generic class that inherits {@link JobParameter}in order to bypass the restriction made - * by spring batch for parameter types (String, Date, Double,Long). - * This class was copied from StackOverflow - * - * Question link - * - */ -public class CustomJobParameter extends JobParameter { - - private static final long serialVersionUID = -5137416276827316303L; - - private T value; - - public CustomJobParameter(T value){ - super(UUID.randomUUID().toString()); - this.value = value; - } - - @Override - public T getValue(){ - return value; - } -} \ No newline at end of file diff --git a/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/util/SerializableJobParameter.java b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/util/SerializableJobParameter.java new file mode 100644 index 0000000..9eb759f --- /dev/null +++ b/spring-batch-context-commons/src/main/java/com/digibooster/spring/batch/util/SerializableJobParameter.java @@ -0,0 +1,33 @@ +package com.digibooster.spring.batch.util; + +import java.io.Serializable; +import java.util.UUID; + +import org.springframework.batch.core.JobParameter; + +/** + * Customized generic class that inherits {@link JobParameter}in order to bypass + * the restriction made by spring batch for parameter types (String, Date, + * Double,Long). This class was copied from StackOverflow + * + * Question + * link + * + */ +public class SerializableJobParameter extends JobParameter { + + private static final long serialVersionUID = -5137416276827316303L; + + private T value; + + public SerializableJobParameter(T value) { + super(UUID.randomUUID().toString()); + this.value = value; + } + + @Override + public T getValue() { + return value; + } +} \ No newline at end of file diff --git a/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/aop/JobExecutionAspectTest.java b/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/aop/JobExecutionAspectTest.java index c018a53..4ad3029 100644 --- a/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/aop/JobExecutionAspectTest.java +++ b/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/aop/JobExecutionAspectTest.java @@ -26,55 +26,50 @@ import com.digibooster.spring.batch.config.TestConfiguration; - @RunWith(SpringRunner.class) @ContextConfiguration(classes = { TestConfiguration.class }) -@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, - DirtiesContextTestExecutionListener.class}) +@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) @TestPropertySource(locations = "classpath:application.properties") public class JobExecutionAspectTest { - + JobLauncher aspectJobLauncher; - - TestJobLauncher jobLauncher =new TestJobLauncher(); - + + TestJobLauncher jobLauncher = new TestJobLauncher(); + @Autowired JobExecutionAspect jobExecutionAspect; - - + @Before - public void setUp() { - - AspectJProxyFactory aspectJProxyFactory = new AspectJProxyFactory(jobLauncher); - aspectJProxyFactory.addAspect(jobExecutionAspect); + public void setUp() { + + AspectJProxyFactory aspectJProxyFactory = new AspectJProxyFactory(jobLauncher); + aspectJProxyFactory.addAspect(jobExecutionAspect); - DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory(); - AopProxy aopProxy = proxyFactory.createAopProxy(aspectJProxyFactory); + DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory(); + AopProxy aopProxy = proxyFactory.createAopProxy(aspectJProxyFactory); - aspectJobLauncher = (JobLauncher) aopProxy.getProxy(); - } + aspectJobLauncher = (JobLauncher) aopProxy.getProxy(); + } - @Test - public void testBeforeRun() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException{ - JobParametersBuilder expectedParametersBuilder= new JobParametersBuilder(); + public void testBeforeRun() throws JobExecutionAlreadyRunningException, JobRestartException, + JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException { + JobParametersBuilder expectedParametersBuilder = new JobParametersBuilder(); expectedParametersBuilder.addString("originalParam", "originalParam"); expectedParametersBuilder.addString("Param1", "textParam1"); expectedParametersBuilder.addLong("Param2", 12L); - - JobParametersBuilder jobParametersBuilder= new JobParametersBuilder(); + + JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); jobParametersBuilder.addString("originalParam", "originalParam"); aspectJobLauncher.run(null, jobParametersBuilder.toJobParameters()); - + Assert.assertEquals(expectedParametersBuilder.toJobParameters(), jobLauncher.getJobParameters()); } - - - - public static class TestJobLauncher implements JobLauncher{ - + + public static class TestJobLauncher implements JobLauncher { + private JobParameters jobParameters; - + public JobParameters getJobParameters() { return jobParameters; } @@ -82,7 +77,7 @@ public JobParameters getJobParameters() { @Override public JobExecution run(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { - this.jobParameters=jobParameters; + this.jobParameters = jobParameters; return null; } } diff --git a/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/config/JobExecutionContextListenerTest.java b/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/config/JobExecutionContextListenerTest.java index 80e4575..66643d6 100644 --- a/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/config/JobExecutionContextListenerTest.java +++ b/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/config/JobExecutionContextListenerTest.java @@ -25,25 +25,25 @@ public void insertContextInfo(JobParametersBuilder jobParametersBuilder) { @Override public void fillJobExecutionContext(JobExecution jobExecution) { // TODO Auto-generated method stub - + } @Override public void removeFromJobExecutionContext(JobExecution jobExecution) { // TODO Auto-generated method stub - + } @Override public void restoreContext(StepExecution stepExecution) { // TODO Auto-generated method stub - + } @Override public void clearContext(StepExecution stepExecution) { // TODO Auto-generated method stub - + } } \ No newline at end of file diff --git a/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/config/TestConfiguration.java b/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/config/TestConfiguration.java index db4d6dc..767b259 100644 --- a/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/config/TestConfiguration.java +++ b/spring-batch-context-commons/src/test/java/com/digibooster/spring/batch/config/TestConfiguration.java @@ -5,13 +5,12 @@ import org.springframework.context.annotation.Configuration; @Configuration -@ComponentScan(basePackages={"com.digibooster.spring.batch.aop"}) +@ComponentScan(basePackages = { "com.digibooster.spring.batch.aop" }) public class TestConfiguration { - @Bean public JobExecutionContextListenerTest jobExecutionContextListener() { return new JobExecutionContextListenerTest(); } - + } diff --git a/spring-batch-locale/pom.xml b/spring-batch-locale/pom.xml index 8b53058..ae01648 100644 --- a/spring-batch-locale/pom.xml +++ b/spring-batch-locale/pom.xml @@ -5,7 +5,7 @@ com.digibooster.spring.batch spring-batch-context - 0.0.1-SNAPSHOT + 1.0.0 spring-batch-locale Spring Batch Locale diff --git a/spring-batch-locale/src/main/java/com/digibooster/spring/batch/locale/listener/JobExecutionLocaleContextListener.java b/spring-batch-locale/src/main/java/com/digibooster/spring/batch/locale/listener/JobExecutionLocaleContextListener.java index 5838a85..9e32c28 100644 --- a/spring-batch-locale/src/main/java/com/digibooster/spring/batch/locale/listener/JobExecutionLocaleContextListener.java +++ b/spring-batch-locale/src/main/java/com/digibooster/spring/batch/locale/listener/JobExecutionLocaleContextListener.java @@ -9,10 +9,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.digibooster.spring.batch.listener.JobExecutionContextListener; -import com.digibooster.spring.batch.util.CustomJobParameter; +import com.digibooster.spring.batch.util.SerializableJobParameter; /** * This class restores the Locale context inside the Spring batch job + * * @author Mohammed ZAHID * */ @@ -27,17 +28,18 @@ public class JobExecutionLocaleContextListener implements JobExecutionContextLis @Override public void insertContextInfo(JobParametersBuilder jobParametersBuilder) { log.debug("Insert the Locale values"); - Locale locale= LocaleContextHolder.getLocale(); - jobParametersBuilder.addParameter(LOCALE_PARAM_NAME, new CustomJobParameter(locale)); + Locale locale = LocaleContextHolder.getLocale(); + jobParametersBuilder.addParameter(LOCALE_PARAM_NAME, new SerializableJobParameter(locale)); } @Override public void fillJobExecutionContext(JobExecution jobExecution) { log.debug("Restore the locale context"); - CustomJobParameter locale = (CustomJobParameter) jobExecution.getJobParameters().getParameters().get(LOCALE_PARAM_NAME); + SerializableJobParameter locale = (SerializableJobParameter) jobExecution.getJobParameters() + .getParameters().get(LOCALE_PARAM_NAME); if (locale != null) { - jobExecution.getExecutionContext().put(LOCALE_PARAM_NAME, (Locale)locale.getValue()); + jobExecution.getExecutionContext().put(LOCALE_PARAM_NAME, (Locale) locale.getValue()); } else { log.error("Could not find parameter {} in order to restore the locale context", LOCALE_PARAM_NAME); } @@ -57,8 +59,8 @@ public void restoreContext(StepExecution stepExecution) { Locale locale = (Locale) stepExecution.getJobExecution().getExecutionContext().get(LOCALE_PARAM_NAME); Locale originalLocale = LocaleContextHolder.getLocale(); ORIGINAL_CONTEXT.set(originalLocale); - LocaleContextHolder.setLocale(locale,true); - + LocaleContextHolder.setLocale(locale, true); + } else { log.error("Could not find key {} in the job execution context", LOCALE_PARAM_NAME); } diff --git a/spring-batch-mdc/pom.xml b/spring-batch-mdc/pom.xml index 5b49201..5b1e714 100644 --- a/spring-batch-mdc/pom.xml +++ b/spring-batch-mdc/pom.xml @@ -5,7 +5,7 @@ com.digibooster.spring.batch spring-batch-context - 0.0.1-SNAPSHOT + 1.0.0 spring-batch-mdc Spring Batch MDC diff --git a/spring-batch-mdc/src/main/java/com/digibooster/spring/batch/mdc/config/MDCConfiguration.java b/spring-batch-mdc/src/main/java/com/digibooster/spring/batch/mdc/config/MDCConfiguration.java index c755711..c365fef 100644 --- a/spring-batch-mdc/src/main/java/com/digibooster/spring/batch/mdc/config/MDCConfiguration.java +++ b/spring-batch-mdc/src/main/java/com/digibooster/spring/batch/mdc/config/MDCConfiguration.java @@ -8,7 +8,7 @@ @Configuration public class MDCConfiguration { - + @Bean public JobExecutionContextListener jobExecutionMDCContextListener() { return new JobExecutionMDCContextListener(); diff --git a/spring-batch-mdc/src/main/java/com/digibooster/spring/batch/mdc/listener/JobExecutionMDCContextListener.java b/spring-batch-mdc/src/main/java/com/digibooster/spring/batch/mdc/listener/JobExecutionMDCContextListener.java index c0b025b..1428dd3 100644 --- a/spring-batch-mdc/src/main/java/com/digibooster/spring/batch/mdc/listener/JobExecutionMDCContextListener.java +++ b/spring-batch-mdc/src/main/java/com/digibooster/spring/batch/mdc/listener/JobExecutionMDCContextListener.java @@ -10,10 +10,12 @@ import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.StepExecution; import com.digibooster.spring.batch.listener.JobExecutionContextListener; -import com.digibooster.spring.batch.util.CustomJobParameter; +import com.digibooster.spring.batch.util.SerializableJobParameter; /** - * This class restores MDC stored information context inside the Spring batch job + * This class restores MDC stored information context inside the Spring batch + * job + * * @author Mohammed ZAHID * */ @@ -30,7 +32,7 @@ public void insertContextInfo(JobParametersBuilder jobParametersBuilder) { log.debug("Insert the MDC values"); Map mdc = MDC.getCopyOfContextMap(); if (mdc != null) { - jobParametersBuilder.addParameter(MDC_PARAM_NAME, new CustomJobParameter(new HashMap(mdc))); + jobParametersBuilder.addParameter(MDC_PARAM_NAME, new SerializableJobParameter(new HashMap(mdc))); } } @@ -38,9 +40,10 @@ public void insertContextInfo(JobParametersBuilder jobParametersBuilder) { @Override public void fillJobExecutionContext(JobExecution jobExecution) { log.debug("Restore the MC context"); - CustomJobParameter mdc = (CustomJobParameter) jobExecution.getJobParameters().getParameters().get(MDC_PARAM_NAME); + SerializableJobParameter mdc = (SerializableJobParameter) jobExecution.getJobParameters() + .getParameters().get(MDC_PARAM_NAME); if (mdc != null) { - jobExecution.getExecutionContext().put(MDC_PARAM_NAME, (HashMap)mdc.getValue()); + jobExecution.getExecutionContext().put(MDC_PARAM_NAME, (HashMap) mdc.getValue()); } else { log.error("Could not find parameter {} in order to restore the MDC context", MDC_PARAM_NAME); } @@ -57,14 +60,15 @@ public void removeFromJobExecutionContext(JobExecution jobExecution) { public void restoreContext(StepExecution stepExecution) { if (stepExecution.getJobExecution().getExecutionContext().containsKey(MDC_PARAM_NAME)) { log.debug("Restore the MDC context"); - HashMap mdc = (HashMap) stepExecution.getJobExecution().getExecutionContext().get(MDC_PARAM_NAME); + HashMap mdc = (HashMap) stepExecution.getJobExecution().getExecutionContext() + .get(MDC_PARAM_NAME); Map originalMdc = MDC.getMDCAdapter().getCopyOfContextMap(); ORIGINAL_CONTEXT.set(originalMdc); MDC.clear(); - for(Map.Entry entry:mdc.entrySet()) { + for (Map.Entry entry : mdc.entrySet()) { MDC.put(entry.getKey(), entry.getValue()); } - + } else { log.error("Could not find key {} in the job execution context", MDC_PARAM_NAME); } diff --git a/spring-batch-security/pom.xml b/spring-batch-security/pom.xml index 5d28f2c..31419b8 100644 --- a/spring-batch-security/pom.xml +++ b/spring-batch-security/pom.xml @@ -5,7 +5,7 @@ com.digibooster.spring.batch spring-batch-context - 0.0.1-SNAPSHOT + 1.0.0 spring-batch-security diff --git a/spring-batch-security/src/main/java/com/digibooster/spring/batch/security/config/SpringSecurityConfiguration.java b/spring-batch-security/src/main/java/com/digibooster/spring/batch/security/config/SpringSecurityConfiguration.java index daa45b2..5929fd2 100644 --- a/spring-batch-security/src/main/java/com/digibooster/spring/batch/security/config/SpringSecurityConfiguration.java +++ b/spring-batch-security/src/main/java/com/digibooster/spring/batch/security/config/SpringSecurityConfiguration.java @@ -8,9 +8,9 @@ @Configuration public class SpringSecurityConfiguration { - + @Bean - public JobExecutionContextListener jobExecutionMDCContextListener() { + public JobExecutionContextListener jobExecutionSecurityContextListener() { return new JobExecutionSecurityContextListener(); } diff --git a/spring-batch-security/src/main/java/com/digibooster/spring/batch/security/listener/JobExecutionSecurityContextListener.java b/spring-batch-security/src/main/java/com/digibooster/spring/batch/security/listener/JobExecutionSecurityContextListener.java index abdc81c..c376e5d 100644 --- a/spring-batch-security/src/main/java/com/digibooster/spring/batch/security/listener/JobExecutionSecurityContextListener.java +++ b/spring-batch-security/src/main/java/com/digibooster/spring/batch/security/listener/JobExecutionSecurityContextListener.java @@ -11,61 +11,63 @@ import org.springframework.batch.core.StepExecution; import com.digibooster.spring.batch.listener.JobExecutionContextListener; -import com.digibooster.spring.batch.util.CustomJobParameter; +import com.digibooster.spring.batch.util.SerializableJobParameter; /** * This class restores spring-security context inside the Spring batch job + * * @author Mohammed ZAHID * */ -public class JobExecutionSecurityContextListener implements JobExecutionContextListener{ +public class JobExecutionSecurityContextListener implements JobExecutionContextListener { private final Logger log = LoggerFactory.getLogger(JobExecutionSecurityContextListener.class); - - private static final String SECURITY_PARAM_NAME="security-param"; - + + private static final String SECURITY_PARAM_NAME = "security-param"; + private static final ThreadLocal ORIGINAL_CONTEXT = new ThreadLocal<>(); - + @Override public void insertContextInfo(JobParametersBuilder jobParametersBuilder) { log.debug("Insert the security context"); - SecurityContext securityContext= SecurityContextHolder.getContext(); - Authentication authentication= securityContext.getAuthentication(); - if(authentication!=null) { - jobParametersBuilder.addParameter(SECURITY_PARAM_NAME, new CustomJobParameter(authentication)); + SecurityContext securityContext = SecurityContextHolder.getContext(); + Authentication authentication = securityContext.getAuthentication(); + if (authentication != null) { + jobParametersBuilder.addParameter(SECURITY_PARAM_NAME, + new SerializableJobParameter(authentication)); } } @Override public void fillJobExecutionContext(JobExecution jobExecution) { log.debug("Restore the scurity context"); - CustomJobParameter authentication=(CustomJobParameter) jobExecution.getJobParameters().getParameters().get(SECURITY_PARAM_NAME); - if(authentication!=null) { - jobExecution.getExecutionContext().put(SECURITY_PARAM_NAME,(Authentication)authentication.getValue()); - } - else { - log.error("Could not find parameter {} in order to restore the security context",SECURITY_PARAM_NAME); + SerializableJobParameter authentication = (SerializableJobParameter) jobExecution + .getJobParameters().getParameters().get(SECURITY_PARAM_NAME); + if (authentication != null) { + jobExecution.getExecutionContext().put(SECURITY_PARAM_NAME, (Authentication) authentication.getValue()); + } else { + log.error("Could not find parameter {} in order to restore the security context", SECURITY_PARAM_NAME); } - + } @Override public void removeFromJobExecutionContext(JobExecution jobExecution) { jobExecution.getExecutionContext().remove(SECURITY_PARAM_NAME); - + } @Override public void restoreContext(StepExecution stepExecution) { - if(stepExecution.getJobExecution().getExecutionContext().containsKey(SECURITY_PARAM_NAME)) { + if (stepExecution.getJobExecution().getExecutionContext().containsKey(SECURITY_PARAM_NAME)) { log.debug("Restore the security context"); - Authentication authentication=(Authentication) stepExecution.getJobExecution().getExecutionContext().get(SECURITY_PARAM_NAME); - SecurityContext securityContext= SecurityContextHolder.getContext(); + Authentication authentication = (Authentication) stepExecution.getJobExecution().getExecutionContext() + .get(SECURITY_PARAM_NAME); + SecurityContext securityContext = SecurityContextHolder.getContext(); ORIGINAL_CONTEXT.set(securityContext.getAuthentication()); securityContext.setAuthentication(authentication); - } - else { - log.error("Could not find key {} in the job execution context",SECURITY_PARAM_NAME); + } else { + log.error("Could not find key {} in the job execution context", SECURITY_PARAM_NAME); } } @@ -74,7 +76,7 @@ public void clearContext(StepExecution stepExecution) { log.debug("Clear the security context"); SecurityContextHolder.clearContext(); Authentication originalAuth = ORIGINAL_CONTEXT.get(); - if(originalAuth!=null) { + if (originalAuth != null) { SecurityContextHolder.getContext().setAuthentication(originalAuth); ORIGINAL_CONTEXT.remove(); } diff --git a/spring-batch-sleuth/pom.xml b/spring-batch-sleuth/pom.xml index b14543d..a221e83 100644 --- a/spring-batch-sleuth/pom.xml +++ b/spring-batch-sleuth/pom.xml @@ -5,7 +5,7 @@ com.digibooster.spring.batch spring-batch-context - 0.0.1-SNAPSHOT + 1.0.0 spring-batch-sleuth @@ -13,6 +13,10 @@ + + 1.3.0.RELEASE + + org.springframework.cloud diff --git a/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/SpanInfoHolder.java b/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/SpanInfoHolder.java index 17eb3c8..4eae4e2 100644 --- a/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/SpanInfoHolder.java +++ b/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/SpanInfoHolder.java @@ -13,10 +13,10 @@ * @see Span * @author Mohammed ZAHID */ -public class SpanInfoHolder implements Serializable{ - +public class SpanInfoHolder implements Serializable { + private static final long serialVersionUID = -8532072257517004671L; - + private String name; private long traceIdHigh; private long traceId; @@ -26,65 +26,84 @@ public class SpanInfoHolder implements Serializable{ private boolean exportable = true; private Map tags; private String processId; - private Map baggage; - + private Map baggage; + public String getName() { return name; } + public void setName(String name) { this.name = name; } + public long getTraceIdHigh() { return traceIdHigh; } + public void setTraceIdHigh(long traceIdHigh) { this.traceIdHigh = traceIdHigh; } + public long getTraceId() { return traceId; } + public void setTraceId(long traceId) { this.traceId = traceId; } + public List getParents() { return parents; } + public void setParents(List parents) { this.parents = parents; } + public long getSpanId() { return spanId; } + public void setSpanId(long spanId) { this.spanId = spanId; } + public boolean isRemote() { return remote; } + public void setRemote(boolean remote) { this.remote = remote; } + public boolean isExportable() { return exportable; } + public void setExportable(boolean exportable) { this.exportable = exportable; } + public Map getTags() { return tags; } + public void setTags(Map tags) { this.tags = tags; } + public String getProcessId() { return processId; } + public void setProcessId(String processId) { this.processId = processId; } + public Map getBaggage() { return baggage; } + public void setBaggage(Map baggage) { this.baggage = baggage; } diff --git a/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/config/SleuthConfiguration.java b/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/config/SleuthConfiguration.java index c78d0e0..e9faa6f 100644 --- a/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/config/SleuthConfiguration.java +++ b/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/config/SleuthConfiguration.java @@ -10,9 +10,9 @@ @Configuration public class SleuthConfiguration { - + @Bean - public JobExecutionContextListener jobExecutionMDCContextListener(@Autowired Tracer tracer) { + public JobExecutionContextListener jobExecutionSleuthContextListener(@Autowired Tracer tracer) { return new JobExecutionSleuthContextListener(tracer); } diff --git a/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/listener/JobExecutionSleuthContextListener.java b/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/listener/JobExecutionSleuthContextListener.java index cdf2c3d..3301712 100644 --- a/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/listener/JobExecutionSleuthContextListener.java +++ b/spring-batch-sleuth/src/main/java/com/digibooster/spring/batch/sleuth/listener/JobExecutionSleuthContextListener.java @@ -1,6 +1,5 @@ package com.digibooster.spring.batch.sleuth.listener; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.batch.core.JobExecution; @@ -10,105 +9,90 @@ import org.springframework.cloud.sleuth.Tracer; import com.digibooster.spring.batch.listener.JobExecutionContextListener; import com.digibooster.spring.batch.sleuth.SpanInfoHolder; -import com.digibooster.spring.batch.util.CustomJobParameter; +import com.digibooster.spring.batch.util.SerializableJobParameter; /** * This class restores sleuth context inside the Spring batch job + * * @author Mohammed ZAHID * */ -public class JobExecutionSleuthContextListener implements JobExecutionContextListener{ +public class JobExecutionSleuthContextListener implements JobExecutionContextListener { private final Logger log = LoggerFactory.getLogger(JobExecutionSleuthContextListener.class); - private static final String SLEUTH_PARAM_NAME="sleuth-param"; - + private static final String SLEUTH_PARAM_NAME = "sleuth-param"; + private final Tracer tracer; - + private static final ThreadLocal ORIGINAL_CONTEXT = new ThreadLocal<>(); - + public JobExecutionSleuthContextListener(Tracer tracer) { - this.tracer= tracer; + this.tracer = tracer; } - @Override public void insertContextInfo(JobParametersBuilder jobParametersBuilder) { log.debug("Save the sleuth context"); - Span currentSpan= tracer.getCurrentSpan(); - SpanInfoHolder infoHolder= new SpanInfoHolder(); - infoHolder.setName(currentSpan.getName()); - infoHolder.setTraceIdHigh(currentSpan.getTraceIdHigh()); - infoHolder.setTraceId(currentSpan.getTraceId()); - infoHolder.setParents(currentSpan.getParents()); - infoHolder.setSpanId(currentSpan.getSpanId()); - infoHolder.setRemote(currentSpan.isRemote()); - infoHolder.setExportable(currentSpan.isExportable()); - infoHolder.setTags(currentSpan.tags()); - infoHolder.setProcessId(currentSpan.getProcessId()); - infoHolder.setBaggage(currentSpan.getBaggage()); - jobParametersBuilder.addParameter(SLEUTH_PARAM_NAME, new CustomJobParameter(infoHolder)); + Span currentSpan = tracer.getCurrentSpan(); + SpanInfoHolder infoHolder = new SpanInfoHolder(); + infoHolder.setName(currentSpan.getName()); + infoHolder.setTraceIdHigh(currentSpan.getTraceIdHigh()); + infoHolder.setTraceId(currentSpan.getTraceId()); + infoHolder.setParents(currentSpan.getParents()); + infoHolder.setSpanId(currentSpan.getSpanId()); + infoHolder.setRemote(currentSpan.isRemote()); + infoHolder.setExportable(currentSpan.isExportable()); + infoHolder.setTags(currentSpan.tags()); + infoHolder.setProcessId(currentSpan.getProcessId()); + infoHolder.setBaggage(currentSpan.getBaggage()); + jobParametersBuilder.addParameter(SLEUTH_PARAM_NAME, new SerializableJobParameter(infoHolder)); } - @Override public void fillJobExecutionContext(JobExecution jobExecution) { log.debug("Restore the scurity context"); - CustomJobParameter spanParameter= (CustomJobParameter) jobExecution.getJobParameters().getParameters().get(SLEUTH_PARAM_NAME); - if(spanParameter!=null) { - SpanInfoHolder infoHolder = (SpanInfoHolder)spanParameter.getValue(); - Span newSpan= Span - .builder() - .name(infoHolder.getName()) - .traceIdHigh(infoHolder.getTraceIdHigh()) - .traceId(infoHolder.getTraceId()) - .parents(infoHolder.getParents()) - .spanId(infoHolder.getSpanId()) - .remote(infoHolder.isRemote()) - .exportable(infoHolder.isExportable()) - .tags(infoHolder.getTags()) - .processId(infoHolder.getProcessId()) - .baggage(infoHolder.getBaggage()) - .build(); + SerializableJobParameter spanParameter = (SerializableJobParameter) jobExecution + .getJobParameters().getParameters().get(SLEUTH_PARAM_NAME); + if (spanParameter != null) { + SpanInfoHolder infoHolder = (SpanInfoHolder) spanParameter.getValue(); + Span newSpan = Span.builder().name(infoHolder.getName()).traceIdHigh(infoHolder.getTraceIdHigh()) + .traceId(infoHolder.getTraceId()).parents(infoHolder.getParents()).spanId(infoHolder.getSpanId()) + .remote(infoHolder.isRemote()).exportable(infoHolder.isExportable()).tags(infoHolder.getTags()) + .processId(infoHolder.getProcessId()).baggage(infoHolder.getBaggage()).build(); jobExecution.getExecutionContext().put(SLEUTH_PARAM_NAME, newSpan); - - } - else { - log.error("Could not find the key {} in job parameters",SLEUTH_PARAM_NAME); + + } else { + log.error("Could not find the key {} in job parameters", SLEUTH_PARAM_NAME); } - - } + } @Override public void removeFromJobExecutionContext(JobExecution jobExecution) { jobExecution.getExecutionContext().remove(SLEUTH_PARAM_NAME); } - @Override public void restoreContext(StepExecution stepExecution) { - if(stepExecution.getJobExecution().getExecutionContext().containsKey(SLEUTH_PARAM_NAME)) { - Span newSpan=(Span) stepExecution.getJobExecution().getExecutionContext().get(SLEUTH_PARAM_NAME); - Span originalSpan= tracer.getCurrentSpan(); + if (stepExecution.getJobExecution().getExecutionContext().containsKey(SLEUTH_PARAM_NAME)) { + Span newSpan = (Span) stepExecution.getJobExecution().getExecutionContext().get(SLEUTH_PARAM_NAME); + Span originalSpan = tracer.getCurrentSpan(); ORIGINAL_CONTEXT.set(originalSpan); tracer.continueSpan(newSpan); + } else { + log.error("Could not find key {} in the job execution context", SLEUTH_PARAM_NAME); } - else { - log.error("Could not find key {} in the job execution context",SLEUTH_PARAM_NAME); - } - - } + } @Override public void clearContext(StepExecution stepExecution) { Span originalSpan = ORIGINAL_CONTEXT.get(); - if(originalSpan!=null) { + if (originalSpan != null) { tracer.continueSpan(originalSpan); ORIGINAL_CONTEXT.remove(); } - + } - - + }