提交 446a6be4 编写于 作者: IIIllI's avatar IIIllI

定时工具类,可传参任务

上级 e2d200f3
......@@ -15,4 +15,6 @@ public interface ParticipantDao {
public List<Award> getAward(Long id);
public int addParticipants(Long userId,Long activityId);
public List<User> getAllParticipants();
}
......@@ -11,4 +11,7 @@ public interface ParticipantService {
//查询中奖名单 活动id
public List<Award> selectAwardList(Long id);
//参与抽奖
public void participateDraw(Long userId,Long activityId);
}
......@@ -23,4 +23,10 @@ public class ParticipantServiceImp implements ParticipantService {
public List<Award> selectAwardList(Long id) {
return participantDao.getAward(id);
}
@Override
public void participateDraw(Long userId, Long activityId) {
participantDao.addParticipants(userId,userId);
}
}
package com.nav.pojo;
package com.nav.utils;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.annotation.Scope;
import org.quartz.JobKey;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.Date;
public class Draw extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("简单的定时任务执行时间:"+new Date());
JobKey jobKey = context.getJobDetail().getKey();
JobDataMap jobDataMap = context.getMergedJobDataMap();
System.out.println("id: "+jobDataMap.getLong("id")+"jobKey: "+jobKey);
}
}
package com.nav.controller;
package com.nav.utils;
import com.nav.pojo.Draw;
import com.nav.utils.Draw;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
@RestController
public class QuartzController {
@Autowired
SchedulerFactoryBean schedulerFactoryBean;
public class Quartz {
@GetMapping("/add")
public void init() throws SchedulerException, ParseException {
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=format.parse("2022-04-23 17:00:45");
//public static SchedulerFactoryBean schedulerFactoryBean;
public static void autoDraw(SchedulerFactoryBean schedulerFactoryBean,Date date, Long activityID) throws SchedulerException, ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date test = format.parse("2022-04-23 19:45:00");
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(Draw.class)
.withIdentity(createRandomStr(10))
.usingJobData("id", activityID)
.storeDurably()
.build();
Trigger trigger=TriggerBuilder.newTrigger()
Trigger trigger = TriggerBuilder.newTrigger()
.forJob(jobDetail)//关联上述的JobDetail
.withIdentity(createRandomStr(10))//给Trigger起个名字
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0))// 重复次数 执行一次不重复
......@@ -39,7 +36,7 @@ public class QuartzController {
scheduler.scheduleJob(jobDetail, trigger);
}
public static String createRandomStr(int length){
public static String createRandomStr(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer stringBuffer = new StringBuffer();
......
......@@ -5,8 +5,8 @@
<mapper namespace="com.nav.dao.ParticipantDao">
<select id="getParticipants" parameterType="long" resultType="com.nav.pojo.User">
select *
from participant
where activity_id = #{id};
from user
where id in (select user_id from participant where activity_id = #{1});
</select>
<select id="getAward" parameterType="long" resultType="com.nav.pojo.Award">
......@@ -17,6 +17,12 @@
and participant.user_id = `user`.id
</select>
<select id="getAllParticipants" resultType="com.nav.pojo.User">
select *
from user
where id in (select user_id from participant);
</select>
<insert id="addParticipants" parameterType="long">
insert into participant (user_id, activity_id, state)
values (#{userId}, #{activityID}, 0)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册