作者 王勇

添加自主定时删除消息收发记录的任务

@@ -5,6 +5,7 @@ import org.springframework.boot.SpringApplication; @@ -5,6 +5,7 @@ import org.springframework.boot.SpringApplication;
5 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
7 import org.springframework.cloud.openfeign.EnableFeignClients; 7 import org.springframework.cloud.openfeign.EnableFeignClients;
  8 +import org.springframework.scheduling.annotation.EnableScheduling;
8 import org.springframework.transaction.annotation.EnableTransactionManagement; 9 import org.springframework.transaction.annotation.EnableTransactionManagement;
9 10
10 /** 11 /**
@@ -15,6 +16,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @@ -15,6 +16,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
15 @EnableFeignClients 16 @EnableFeignClients
16 @EnableEurekaClient 17 @EnableEurekaClient
17 @EnableTransactionManagement 18 @EnableTransactionManagement
  19 +@EnableScheduling
18 public class MessageBusServiceApplication { 20 public class MessageBusServiceApplication {
19 21
20 public static void main(String[] args) { 22 public static void main(String[] args) {
@@ -3,10 +3,13 @@ package com.sunyo.wlpt.message.bus.service.controller; @@ -3,10 +3,13 @@ package com.sunyo.wlpt.message.bus.service.controller;
3 import com.fasterxml.jackson.annotation.JsonFormat; 3 import com.fasterxml.jackson.annotation.JsonFormat;
4 import com.github.pagehelper.PageInfo; 4 import com.github.pagehelper.PageInfo;
5 import com.sunyo.wlpt.message.bus.service.domain.MessageNote; 5 import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
  6 +import com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete;
6 import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding; 7 import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding;
7 import com.sunyo.wlpt.message.bus.service.response.ResultJson; 8 import com.sunyo.wlpt.message.bus.service.response.ResultJson;
8 import com.sunyo.wlpt.message.bus.service.service.MessageNoteService; 9 import com.sunyo.wlpt.message.bus.service.service.MessageNoteService;
  10 +import com.sunyo.wlpt.message.bus.service.service.SchedulingDeleteService;
9 import com.sunyo.wlpt.message.bus.service.utils.IdUtils; 11 import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
  12 +import org.springframework.scheduling.annotation.Scheduled;
10 import org.springframework.web.bind.annotation.*; 13 import org.springframework.web.bind.annotation.*;
11 14
12 import javax.annotation.Resource; 15 import javax.annotation.Resource;
@@ -25,6 +28,9 @@ public class MessageNoteController { @@ -25,6 +28,9 @@ public class MessageNoteController {
25 @Resource 28 @Resource
26 private MessageNoteService messageNoteService; 29 private MessageNoteService messageNoteService;
27 30
  31 + @Resource
  32 + private SchedulingDeleteService schedulingDeleteService;
  33 +
28 /** 34 /**
29 * 分页查询,消息收发记录 35 * 分页查询,消息收发记录
30 * 36 *
@@ -167,4 +173,20 @@ public class MessageNoteController { @@ -167,4 +173,20 @@ public class MessageNoteController {
167 } 173 }
168 return result; 174 return result;
169 } 175 }
  176 +
  177 + /**
  178 + * 每日凌晨4点
  179 + * 自动删除 (默认时间)7天前的记录
  180 + */
  181 + @Scheduled(cron = "0 0 4 * * ? ")
  182 + @GetMapping("/delete")
  183 + public void autoDeleteMessageNote() {
  184 + // 类型
  185 + String deleteType = "message_note";
  186 + // 首先获取可自主设置的默认时间
  187 + SchedulingDelete schedulingDelete = schedulingDeleteService.selectByType(deleteType);
  188 + Integer deleteTime = schedulingDelete.getDeleteTime();
  189 + // 定时自动删除
  190 + messageNoteService.autoDelete(deleteTime);
  191 + }
170 } 192 }
  1 +package com.sunyo.wlpt.message.bus.service.domain;
  2 +
  3 +import java.io.Serializable;
  4 +import lombok.AllArgsConstructor;
  5 +import lombok.Data;
  6 +import lombok.NoArgsConstructor;
  7 +
  8 +/**
  9 + * @author 子诚
  10 + * Description:自动定时删除的时间设置表
  11 + * 时间:2020/7/14 10:35
  12 + */
  13 +@Data
  14 +@AllArgsConstructor
  15 +@NoArgsConstructor
  16 +public class SchedulingDelete implements Serializable {
  17 +
  18 + private static final long serialVersionUID = -3199487735394538126L;
  19 +
  20 + /**
  21 + * 时间设置表的ID
  22 + */
  23 + private String id;
  24 +
  25 + /**
  26 + * 设置时间
  27 + */
  28 + private Integer deleteTime;
  29 +
  30 + /**
  31 + * 类型
  32 + */
  33 + private String deleteType;
  34 +}
1 package com.sunyo.wlpt.message.bus.service.mapper; 1 package com.sunyo.wlpt.message.bus.service.mapper;
2 2
3 import com.sunyo.wlpt.message.bus.service.domain.MessageNote; 3 import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
4 -import org.apache.ibatis.annotations.Mapper;import java.util.List; 4 +import org.apache.ibatis.annotations.Mapper;
  5 +import org.apache.ibatis.annotations.Param;
  6 +
  7 +import java.util.List;
5 8
6 /** 9 /**
7 * @author 子诚 10 * @author 子诚
@@ -65,4 +68,11 @@ public interface MessageNoteMapper { @@ -65,4 +68,11 @@ public interface MessageNoteMapper {
65 * @return List<MessageNote> 68 * @return List<MessageNote>
66 */ 69 */
67 List<MessageNote> selectMessageNoteList(MessageNote messageNote); 70 List<MessageNote> selectMessageNoteList(MessageNote messageNote);
  71 +
  72 + /**
  73 + * 自动删除消息收发记录
  74 + *
  75 + * @param deleteTime 删除的时间
  76 + */
  77 + void autoDelete(@Param("deleteTime") Integer deleteTime);
68 } 78 }
  1 +package com.sunyo.wlpt.message.bus.service.mapper;
  2 +
  3 +import com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete;
  4 +import org.apache.ibatis.annotations.Mapper;
  5 +import org.apache.ibatis.annotations.Param;
  6 +
  7 +/**
  8 + * @author 子诚
  9 + * Description:
  10 + * 时间:2020/7/14 10:35
  11 + */
  12 +@Mapper
  13 +public interface SchedulingDeleteMapper {
  14 + /**
  15 + * delete by primary key
  16 + *
  17 + * @param id primaryKey
  18 + * @return deleteCount
  19 + */
  20 + int deleteByPrimaryKey(String id);
  21 +
  22 + /**
  23 + * insert record to table
  24 + *
  25 + * @param record the record
  26 + * @return insert count
  27 + */
  28 + int insert(SchedulingDelete record);
  29 +
  30 + /**
  31 + * insert record to table selective
  32 + *
  33 + * @param record the record
  34 + * @return insert count
  35 + */
  36 + int insertSelective(SchedulingDelete record);
  37 +
  38 + /**
  39 + * select by primary key
  40 + *
  41 + * @param id primary key
  42 + * @return object by primary key
  43 + */
  44 + SchedulingDelete selectByPrimaryKey(String id);
  45 +
  46 + /**
  47 + * update record selective
  48 + *
  49 + * @param record the updated record
  50 + * @return update count
  51 + */
  52 + int updateByPrimaryKeySelective(SchedulingDelete record);
  53 +
  54 + /**
  55 + * update record
  56 + *
  57 + * @param record the updated record
  58 + * @return update count
  59 + */
  60 + int updateByPrimaryKey(SchedulingDelete record);
  61 +
  62 + /**
  63 + * 获取时间设置表
  64 + *
  65 + * @param deleteType 删除的类型
  66 + * @return {@link SchedulingDelete }
  67 + */
  68 + SchedulingDelete selectByType(@Param("deleteType") String deleteType);
  69 +}
@@ -67,6 +67,13 @@ public interface MessageNoteService { @@ -67,6 +67,13 @@ public interface MessageNoteService {
67 * @return 消息收发记录-列表 67 * @return 消息收发记录-列表
68 */ 68 */
69 PageInfo selectMessageNoteList(MessageNote messageNote, Integer pageNum, Integer pageSize); 69 PageInfo selectMessageNoteList(MessageNote messageNote, Integer pageNum, Integer pageSize);
  70 +
  71 + /**
  72 + * 自动删除消息收发记录
  73 + *
  74 + * @param deleteTime 删除的时间
  75 + */
  76 + void autoDelete(Integer deleteTime);
70 } 77 }
71 78
72 79
  1 +package com.sunyo.wlpt.message.bus.service.service;
  2 +
  3 +import com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete;
  4 +
  5 +/**
  6 + * @author 子诚
  7 + * Description:
  8 + * 时间:2020/7/14 10:35
  9 + */
  10 +public interface SchedulingDeleteService {
  11 +
  12 +
  13 + /**
  14 + * delete by primary key
  15 + *
  16 + * @param id primaryKey
  17 + * @return deleteCount
  18 + */
  19 + int deleteByPrimaryKey(String id);
  20 +
  21 + /**
  22 + * insert record to table
  23 + *
  24 + * @param record the record
  25 + * @return insert count
  26 + */
  27 + int insert(SchedulingDelete record);
  28 +
  29 + /**
  30 + * insert record to table selective
  31 + *
  32 + * @param record the record
  33 + * @return insert count
  34 + */
  35 + int insertSelective(SchedulingDelete record);
  36 +
  37 + /**
  38 + * select by primary key
  39 + *
  40 + * @param id primary key
  41 + * @return object by primary key
  42 + */
  43 + SchedulingDelete selectByPrimaryKey(String id);
  44 +
  45 + /**
  46 + * update record selective
  47 + *
  48 + * @param record the updated record
  49 + * @return update count
  50 + */
  51 + int updateByPrimaryKeySelective(SchedulingDelete record);
  52 +
  53 + /**
  54 + * update record
  55 + *
  56 + * @param record the updated record
  57 + * @return update count
  58 + */
  59 + int updateByPrimaryKey(SchedulingDelete record);
  60 +
  61 + /**
  62 + * 获取时间设置表
  63 + *
  64 + * @param deleteType 删除的类型
  65 + * @return {@link SchedulingDelete }
  66 + */
  67 + SchedulingDelete selectByType(String deleteType);
  68 +}
@@ -89,6 +89,11 @@ public class MessageNoteServiceImpl implements MessageNoteService { @@ -89,6 +89,11 @@ public class MessageNoteServiceImpl implements MessageNoteService {
89 return pageInfo; 89 return pageInfo;
90 } 90 }
91 91
  92 + @Override
  93 + public void autoDelete(Integer deleteTime) {
  94 + messageNoteMapper.autoDelete(deleteTime);
  95 + }
  96 +
92 } 97 }
93 98
94 99
  1 +package com.sunyo.wlpt.message.bus.service.service.impl;
  2 +
  3 +import org.springframework.stereotype.Service;
  4 +
  5 +import javax.annotation.Resource;
  6 +
  7 +import com.sunyo.wlpt.message.bus.service.mapper.SchedulingDeleteMapper;
  8 +import com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete;
  9 +import com.sunyo.wlpt.message.bus.service.service.SchedulingDeleteService;
  10 +
  11 +/**
  12 + * @author 子诚
  13 + * Description:
  14 + * 时间:2020/7/14 10:35
  15 + */
  16 +@Service
  17 +public class SchedulingDeleteServiceImpl implements SchedulingDeleteService {
  18 +
  19 + @Resource
  20 + private SchedulingDeleteMapper schedulingDeleteMapper;
  21 +
  22 + @Override
  23 + public int deleteByPrimaryKey(String id) {
  24 + return schedulingDeleteMapper.deleteByPrimaryKey(id);
  25 + }
  26 +
  27 + @Override
  28 + public int insert(SchedulingDelete record) {
  29 + return schedulingDeleteMapper.insert(record);
  30 + }
  31 +
  32 + @Override
  33 + public int insertSelective(SchedulingDelete record) {
  34 + return schedulingDeleteMapper.insertSelective(record);
  35 + }
  36 +
  37 + @Override
  38 + public SchedulingDelete selectByPrimaryKey(String id) {
  39 + return schedulingDeleteMapper.selectByPrimaryKey(id);
  40 + }
  41 +
  42 + @Override
  43 + public int updateByPrimaryKeySelective(SchedulingDelete record) {
  44 + return schedulingDeleteMapper.updateByPrimaryKeySelective(record);
  45 + }
  46 +
  47 + @Override
  48 + public int updateByPrimaryKey(SchedulingDelete record) {
  49 + return schedulingDeleteMapper.updateByPrimaryKey(record);
  50 + }
  51 +
  52 + @Override
  53 + public SchedulingDelete selectByType(String deleteType) {
  54 + return schedulingDeleteMapper.selectByType(deleteType);
  55 + }
  56 +
  57 +}
@@ -25,8 +25,8 @@ @@ -25,8 +25,8 @@
25 </resultMap> 25 </resultMap>
26 <sql id="Base_Column_List"> 26 <sql id="Base_Column_List">
27 <!--@mbg.generated--> 27 <!--@mbg.generated-->
28 - id, user_id, username, server_id, `server_name`, virtual_host_id, virtual_host_name,  
29 - exchange_id, exchange_name, queue_id, queue_name, routing_key_id, routing_key_name, 28 + id, user_id, username, server_id, `server_name`, virtual_host_id, virtual_host_name,
  29 + exchange_id, exchange_name, queue_id, queue_name, routing_key_id, routing_key_name,
30 send_time, receive_time, send_content, gmt_create, gmt_modified 30 send_time, receive_time, send_content, gmt_create, gmt_modified
31 </sql> 31 </sql>
32 <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap"> 32 <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
@@ -254,6 +254,7 @@ @@ -254,6 +254,7 @@
254 gmt_modified = #{gmtModified,jdbcType=TIMESTAMP} 254 gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
255 where id = #{id,jdbcType=VARCHAR} 255 where id = #{id,jdbcType=VARCHAR}
256 </update> 256 </update>
  257 +
257 <!-- 获取,消息收发记录列表 --> 258 <!-- 获取,消息收发记录列表 -->
258 <select id="selectMessageNoteList" parameterType="com.sunyo.wlpt.message.bus.service.domain.MessageNote" resultMap="BaseResultMap"> 259 <select id="selectMessageNoteList" parameterType="com.sunyo.wlpt.message.bus.service.domain.MessageNote" resultMap="BaseResultMap">
259 select 260 select
@@ -294,5 +295,9 @@ @@ -294,5 +295,9 @@
294 </if> 295 </if>
295 </where> 296 </where>
296 </select> 297 </select>
297 - 298 + <!-- 自动删除 message_note记录 -->
  299 + <delete id="autoDelete" parameterType="java.lang.Integer">
  300 + delete from message_note
  301 + where to_days(now())-to_days(gmt_create) >= #{deleteTime,jdbcType=INTEGER}
  302 + </delete>
298 </mapper> 303 </mapper>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 +<mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.SchedulingDeleteMapper">
  4 + <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
  5 + <!--@mbg.generated-->
  6 + <!--@Table scheduling_delete-->
  7 + <id column="id" jdbcType="VARCHAR" property="id" />
  8 + <result column="delete_time" jdbcType="INTEGER" property="deleteTime" />
  9 + <result column="delete_type" jdbcType="VARCHAR" property="deleteType" />
  10 + </resultMap>
  11 + <sql id="Base_Column_List">
  12 + <!--@mbg.generated-->
  13 + id, delete_time, delete_type
  14 + </sql>
  15 + <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
  16 + <!--@mbg.generated-->
  17 + select
  18 + <include refid="Base_Column_List" />
  19 + from scheduling_delete
  20 + where id = #{id,jdbcType=VARCHAR}
  21 + </select>
  22 +
  23 + <select id="selectByType" parameterType="java.lang.String" resultMap="BaseResultMap">
  24 + select
  25 + <include refid="Base_Column_List" />
  26 + from scheduling_delete
  27 + <where>
  28 + <!-- 删除类型 -->
  29 + <if test="deleteType != null and deleteType !=''">
  30 + delete_type = #{deleteType,jdbcType=VARCHAR}
  31 + </if>
  32 + </where>
  33 +
  34 + </select>
  35 + <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
  36 + <!--@mbg.generated-->
  37 + delete from scheduling_delete
  38 + where id = #{id,jdbcType=VARCHAR}
  39 + </delete>
  40 + <insert id="insert" parameterType="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
  41 + <!--@mbg.generated-->
  42 + insert into scheduling_delete (id, delete_time, delete_type
  43 + )
  44 + values (#{id,jdbcType=VARCHAR}, #{deleteTime,jdbcType=INTEGER}, #{deleteType,jdbcType=VARCHAR}
  45 + )
  46 + </insert>
  47 + <insert id="insertSelective" parameterType="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
  48 + <!--@mbg.generated-->
  49 + insert into scheduling_delete
  50 + <trim prefix="(" suffix=")" suffixOverrides=",">
  51 + <if test="id != null">
  52 + id,
  53 + </if>
  54 + <if test="deleteTime != null">
  55 + delete_time,
  56 + </if>
  57 + <if test="deleteType != null">
  58 + delete_type,
  59 + </if>
  60 + </trim>
  61 + <trim prefix="values (" suffix=")" suffixOverrides=",">
  62 + <if test="id != null">
  63 + #{id,jdbcType=VARCHAR},
  64 + </if>
  65 + <if test="deleteTime != null">
  66 + #{deleteTime,jdbcType=INTEGER},
  67 + </if>
  68 + <if test="deleteType != null">
  69 + #{deleteType,jdbcType=VARCHAR},
  70 + </if>
  71 + </trim>
  72 + </insert>
  73 + <update id="updateByPrimaryKeySelective" parameterType="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
  74 + <!--@mbg.generated-->
  75 + update scheduling_delete
  76 + <set>
  77 + <if test="deleteTime != null">
  78 + delete_time = #{deleteTime,jdbcType=INTEGER},
  79 + </if>
  80 + <if test="deleteType != null">
  81 + delete_type = #{deleteType,jdbcType=VARCHAR},
  82 + </if>
  83 + </set>
  84 + where id = #{id,jdbcType=VARCHAR}
  85 + </update>
  86 + <update id="updateByPrimaryKey" parameterType="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
  87 + <!--@mbg.generated-->
  88 + update scheduling_delete
  89 + set delete_time = #{deleteTime,jdbcType=INTEGER},
  90 + delete_type = #{deleteType,jdbcType=VARCHAR}
  91 + where id = #{id,jdbcType=VARCHAR}
  92 + </update>
  93 +</mapper>