Spring Event + 异步实现业务的解耦(Spring Retry重试)

使用场景

  • 项目开发中经常会涉及到非常复杂的业务逻辑,如果全部耦合在一起,一个类的代码就会非常长,所以需要我们对业务代码进行解耦。通常在一个复杂的逻辑业务里面,会包含一个核心业务和N个子业务,有些子业务,我们是不需要在当前请求中同步完成的,例如短信发送等。这时,我们可能会想到MQ。但是当我们不想引入MQ时,就可以考虑使用Spring Event,它相当于一个观察者模式,当一个Bean完成任务之后,会通知另一个Bean去执行相应的任务。
  • Spring Retry也是Spring里的一个组件,主要实现是发生异常后重新调用。当一些瞬时错误(例如网络问题)发生时,Spring Retry的重试可以帮我们避免这种异常造成的服务调用失败的情况。

使用案例

自定义事件

@Data
@AllArgsConstructor
public class MsgEvent {
    private String msgId;
}

定义监听器

@Component
@Slf4j
public class MsgListener {

    /**
     * value值表示当哪些异常的时候触发重试,
     * maxAttempts表示最大重试次数默认为3,
     * delay表示重试的延迟时间,
     * multiplier表示上一次延时时间是这一次的倍数。
     * @param event
     */
    @EventListener
    @Async
    @Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))
    public void sendMsg(MsgEvent event) {
        String msgId = event.getMsgId();
        StopWatch watch = new StopWatch(msgId);
        watch.start();
        log.info("开始发短信");
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        watch.stop();
        log.info("短信发送成功, 消息id:【{}】 | 耗时: ({})", msgId, watch.getLastTaskTimeMillis());
    }
}

启动类

添加@EnableAsync和@EnableRetry注解

@SpringBootApplication
@EnableAsync
@EnableRetry
public class LabApplication {
    public static void main(String[] args) {
        SpringApplication.run(LabApplication.class, args);
    }
}

单元测试

@SpringBootTest
@Slf4j
public class EventTest {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void msgTest() {
        applicationContext.publishEvent(new MsgEvent("123"));
        log.info("短信发送事件发布成功");
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

执行结果

猜你喜欢

转载自blog.csdn.net/WXF_Sir/article/details/128219760#comments_28229479