Merge branch 'master' of D:\ideaProject\ad-platform with conflicts.
parent
f367d6866f
commit
1271aeb0e9
@ -0,0 +1,37 @@
|
|||||||
|
package com.baiye.model.entity;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.EntityListeners;
|
||||||
|
import javax.persistence.MappedSuperclass;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujingtao
|
||||||
|
* @date 2022/01/04
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@MappedSuperclass
|
||||||
|
@EntityListeners(AuditingEntityListener.class)
|
||||||
|
public class BaseTimeTask {
|
||||||
|
|
||||||
|
|
||||||
|
@Column(name = "execute_time")
|
||||||
|
private Date executeTime;
|
||||||
|
|
||||||
|
@Column(name = "is_repeat")
|
||||||
|
private Integer isRepeat;
|
||||||
|
|
||||||
|
@Column(name = "status")
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
|
@Column(name = "message")
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
@Column(name = "user_id")
|
||||||
|
private Long userId;
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,23 @@
|
|||||||
|
package com.baiye.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujingtao
|
||||||
|
* @date 2022/01/04
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableAsync
|
||||||
|
public class ThreadPoolConfig {
|
||||||
|
|
||||||
|
@Bean(value = "taskExecutor")
|
||||||
|
public Executor taskExecutor() {
|
||||||
|
return new ThreadPoolExecutor(
|
||||||
|
2, 16, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>(3),
|
||||||
|
Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baiye.job;
|
||||||
|
|
||||||
|
import com.baiye.modules.elsaticjob.entity.jobInstance.ElasticSimpleJob;
|
||||||
|
import com.baiye.modules.elsaticjob.service.impl.AutoReminderServiceImpl;
|
||||||
|
import com.dangdang.ddframe.job.api.ShardingContext;
|
||||||
|
import com.dangdang.ddframe.job.api.simple.SimpleJob;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujingtao
|
||||||
|
* @date 2022/01/05
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@ElasticSimpleJob(jobName = "AutomaticReminderJob", cron = "0 0 0/1 * * ?")
|
||||||
|
public class AutomaticReminderJob implements SimpleJob {
|
||||||
|
@Resource
|
||||||
|
private AutoReminderServiceImpl timeTaskServiceImpl;
|
||||||
|
private static AutomaticReminderJob automaticReminderJob;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
automaticReminderJob = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(ShardingContext shardingContext) {
|
||||||
|
automaticReminderJob.timeTaskServiceImpl.runAutomaticReminder();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baiye.modules.elsaticjob.api;
|
||||||
|
|
||||||
|
import com.baiye.http.CommonResponse;
|
||||||
|
import com.baiye.modules.elsaticjob.entity.AutoReminder;
|
||||||
|
import com.baiye.modules.elsaticjob.service.AutoReminderService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujingtao
|
||||||
|
* @date 2022/01/05
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/time")
|
||||||
|
@Api(tags = "操作定时任务的条件")
|
||||||
|
public class AutoReminderController {
|
||||||
|
@Resource
|
||||||
|
private AutoReminderService timeTaskService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册新任务
|
||||||
|
*
|
||||||
|
* @param timeTask
|
||||||
|
*/
|
||||||
|
@PostMapping("/add")
|
||||||
|
@ApiOperation("新增定时任务")
|
||||||
|
public CommonResponse<Object> addTimeTask(@RequestBody AutoReminder timeTask) {
|
||||||
|
|
||||||
|
return timeTaskService.addTimeTask(timeTask);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baiye.modules.elsaticjob.dao;
|
||||||
|
|
||||||
|
import com.baiye.modules.elsaticjob.entity.AutoReminder;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujingtao
|
||||||
|
* @date 2022/01/05
|
||||||
|
*/
|
||||||
|
@Repository
|
||||||
|
public interface AutoReminderRepository extends JpaRepository<AutoReminder, Long>, JpaSpecificationExecutor<AutoReminder> {
|
||||||
|
/**
|
||||||
|
* 查询所有未执行的任务
|
||||||
|
*
|
||||||
|
* @param status
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<AutoReminder> findAllByStatus(Integer status);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改只提醒一次的任务状态
|
||||||
|
*
|
||||||
|
* @param status
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
@Modifying
|
||||||
|
@Query(value = "update AutoReminder set status =?1 where id =?2")
|
||||||
|
void updateTimeTaskStatus(Integer status, Long id);
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baiye.modules.elsaticjob.entity;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DatePattern;
|
||||||
|
import com.baiye.model.entity.BaseTimeTask;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.data.annotation.LastModifiedDate;
|
||||||
|
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujingtao
|
||||||
|
* @date 2022/01/05
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Entity
|
||||||
|
@Table(name = "tb_auto_reminder")
|
||||||
|
@EntityListeners(AuditingEntityListener.class)
|
||||||
|
public class AutoReminder extends BaseTimeTask implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1343708488273542805L;
|
||||||
|
@Id
|
||||||
|
@Column(name = "id")
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@LastModifiedDate
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_DATETIME_PATTERN, timezone = "GMT+8")
|
||||||
|
@Column(name = "create_time")
|
||||||
|
private Date createTime;
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baiye.modules.elsaticjob.service;
|
||||||
|
|
||||||
|
import com.baiye.http.CommonResponse;
|
||||||
|
import com.baiye.modules.elsaticjob.entity.AutoReminder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujingtao
|
||||||
|
* @date 2022/01/05
|
||||||
|
*/
|
||||||
|
public interface AutoReminderService {
|
||||||
|
/**
|
||||||
|
* 新增定时任务
|
||||||
|
*
|
||||||
|
* @param timeTask
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
CommonResponse<Object> addTimeTask(AutoReminder timeTask);
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baiye.modules.elsaticjob.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.date.DateUnit;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.baiye.constant.DefaultNumberConstants;
|
||||||
|
import com.baiye.http.CommonResponse;
|
||||||
|
import com.baiye.modules.elsaticjob.dao.AutoReminderRepository;
|
||||||
|
import com.baiye.modules.elsaticjob.entity.AutoReminder;
|
||||||
|
import com.baiye.modules.elsaticjob.service.AutoReminderService;
|
||||||
|
import com.baiye.socket.WebSocketServer;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujingtao
|
||||||
|
* @date 2022/01/05
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class AutoReminderServiceImpl implements AutoReminderService {
|
||||||
|
@Resource
|
||||||
|
private AutoReminderRepository timeTaskRepository;
|
||||||
|
@Resource
|
||||||
|
private WebSocketServer webSocketServer;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommonResponse<Object> addTimeTask(AutoReminder timeTask) {
|
||||||
|
if (timeTaskRepository.save(timeTask).getId() == null) {
|
||||||
|
return CommonResponse.createByErrorMessage("保存定时任务失败");
|
||||||
|
}
|
||||||
|
return CommonResponse.createBySuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行自体醒任务
|
||||||
|
*/
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void runAutomaticReminder() {
|
||||||
|
List<AutoReminder> allByStatus = timeTaskRepository.findAllByStatus(DefaultNumberConstants.ZERO_NUMBER);
|
||||||
|
if (CollUtil.isEmpty(allByStatus)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JSONObject object = new JSONObject();
|
||||||
|
object.putOpt("type", "");
|
||||||
|
object.putOpt("code", DefaultNumberConstants.TWO_HUNDRED);
|
||||||
|
for (AutoReminder info : allByStatus) {
|
||||||
|
if (DateUtil.between(info.getExecuteTime(), DateUtil.date(), DateUnit.HOUR) == 0) {
|
||||||
|
object.putOpt("message", info.getUserId());
|
||||||
|
try {
|
||||||
|
webSocketServer.sendMessage(JSONUtil.toJsonStr(object), String.valueOf(info.getUserId()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("执行自提醒任务 id:{} 发送websocket失败 {}", info.getId(), e.getMessage());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (info.getIsRepeat() == DefaultNumberConstants.ZERO_NUMBER) {
|
||||||
|
timeTaskRepository.updateTimeTaskStatus(DefaultNumberConstants.ONE_NUMBER, info.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue