Java 我们可以在Spring Retry中配置两个RetryConfiguration类吗?

Java 我们可以在Spring Retry中配置两个RetryConfiguration类吗?,java,spring-batch,spring-retry,Java,Spring Batch,Spring Retry,我们可以在Spring批处理中配置两个retryConfigurations类以在Tasklet中使用吗?我有复杂的东西,所以需要两种不同的重试机制。只是想了解应该调用哪个重试模板 有人能给我一些快速的提示吗 @EnableRetry @Configuration @PropertySource("classpath:config.properties") public class ABCRetryConfigurations { @Value("${retryPeriod}")

我们可以在Spring批处理中配置两个retryConfigurations类以在Tasklet中使用吗?我有复杂的东西,所以需要两种不同的重试机制。只是想了解应该调用哪个重试模板

有人能给我一些快速的提示吗

@EnableRetry
@Configuration
@PropertySource("classpath:config.properties")
public class ABCRetryConfigurations {
    @Value("${retryPeriod}")
    private int retryIntervalInSeconds;

    @Bean
    public RetryTemplate retryTemplate() {
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(3);

        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
        backOffPolicy.setBackOffPeriod(retryIntervalInSeconds * 1000);

        RetryTemplate template = new RetryTemplate();
        template.setRetryPolicy(retryPolicy);
        template.setBackOffPolicy(backOffPolicy);

        return template;
    }
}

这是我的密码

public class XXXXXXXX {
    private static Logger logger = Logger.getLogger(XXXXXXXXX.class);

    @Autowired
    private RetryTemplate retryTemplate; 

    private Set<String> abcFileNames;
    private ChunkContext paramChunkContext; 

    public XXXXXXXX(Set<String> abcFileNames, SendXXXTasklet tasklet, ChunkContext paramChunkContext) {
        this.abcFileNames = abcFileNames;
        this.tasklet = tasklet;
        this.paramChunkContext = paramChunkContext;
    }

    public RepeatStatus executeXXXXRetry() {
        // Retry configuration
        retryTemplate.setBackOffPolicy( fixedBackOffPolicy );

        return retryTemplate.execute(context -> {
            return tasklet.XXX(abcFileNames, paramChunkContext);
        });
    }
}
只是想了解应该调用哪个重试模板

哪一个应该被调用取决于您告诉Spring容器要自动连接哪个重试模板。在您的示例中,您有两个RetryTemplate bean RetryTemplate和billingRetryTemplate,并且您希望在XXXXXXXX类中自动关联一个RetryTemplate RetryTemplate。在您的例子中,是RetryTemplatebean将按名称自动连接。默认情况下,方法名称是bean名称。您可以使用@Primary或@Qualifier更明确地说明要使用哪一个

您可以在Spring框架文档中找到更多关于此的详细信息:

public class XXXXXXXX {
    private static Logger logger = Logger.getLogger(XXXXXXXXX.class);

    @Autowired
    private RetryTemplate retryTemplate; 

    private Set<String> abcFileNames;
    private ChunkContext paramChunkContext; 

    public XXXXXXXX(Set<String> abcFileNames, SendXXXTasklet tasklet, ChunkContext paramChunkContext) {
        this.abcFileNames = abcFileNames;
        this.tasklet = tasklet;
        this.paramChunkContext = paramChunkContext;
    }

    public RepeatStatus executeXXXXRetry() {
        // Retry configuration
        retryTemplate.setBackOffPolicy( fixedBackOffPolicy );

        return retryTemplate.execute(context -> {
            return tasklet.XXX(abcFileNames, paramChunkContext);
        });
    }
}