Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

40.5. @EnableResilientMethods

启用配置,在配置类上添加@EnableResilientMethods注解

40.5.1. @ConcurrencyLimit 连流

在需要限制并发的方法上添加@ConcurrencyLimit注解

		
@Configuration
@EnableResilientMethods
public class ResilientMethodsConfig {
    // 配置类
}

@Component
public class NotificationService {
    @ConcurrencyLimit(10)
    public void sendNotification() {
        this.jmsClient.destination("notifications").send(...);
    }
    
    @ConcurrencyLimit(1)
    public void processCriticalTask() {
        // 关键任务处理
    }
}		
		
			

40.5.2. @Retry 重试

			
@Retryable
public void sendNotification() {
    this.jmsClient.destination("notifications").send(...);
}			
			
			

可通过@Retryable注解中的includes、excludes和implicit值属性,限制触发重试的异常类型。

			
@Retryable(  includes = MessageDeliveryException.class,  maxAttempts = 5,  delay = 100,  jitter = 10,  multiplier = 2,  maxDelay = 1000)
public void sendNotification() {    
	this.jmsClient.destination("notifications").send(...);
}			
			
			

RetryTemplate

			
var retryPolicy = RetryPolicy.builder()
        .includes(MessageDeliveryException.class)
        .maxAttempts(5)
        .delay(Duration.ofMillis(100))
        .build();
var retryTemplate = new RetryTemplate(retryPolicy);
retryTemplate.execute(() -> jmsClient.destination("notifications").send(...));