栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带批注和缓存的Spring Batch

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带批注和缓存的Spring Batch

JobExecutionListener
可以使用A 在执行作业之前用参考数据填充高速缓存,并在作业完成后清除高速缓存。

这是一个例子:

import org.springframework.batch.core.Job;import org.springframework.batch.core.JobExecution;import org.springframework.batch.core.JobExecutionListener;import org.springframework.batch.core.JobParameters;import org.springframework.batch.core.Step;import org.springframework.batch.core.StepContribution;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.launch.JobLauncher;import org.springframework.batch.core.scope.context.ChunkContext;import org.springframework.batch.core.step.tasklet.Tasklet;import org.springframework.batch.repeat.RepeatStatus;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.CacheManager;import org.springframework.cache.concurrent.ConcurrentMapCacheManager;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@EnableBatchProcessingpublic class MyJob {    private JobBuilderFactory jobs;    private StepBuilderFactory steps;    public MyJob(JobBuilderFactory jobs, StepBuilderFactory steps) {       this.jobs = jobs;       this.steps = steps;    }    @Bean    public CacheManager cacheManager() {        return new ConcurrentMapCacheManager(); // return the implementation you want    }    @Bean    public Tasklet tasklet() {        return new MyTasklet(cacheManager());    }    @Bean    public Step step() {        return steps.get("step")     .tasklet(tasklet())     .build();    }    @Bean    public JobExecutionListener jobExecutionListener() {        return new CachingJobExecutionListener(cacheManager());    }    @Bean    public Job job() {        return jobs.get("job")     .start(step())     .listener(jobExecutionListener())     .build();    }    class MyTasklet implements Tasklet {        private CacheManager cacheManager;        public MyTasklet(CacheManager cacheManager) { this.cacheManager = cacheManager;        }        @Override        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { String name = (String) cacheManager.getCache("referenceData").get("foo").get(); System.out.println("Hello " + name); return RepeatStatus.FINISHED;        }    }    class CachingJobExecutionListener implements JobExecutionListener {        private CacheManager cacheManager;        public CachingJobExecutionListener(CacheManager cacheManager) { this.cacheManager = cacheManager;        }        @Override        public void beforeJob(JobExecution jobExecution) { // populate cache as needed. Can use a jdbcTemplate to query the db here and populate the cache cacheManager.getCache("referenceData").put("foo", "bar");        }        @Override        public void afterJob(JobExecution jobExecution) { // clear cache when the job is finished cacheManager.getCache("referenceData").clear();        }    }    public static void main(String[] args) throws Exception {        ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);        JobLauncher jobLauncher = context.getBean(JobLauncher.class);        Job job = context.getBean(Job.class);        jobLauncher.run(job, new JobParameters());    }}

执行后,将打印:

Hello bar

这意味着可以从缓存中正确检索数据。您将需要调整样本以查询数据库并填充缓存(请参见代码中的注释)。

希望这可以帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/365859.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号