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") + ""; }