spring boot2.x+ 异步多线程+设计模式(工厂+策略)实现多种邮件的发送

项目结构

├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─sea
│  │  │          └─order
│  │  │              ├─annotation  -- 自定义注解
│  │  │              ├─common      -- 公共内容
│  │  │              ├─components  --组件
│  │  │              ├─config      -- 配置
│  │  │              ├─controller  -- 视图层
│  │  │              ├─dto          -- 业务dto
│  │  │              ├─event       -- 事件
│  │  │              │  └─handler   -- 事件处理
│  │  │              ├─factory      -- 工厂
│  │  │              ├─service      -- 服务
│  │  │              └─strategy      -- 策略
│  │  └─resources
│  │      └─templates               --html模板
│  └─test
│      └─java
│          └─com
│              └─sea
│                  └─order
│                      └─service   -- 单元测试

以上就是本项目的结构

背景

随着电商业务的扩展,大家都知道java在电商业务中的霸主地位,本次blog将分享公司项目里用到的多线程实现邮件发送。

设计模式的应用

策略+工厂

package com.sea.order.strategy;


import com.sea.order.common.R;
import com.sea.order.dto.OrderDTO;

/**
 * @description: 发送订单邮件策略(下单,取消订单,缺货)
 * @author: sea
 * @time: 2020/5/2 0002 20:54
 */
public interface OrderEmailTypeStrategyInterface {
   

    /**
     *
     * 发送订单邮件共同抽象
     * @author sea
     * @date 2020/8/13 21:40
     * @param [type, orderDTO]
     * @return com.sea.order.common.R<java.lang.Void>
     */
    R<Void> sendOrderEmailStrategy(int type, OrderDTO orderDTO);
}

package com.sea.order.factory;
import com.google.common.collect.Maps;
import com.sea.order.annotation.OrderEmailTypeAnnotation;
import com.sea.order.strategy.OrderEmailTypeStrategyInterface;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @description: 发送订单邮件类型策略工厂
 * @author: Sea
 * @time: 2020/06/01 0002 04:26
 */
@Component
public class OrderEmailStrategyFactory {
   
    private static final Map<String, OrderEmailTypeStrategyInterface> STRATEGY_BEAN_CACHE = Maps.newConcurrentMap();

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * @description: 订单邮件类型策略
     * @param: type
     * @return: OrderEmailTypeStrategyInterface
     * @author: Sea
     * @time: 2020/06/01 0002 04:26
     */
    public OrderEmailTypeStrategyInterface orderEmailStrategy(int type) {
   
        Optional<OrderEmailTypeStrategyInterface> strategyOptional = STRATEGY_BEAN_CACHE.entrySet()
                .stream()
                .map(e->{
   
                    OrderEmailTypeAnnotation validType = e.getValue().getClass().getDeclaredAnnotation(OrderEmailTypeAnnotation.class);
                    if (Objects.equals(validType.type(
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值