JAVA定时功能

package com.qycloud.modules.issuevote.service;

import com.alibaba.fastjson.JSONObject;
import com.qycloud.common.constant.BusiConstant;
import com.qycloud.common.util.DateUtils;
import com.qycloud.modules.onlineredchamber.entity.RedIssueRelease;
import com.qycloud.modules.onlineredchamber.mapper.RedIssueReleaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Component
@Service
@EnableScheduling
public class IssueVoteAndTaskService {

    @Autowired
    private RedIssueReleaseMapper redIssueReleaseMapper;
    //Cron表达式范例:
    //每隔5秒执行一次:*/5 * * * * ?
    //每隔1分钟执行一次:0 */1 * * * ?
    //每天23点执行一次:0 0 23 * * ?
    //每天凌晨1点执行一次:0 0 1 * * ?
    //每月1号凌晨1点执行一次:0 0 1 1 * ?
    //每月最后一天23点执行一次:0 0 23 L * ?
    //每周星期天凌晨1点实行一次:0 0 1 ? * L

    // 表示方法执行完成后5秒再执行
    //@Scheduled(fixedDelay = 6000)
    @Scheduled(cron ="0 */1 * * * ?")
    public void TestTask() throws Exception {

        //查询条件
        Map<String, Object> params = new HashMap<>();
        params.put("voteState", BusiConstant.ISSUE_VOTE_STATUS_VOTEING);
        List<RedIssueRelease> list = redIssueReleaseMapper.pageRedIssueReleaseList(params);
        List<RedIssueRelease> stopList = new ArrayList<>();
        String date = DateUtils.formatDate(DateUtils.getDate(),"YYYY-MM-dd");//获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (list != null && list.size() > 0) {
            for (RedIssueRelease item : list) {
               //结束日期等于当前日期 或 结束日期在当前日期之前
                if (sdf.parse(item.getVoteEndDate()).equals(sdf.parse(date)) ||                            sdf.parse(item.getVoteEndDate()).before(sdf.parse(date))) {
                    item.setState(BusiConstant.ISSUE_STATUS_NOFEEDBACK);
                    item.setVoteState(BusiConstant.ISSUE_VOTE_STATUS_END);
                    item.setLastTime(DateUtils.getDate());
                    stopList.add(item);
                }
            }
        }

         if (stopList != null && stopList.size() > 0) {
            redIssueReleaseMapper.updateRedIssueReleaseListById(stopList);//如果等于当前时间更新状态
        }
    }



    public String getTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String nowDate = format.format(d);//获取当前时间
        //System.out.println("获取当前时间= "+ nowDate);
        return nowDate;
    }


    public JSONObject getDistanceTime(String firstLoginTime, String nowTime) {
        JSONObject dataMap = new JSONObject();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1;

        try {
            d1 = df.parse(nowTime);
            Date d2 = df.parse(firstLoginTime);// 用户初次登录时间
            long diff = d1.getTime() - d2.getTime();
            //当前的时间减去我初次登陆的时间如果大于等于2小时

            dataMap.put("sss", diff);


            return dataMap;
        } catch (ParseException e) {

        }
        return dataMap;

    }
}

//单体启动类(采用此类启动为单体模式)

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
//@PropertySource(value = "classpath:config/aliyunapi.properties",encoding = "utf-8")
@ComponentScan(basePackages = {"com.*","org.jeecg.modules.jmreport.*"})
public class QycloudSingleApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(QycloudSingleApplication.class);
    }

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(QycloudSingleApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path") + "";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值