定时任务消息 长时间未发送的进行清理

master
wujingtao 3 years ago
parent 8ba6d06eb5
commit 76ed8721a7

@ -20,7 +20,7 @@ import javax.annotation.Resource;
*/ */
@Slf4j @Slf4j
@ElasticSimpleJob( @ElasticSimpleJob(
jobName = "ConfirmReceiptJob", cron = "0 0/1 * * * ?", shardingTotalCount = 1, overwrite = true, jobEvent = false) jobName = "ConfirmReceiptJob", cron = "0 0/30 * * * ?", shardingTotalCount = 1, overwrite = true, jobEvent = false)
@Component @Component
public class ConfirmReceiptJob implements SimpleJob { public class ConfirmReceiptJob implements SimpleJob {

@ -0,0 +1,36 @@
package com.hchbox.job;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.example.autoconfig.ElasticSimpleJob;
import com.hchbox.module.service.TimerTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* @author wjt
* @date 2021/11/26
*/
@Slf4j
@ElasticSimpleJob(
jobName = "DeleteExpiredMessageJob", cron = "0 0 0/1 * * ?", shardingTotalCount = 1, overwrite = true, jobEvent = false)
@Component
public class DeleteExpiredMessageJob implements SimpleJob {
@Resource
private TimerTaskService timerTaskService;
private static DeleteExpiredMessageJob deleteExpiredMessageJob;
@PostConstruct
public void init() {
deleteExpiredMessageJob = this;
}
@Override
public void execute(ShardingContext shardingContext) {
deleteExpiredMessageJob.timerTaskService.deleteNotSendMessage();
}
}

@ -16,26 +16,26 @@ import javax.annotation.Resource;
* @date 2021/10/15 * @date 2021/10/15
* *
*/ */
@Slf4j //@Slf4j
@ElasticSimpleJob( //@ElasticSimpleJob(
//0 0 5 1 1/1 ? * -------每月1日5点 // //0 0 5 1 1/1 ? * -------每月1日5点
jobName = "ObtainInfoExportDelJob", cron = "0 0 5 1 1/1 ? *", shardingTotalCount = 1, overwrite = true, jobEvent = false) // jobName = "ObtainInfoExportDelJob", cron = "0 0 5 1 1/1 ? *", shardingTotalCount = 1, overwrite = true, jobEvent = false)
@Component //@Component
public class ObtainInfoExportDelJob implements SimpleJob { //public class ObtainInfoExportDelJob implements SimpleJob {
//
@Resource // @Resource
private ObtainInfoService obtainInfoService; // private ObtainInfoService obtainInfoService;
//
private static ObtainInfoExportDelJob obtainInfoExportDelJob; // private static ObtainInfoExportDelJob obtainInfoExportDelJob;
//
@PostConstruct // @PostConstruct
public void init() { // public void init() {
obtainInfoExportDelJob = this; // obtainInfoExportDelJob = this;
} // }
//
@Override // @Override
public void execute(ShardingContext shardingContext) { // public void execute(ShardingContext shardingContext) {
log.info("===============ObtainInfoExportDelJob备份数据到本地开始,{}================", DateUtil.date()); // log.info("===============ObtainInfoExportDelJob备份数据到本地开始,{}================", DateUtil.date());
// obtainInfoExportDelJob.obtainInfoService.selectExportDel(); // obtainInfoExportDelJob.obtainInfoService.selectExportDel();
} // }
} //}

@ -15,26 +15,26 @@ import javax.annotation.Resource;
* @date 2021/10/14 * @date 2021/10/14
* *
*/ */
@Slf4j //@Slf4j
@ElasticSimpleJob( //@ElasticSimpleJob(
//每天一点执行0 0 1 * * ? // //每天一点执行0 0 1 * * ?
jobName = "ObtainInfoJob", cron = "0 0 1 * * ?", shardingTotalCount = 1, overwrite = true, jobEvent = false) // jobName = "ObtainInfoJob", cron = "0 0 1 * * ?", shardingTotalCount = 1, overwrite = true, jobEvent = false)
@Component //@Component
public class ObtainInfoJob implements SimpleJob { //public class ObtainInfoJob implements SimpleJob {
//
@Resource // @Resource
private ObtainInfoService obtainInfoService; // private ObtainInfoService obtainInfoService;
//
private static ObtainInfoJob obtainInfoJob; // private static ObtainInfoJob obtainInfoJob;
//
@PostConstruct // @PostConstruct
public void init() { // public void init() {
obtainInfoJob = this; // obtainInfoJob = this;
} // }
//
@Override // @Override
public void execute(ShardingContext shardingContext) { // public void execute(ShardingContext shardingContext) {
log.info("===============ObtainInfoJob同步前一天数据开始,{}================", DateUtil.date()); // log.info("===============ObtainInfoJob同步前一天数据开始,{}================", DateUtil.date());
// obtainInfoJob.obtainInfoService.selectAllSave(); // obtainInfoJob.obtainInfoService.selectAllSave();
} // }
} //}

@ -1,44 +0,0 @@
package com.hchbox.module.controller;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* @author Enzo
* @date : 2021/6/17
*/
@Slf4j
@RestController
@RequestMapping("/wwsend")
public class TestController {
@GetMapping("/test")
public String test(HttpServletRequest request) {
log.info("请求路径:" + request.getRequestURL().toString());
log.info("请求ip" + request.getRemoteAddr());
Enumeration pNames = request.getParameterNames();
Map<String, Object> map = new HashMap<String, Object>(16);
while (pNames.hasMoreElements()) {
String key = pNames.nextElement().toString();
String result = request.getParameter(key);
map.put(key, result);
}
log.info("请求参数:" + new Gson().toJson(map));
return request.getParameter("TAO_MI_TOKEN");
}
@GetMapping("/helloWorld")
public String test(){
return "Hello World Wwsend";
}
}

@ -67,6 +67,7 @@ public class WwRemindController {
long parentId = sellerAccount.getParentId(); long parentId = sellerAccount.getParentId();
sellerNick = sellerAccountService.findSellerAccountById(parentId).getNickname(); sellerNick = sellerAccountService.findSellerAccountById(parentId).getNickname();
} }
log.info("===========sellerNick:{} 查询消息======", sellerNick);
return new CommonResult<>().success(delayRemindService.selectDelayRemind(sellerNick)); return new CommonResult<>().success(delayRemindService.selectDelayRemind(sellerNick));
} }

@ -1,8 +1,10 @@
package com.hchbox.module.dao; package com.hchbox.module.dao;
import cn.hutool.core.date.DateTime;
import com.hchbox.module.entity.AutoWaitMessage; import com.hchbox.module.entity.AutoWaitMessage;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
@ -20,4 +22,13 @@ public interface AutoWaitForMessageRepository extends JpaRepository<AutoWaitMess
* @return * @return
*/ */
List<AutoWaitMessage> findAllBySender(String sellerNick); List<AutoWaitMessage> findAllBySender(String sellerNick);
/**
*
*
* @param creatTime
* @return
*/
@Query(value = "select a.id from AutoWaitMessage as a where a.createTime < ?1")
List<Long> findIdByCreatTime(DateTime creatTime);
} }

@ -2,9 +2,12 @@ package com.hchbox.module.entity;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*; import javax.persistence.*;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date;
/** /**
* @author wujingtao * @author wujingtao
@ -13,10 +16,13 @@ import java.io.Serializable;
@Data @Data
@Entity @Entity
@Table(name = "auto_wait_for_message") @Table(name = "auto_wait_for_message")
@EntityListeners(AuditingEntityListener.class)
public class AutoWaitMessage implements Serializable { public class AutoWaitMessage implements Serializable {
@Id @Id
@Column(name = "task_id") @Column(name = "id")
@ApiModelProperty(value = "id") @ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "taskId")
private Long taskId; private Long taskId;
@ApiModelProperty("发送人") @ApiModelProperty("发送人")
private String sender; private String sender;
@ -26,4 +32,8 @@ public class AutoWaitMessage implements Serializable {
private String sendMessage; private String sendMessage;
@ApiModelProperty("接收人") @ApiModelProperty("接收人")
private String buyer; private String buyer;
@CreationTimestamp
@ApiModelProperty("创建时间")
@Column(name = "create_time")
private Date createTime;
} }

@ -12,5 +12,10 @@ public interface TimerTaskService {
* @param settingType * @param settingType
* @param tradeStatus * @param tradeStatus
*/ */
void sendMessage(Integer settingType,String tradeStatus); void sendMessage(Integer settingType, String tradeStatus);
/**
*
*/
void deleteNotSendMessage();
} }

@ -20,6 +20,7 @@ import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
@ -116,9 +117,10 @@ public class TimerTaskServiceImpl implements TimerTaskService {
return; return;
} }
//创建发送信息 //创建发送信息
createAutoTaskMessageInfo(templateStr, as, subNickList, info, autoTaskMessage.getId()); long id = createAutoTaskMessageInfo(templateStr, as, subNickList, info, autoTaskMessage.getId());
//创建待发送列表 //创建待发送列表
AutoWaitMessage autoWaitForMessage = new AutoWaitMessage(); AutoWaitMessage autoWaitForMessage = new AutoWaitMessage();
autoWaitForMessage.setId(id);
autoWaitForMessage.setTaskId(autoTaskMessage.getId()); autoWaitForMessage.setTaskId(autoTaskMessage.getId());
autoWaitForMessage.setTaskName(autoTaskMessage.getTaskName()); autoWaitForMessage.setTaskName(autoTaskMessage.getTaskName());
autoWaitForMessage.setSender(info.getSellerNick()); autoWaitForMessage.setSender(info.getSellerNick());
@ -150,7 +152,7 @@ public class TimerTaskServiceImpl implements TimerTaskService {
} }
} }
private void createAutoTaskMessageInfo(String templateStr, AppSetting appSetting, List<String> subNickList, TopTrade trade, Long taskId) { private Long createAutoTaskMessageInfo(String templateStr, AppSetting appSetting, List<String> subNickList, TopTrade trade, Long taskId) {
AutoTaskMessageInfo autoTaskMessageInfo = new AutoTaskMessageInfo(); AutoTaskMessageInfo autoTaskMessageInfo = new AutoTaskMessageInfo();
autoTaskMessageInfo.setTaskId(taskId); autoTaskMessageInfo.setTaskId(taskId);
autoTaskMessageInfo.setMessageNick(subNickList.get(DefaultNumberConstants.ZERO_NUMBER)); autoTaskMessageInfo.setMessageNick(subNickList.get(DefaultNumberConstants.ZERO_NUMBER));
@ -163,7 +165,7 @@ public class TimerTaskServiceImpl implements TimerTaskService {
String content = RemindTemplates.prepareMsg(templateStr, trade); String content = RemindTemplates.prepareMsg(templateStr, trade);
// 模板替换 // 模板替换
autoTaskMessageInfo.setInfo(content); autoTaskMessageInfo.setInfo(content);
autoTaskMessageInfoJpa.save(autoTaskMessageInfo); return autoTaskMessageInfoJpa.save(autoTaskMessageInfo).getId();
} }
private AutoTaskMessage createAutoTaskMessage(AppSetting setting, String template) { private AutoTaskMessage createAutoTaskMessage(AppSetting setting, String template) {
@ -199,4 +201,19 @@ public class TimerTaskServiceImpl implements TimerTaskService {
} }
return list; return list;
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteNotSendMessage() {
//超过三小时未发送的消息 就被清除
DateTime newDate3 = DateUtil.offsetHour(DateUtil.date(), -3);
List<Long> idList = autoWaitForMessageRepository.findIdByCreatTime(newDate3);
if (CollUtil.isEmpty(idList)) {
return;
}
for (Long id : idList) {
autoWaitForMessageRepository.deleteById(id);
autoTaskMessageInfoJpa.deleteById(id);
}
}
} }

Loading…
Cancel
Save