java 实现策略设计模式


@Component
public class DpReportServiceFactory {

    @Autowired
    private DpReportDataServiceGovImpl dpReportDataServiceGov;

    /**
     * 根据requestType判断类型,实例化并返回对应对象
     *
     * @param requestType
     * @return
     */
    public DpReportDataService createService(String requestType) {
        DpReportDataService service = null;
        if ("gov".equals(requestType)) {
            service = dpReportDataServiceGov;
        }  
        return service;
    }

}
/**
 * 为定时任务提供上报接口
 */
public interface DpReportJobService {

    Boolean reportData(ReportConfigDataDTO reportConfigDataDTO);
}
@Slf4j
@RequiredArgsConstructor
@Service
public class DpReportJobServiceImpl implements DpReportJobService {

    @Resource
    private DpReportServiceFactory dpReportServiceFactory;

    @Override
    public Boolean reportData(ReportConfigDataDTO reportConfigDataDTO) {
        DpReportDataService dpReportService = dpReportServiceFactory.createService(reportConfigDataDTO.getReportType());
        // 上报数据
        dpReportService.reportData(reportConfigDataDTO);
        return true;
    }
}
public interface DpReportDataService {

    Boolean reportData(ReportConfigDataDTO reportConfigDataDTO);
}
@Service
@Slf4j
public class DpReportDataServiceGovImpl implements DpReportDataService { 
   @Override
    public Boolean reportData(ReportConfigDataDTO dto) { 
		 return true;
	}
}
@Component
@Slf4j
public class ScheduledTaskDpReport {

    @Resource
    private DpReportJobService dpReportJobService;

    @Resource
    private ReportConfigDataList reportConfigList;

    /**
     * 每天凌晨1点执行一次定时任务,推送前一天的数据
     */
    @Scheduled(cron = "0 0 1 * * ? ")
    // @Scheduled(cron = "0 0/5 * * * ? ")
    public ResultBean<Boolean> reportData() {
        for (ReportConfigDataDTO dto : reportConfigList.getReportConfigData()) {
             log.info("定时任务上报数据:{}", JSONUtil.toJsonStr(dto));
            dpReportJobService.reportData(dto);
        }
        log.info("定时任务上报数据结束");
        return ResultBean.ok(true);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值