自定义 spring retry持久化方式

Spring Retry允许你自定义持久化机制,以便将重试状态保存到数据库或其他持久化存储中。你可以通过实现RetryContextCache接口来自定义持久化机制,并将其配置到RetryTemplate中。

自定义RetryTemplate

下面是一个简单的示例,演示如何实现自定义的重试上下文缓存:

import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryPolicy;
import org.springframework.retry.support.RetryTemplate;

public class CustomRetryTemplate extends RetryTemplate {

    private CustomRetryContextCache retryContextCache;

    public CustomRetryTemplate(CustomRetryContextCache retryContextCache) {
        this.retryContextCache = retryContextCache;
    }

    @Override
    public <T> T execute(RetryPolicy retryPolicy, RetryCallback<T> retryCallback, RecoveryCallback<T> recoveryCallback)
            throws Exception {
        RetryContext context = open(retryPolicy);
        try {
            return doExecute(retryCallback, recoveryCallback, context);
        } finally {
            close(retryPolicy, context);
        }
    }

    protected RetryContext open(RetryPolicy retryPolicy) {
        RetryContext context = retryContextCache.getContext(retryPolicy);
        retryPolicy.registerThrowable(context, new RuntimeException()); // Placeholder exception
        return context;
    }

    protected void close(RetryPolicy retryPolicy, RetryContext context) {
        retryContextCache.close(retryPolicy, context);
    }
}

在这个示例中,我们创建了一个名为CustomRetryTemplate的自定义重试模板,它使用了一个RetryContextCache来管理重试上下文的持久化。你需要实现RetryContextCache接口并提供自定义的持久化逻辑,例如将重试上下文保存到数据库或缓存中。

自定义 RetryContextCache

现了RetryContextCache接口并将其配置到CustomRetryTemplate中,你就可以像使用普通的RetryTemplate一样使用它,它会自动使用你的自定义持久化逻辑来管理重试状态。

import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.DefaultRetryState;
import org.springframework.retry.support.RetryContextCache;
import org.springframework.retry.support.RetryTemplate;

import java.util.HashMap;
import java.util.Map;

public class CustomRetryContextCache implements RetryContextCache {

    private Map<RetryPolicy, RetryContext> contextCache = new HashMap<>();

    @Override
    public RetryContext getContext(RetryPolicy retryPolicy) {
        synchronized (this) {
            if (!contextCache.containsKey(retryPolicy)) {
                contextCache.put(retryPolicy, new DefaultRetryContext());
            }
            return contextCache.get(retryPolicy);
        }
    }

    @Override
    public void close(RetryPolicy retryPolicy, RetryContext context) {
       
    }

    // Custom implementation of RetryContext
    private static class DefaultRetryContext implements RetryContext {
        private final Map<String, Object> map = new HashMap<>();

        @Override
        public RetryPolicy getRetryPolicy() {
            return new SimpleRetryPolicy(); 
        }

        @Override
        public Throwable getLastThrowable() {
            return null;
        }

        @Override
        public void setLastThrowable(Throwable throwable) {
           
        }

        @Override
        public int getRetryCount() {
            return 0; 
        }

        @Override
        public void registerThrowable(Throwable throwable) {
            
        }

        @Override
        public boolean isExhaustedOnly() {
            return false; 
        }

        @Override
        public Object getAttribute(String name) {
            return map.get(name);
        }

        @Override
        public void setAttribute(String name, Object value) {
            map.put(name, value);
        }
    }
}

public class CustomRetryTemplate extends RetryTemplate {

    public CustomRetryTemplate() {
        setRetryContextCache(new CustomRetryContextCache());
    }
}

一个简单的CustomRetryContextCache,它使用HashMap来缓存重试上下文。在真实场景中,你需要根据你的需求将重试上下文存储到持久化存储中,并实现逻辑来从持久化存储中加载重试上下文。这也是spring retry底层存储的方式,这里如果我们希望其持久化到DB等,方便后期的调优与定位。

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨同学~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值