当前位置: 首页 > 知识库问答 >
问题:

带注释和缓存的Spring批处理

籍靖
2023-03-14

共有1个答案

袁鸿达
2023-03-14

JobExecutionListener可用于在作业执行前用引用数据填充缓存,并在作业完成后清除缓存。

下面是一个例子:

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
@EnableBatchProcessing
public 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
 类似资料:
  • 问题内容: 有没有人有很好的Spring Batch(使用批注)示例来缓存可供处理器访问的参考表? 我只需要一个简单的缓存,运行一个查询,该查询返回一些byte []并将其保留在内存中,直到执行作业。 感谢有关此主题的任何帮助。 谢谢 ! 问题答案: 可以使用A 在执行作业之前用参考数据填充高速缓存,并在作业完成后清除高速缓存。 这是一个例子: 执行后,将打印: 这意味着可以从缓存中正确检索数据。

  • 我的Spring应用程序由两个上下文xml配置文件组成,第一个是根上下文。xml仅扫描非控制器带注释的bean: 而第二个servlet上下文。xml包含所有spring mvc设置和扫描控制器带注释的bean web.xml上的DispatcherServlet配置如下所示 我想尝试基于注释的缓存,所以我将以下bean定义添加到root-context.xml 并使用一个带有注释的类来测试这一点

  • 主要内容:使用Rem语句注释,注释使用::声明为创建的脚本添加注释或文档总是一个好习惯。 这是一个维护脚本用来理解脚本实际所做的事情所必需的注释。 例如,考虑下面这段没有注释形式的代码。 如果一个没有任何注释的脚本,普通人试图理解脚本,那么需要很多时间来理解脚本做些什么工作。 使用Rem语句注释 有两种方法可以在批处理脚本中创建注释; 一个是通过命令。 语句后的任何文本都将被视为注释,不会被执行。 以下是此声明的一般语法。 语法 其中是需要添

  • 我想将主数据缓存到Redis。 所以,我写了这些代码。 和 和ymlfile 但是,缓存不工作... 无论何时调用printTest方法,都将执行“getTest”方法。Redis没有数据。。。我的代码中有什么问题? SpringBoot版本是1.4.0 依赖关系是

  • 我正在使用注释处理器来处理方法参数的注释。 用于参数的注释类型有一个注释@参数 现在,当注释处理器运行时,我想检查参数注释()是否有参数注释。我通过执行以下代码来实现这一点。 由于某种原因,arg始终为空。是否有注释未返回的原因?

  • 我按照本教程在Java中配置Spring批处理作业。它通过使用一个接口为多个数据源做准备,然后由每个数据源实现该接口。 这是我目前所掌握的: PostgreSQLConfig.java jobconfig.java 通过对我的MySQLConfig使用注释,我希望使用MySQLConfig bean。相反,我得到的是: