Spring task定时任务

       第一次写定时任务,短信通知定时,虽然写完了,但是感觉代码不够简洁,性能不行害怕跑蹦了,放到这里一是为了记录,更多的是希望大家提出宝贵的意见。。。。。

 

1.将用户Id拼接成字符串,用来批量查询用户信息。

String memberId = memberTransactions.stream().map(MemberTransaction::getMemberId).map(String::valueOf).collect(Collectors.joining(","));

2.这段代码是为了根据不同的商户将用户数据分组。

  logger.debug("用户信息-------->{}", "memberInfo=" + JSONObject.toJSONString(memberInfo));
        Map<String, List<MemberData>> resultMap = new HashMap<>();
        for (MemberData memberData : memberInfo) {
            if (resultMap.containsKey(memberData.getUuid())) {//map中异常批次已存在,将该数据存放到同一个key(key存放的是异常批次)的map中
                resultMap.get(memberData.getUuid()).add(memberData);
            } else {//map中不存在,新建key,用来存放数据
                List<MemberData> tmpList = new ArrayList<>();
                tmpList.add(memberData);
                resultMap.put(memberData.getUuid(), tmpList);
            }
        }

3.为了动态获取定时的时间,将注解写到配置文件中,再动态获取。



@Component
@PropertySource("classpath:TimerConditionTask.properties")
public class SmsTask {

@Scheduled(cron= "${smsRepayTimeToDay.schedule}")
public void repayTimeToDay() {    
    }


@Scheduled(cron = "${smsRepayTimeYesterday.schedule}")
public void repayTimeYesterday() {

}


}

4定时从从数据库中查出执行时间,拼接成注解格式,写到配置文件中

 //属性文件的路径
    static String profilePath="src/main/resources/TimerConditionTask.properties";

    /**
     * 采用静态方法
     */
    private static Properties props = new Properties();

    static {
        try {
            FileInputStream fileInputStream = new FileInputStream(profilePath);
            props.load(fileInputStream);//先加载配置文件
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 


//每天一点定时查数据库,获取执行时间
 @Scheduled(cron = "0 0 1 * * ?")
    public void getTimerCondition() {
        logger.debug("获取触发条件------>开始");
        String[] ss = {"4", "5"};
        TreeMap<String, String> map = new TreeMap<>();
        for (int i=0;i<2;i++) {
             if ("4".equals(shortType)&&i==0) {
                    String key = "smsRepayTimeTomorrow.schedule";
                    String value = formatTime(data);
                    map.put(key, value);
                 }else{
                    String key = "smsRepayTimeToDay.schedule";
                    String value = formatTime(data);
                    map.put(key, value);
             }

    writeProperties(map);//将处理好的map写到配置文件中
            logger.debug("获取触发条件----->结束");
        }
    }


//处理时间格式
    private  String formatTime(String data){
        StringBuffer stringBuffer = new StringBuffer();
        String one=null;
        String two=null;
        String three=null;
        if( "00".equals(data.split(":")[0])){
            three="0";
        }else{
           three=data.split(":")[0];
        }
        if( "00".equals(data.split(":")[1])){
            two="0";
        }else{
            two=data.split(":")[1];
        }
        if( "00".equals(data.split(":")[2])){
            one="0";
        }else{
            one=data.split(":")[2];
        }
        String value=one+" "+two+" "+three+" "+"* * ?";
        return value;
    }



 /**
     * 更新(或插入)一对properties信息(主键及其键值)
     * 如果该主键已经存在,更新该主键的值;
     * 如果该主键不存在,则插件一对键值。
     */
    public static void writeProperties(Map<String, String> map) {
        try {
            // 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
            // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
            OutputStream fos = new FileOutputStream(profilePath);

            for (String keyname : map.keySet()) {
                props.setProperty(keyname, map.get(keyname));
            }
            // 以适合使用 load 方法加载到 Properties 表中的格式,
            // 将此 Properties 表中的属性列表(键和元素对)写入输出流
            props.store(fos, "");
            fos.close();
        } catch (IOException e) {
            System.err.println("属性文件更新错误");
        }
    }

5.生成的配置文件

#
#Sat Dec 22 16:22:00 CST 2018
smsRepayTimeYesterday.schedule=0 0 12 * * ?
smsRepayTimeTomorrow.schedule=0 0 12 * * ?
smsRepayTimeToDay.schedule=0 0 12 * * ?

业务代码不是重点,如果有错误,请谅解(抱拳)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值