作者 王勇

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

... ... @@ -5,6 +5,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
... ... @@ -15,6 +16,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableFeignClients
@EnableEurekaClient
@EnableTransactionManagement
@EnableScheduling
public class MessageBusServiceApplication {
public static void main(String[] args) {
... ...
... ... @@ -3,10 +3,13 @@ package com.sunyo.wlpt.message.bus.service.controller;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
import com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete;
import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding;
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
import com.sunyo.wlpt.message.bus.service.service.MessageNoteService;
import com.sunyo.wlpt.message.bus.service.service.SchedulingDeleteService;
import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
... ... @@ -25,6 +28,9 @@ public class MessageNoteController {
@Resource
private MessageNoteService messageNoteService;
@Resource
private SchedulingDeleteService schedulingDeleteService;
/**
* 分页查询,消息收发记录
*
... ... @@ -167,4 +173,20 @@ public class MessageNoteController {
}
return result;
}
/**
* 每日凌晨4点
* 自动删除 (默认时间)7天前的记录
*/
@Scheduled(cron = "0 0 4 * * ? ")
@GetMapping("/delete")
public void autoDeleteMessageNote() {
// 类型
String deleteType = "message_note";
// 首先获取可自主设置的默认时间
SchedulingDelete schedulingDelete = schedulingDeleteService.selectByType(deleteType);
Integer deleteTime = schedulingDelete.getDeleteTime();
// 定时自动删除
messageNoteService.autoDelete(deleteTime);
}
}
... ...
package com.sunyo.wlpt.message.bus.service.domain;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author 子诚
* Description:自动定时删除的时间设置表
* 时间:2020/7/14 10:35
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SchedulingDelete implements Serializable {
private static final long serialVersionUID = -3199487735394538126L;
/**
* 时间设置表的ID
*/
private String id;
/**
* 设置时间
*/
private Integer deleteTime;
/**
* 类型
*/
private String deleteType;
}
\ No newline at end of file
... ...
package com.sunyo.wlpt.message.bus.service.mapper;
import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author 子诚
... ... @@ -65,4 +68,11 @@ public interface MessageNoteMapper {
* @return List<MessageNote>
*/
List<MessageNote> selectMessageNoteList(MessageNote messageNote);
/**
* 自动删除消息收发记录
*
* @param deleteTime 删除的时间
*/
void autoDelete(@Param("deleteTime") Integer deleteTime);
}
\ No newline at end of file
... ...
package com.sunyo.wlpt.message.bus.service.mapper;
import com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @author 子诚
* Description:
* 时间:2020/7/14 10:35
*/
@Mapper
public interface SchedulingDeleteMapper {
/**
* delete by primary key
*
* @param id primaryKey
* @return deleteCount
*/
int deleteByPrimaryKey(String id);
/**
* insert record to table
*
* @param record the record
* @return insert count
*/
int insert(SchedulingDelete record);
/**
* insert record to table selective
*
* @param record the record
* @return insert count
*/
int insertSelective(SchedulingDelete record);
/**
* select by primary key
*
* @param id primary key
* @return object by primary key
*/
SchedulingDelete selectByPrimaryKey(String id);
/**
* update record selective
*
* @param record the updated record
* @return update count
*/
int updateByPrimaryKeySelective(SchedulingDelete record);
/**
* update record
*
* @param record the updated record
* @return update count
*/
int updateByPrimaryKey(SchedulingDelete record);
/**
* 获取时间设置表
*
* @param deleteType 删除的类型
* @return {@link SchedulingDelete }
*/
SchedulingDelete selectByType(@Param("deleteType") String deleteType);
}
\ No newline at end of file
... ...
... ... @@ -67,6 +67,13 @@ public interface MessageNoteService {
* @return 消息收发记录-列表
*/
PageInfo selectMessageNoteList(MessageNote messageNote, Integer pageNum, Integer pageSize);
/**
* 自动删除消息收发记录
*
* @param deleteTime 删除的时间
*/
void autoDelete(Integer deleteTime);
}
... ...
package com.sunyo.wlpt.message.bus.service.service;
import com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete;
/**
* @author 子诚
* Description:
* 时间:2020/7/14 10:35
*/
public interface SchedulingDeleteService {
/**
* delete by primary key
*
* @param id primaryKey
* @return deleteCount
*/
int deleteByPrimaryKey(String id);
/**
* insert record to table
*
* @param record the record
* @return insert count
*/
int insert(SchedulingDelete record);
/**
* insert record to table selective
*
* @param record the record
* @return insert count
*/
int insertSelective(SchedulingDelete record);
/**
* select by primary key
*
* @param id primary key
* @return object by primary key
*/
SchedulingDelete selectByPrimaryKey(String id);
/**
* update record selective
*
* @param record the updated record
* @return update count
*/
int updateByPrimaryKeySelective(SchedulingDelete record);
/**
* update record
*
* @param record the updated record
* @return update count
*/
int updateByPrimaryKey(SchedulingDelete record);
/**
* 获取时间设置表
*
* @param deleteType 删除的类型
* @return {@link SchedulingDelete }
*/
SchedulingDelete selectByType(String deleteType);
}
... ...
... ... @@ -89,6 +89,11 @@ public class MessageNoteServiceImpl implements MessageNoteService {
return pageInfo;
}
@Override
public void autoDelete(Integer deleteTime) {
messageNoteMapper.autoDelete(deleteTime);
}
}
... ...
package com.sunyo.wlpt.message.bus.service.service.impl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.sunyo.wlpt.message.bus.service.mapper.SchedulingDeleteMapper;
import com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete;
import com.sunyo.wlpt.message.bus.service.service.SchedulingDeleteService;
/**
* @author 子诚
* Description:
* 时间:2020/7/14 10:35
*/
@Service
public class SchedulingDeleteServiceImpl implements SchedulingDeleteService {
@Resource
private SchedulingDeleteMapper schedulingDeleteMapper;
@Override
public int deleteByPrimaryKey(String id) {
return schedulingDeleteMapper.deleteByPrimaryKey(id);
}
@Override
public int insert(SchedulingDelete record) {
return schedulingDeleteMapper.insert(record);
}
@Override
public int insertSelective(SchedulingDelete record) {
return schedulingDeleteMapper.insertSelective(record);
}
@Override
public SchedulingDelete selectByPrimaryKey(String id) {
return schedulingDeleteMapper.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(SchedulingDelete record) {
return schedulingDeleteMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(SchedulingDelete record) {
return schedulingDeleteMapper.updateByPrimaryKey(record);
}
@Override
public SchedulingDelete selectByType(String deleteType) {
return schedulingDeleteMapper.selectByType(deleteType);
}
}
... ...
... ... @@ -25,8 +25,8 @@
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, user_id, username, server_id, `server_name`, virtual_host_id, virtual_host_name,
exchange_id, exchange_name, queue_id, queue_name, routing_key_id, routing_key_name,
id, user_id, username, server_id, `server_name`, virtual_host_id, virtual_host_name,
exchange_id, exchange_name, queue_id, queue_name, routing_key_id, routing_key_name,
send_time, receive_time, send_content, gmt_create, gmt_modified
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
... ... @@ -254,6 +254,7 @@
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=VARCHAR}
</update>
<!-- 获取,消息收发记录列表 -->
<select id="selectMessageNoteList" parameterType="com.sunyo.wlpt.message.bus.service.domain.MessageNote" resultMap="BaseResultMap">
select
... ... @@ -294,5 +295,9 @@
</if>
</where>
</select>
<!-- 自动删除 message_note记录 -->
<delete id="autoDelete" parameterType="java.lang.Integer">
delete from message_note
where to_days(now())-to_days(gmt_create) >= #{deleteTime,jdbcType=INTEGER}
</delete>
</mapper>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.SchedulingDeleteMapper">
<resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
<!--@mbg.generated-->
<!--@Table scheduling_delete-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="delete_time" jdbcType="INTEGER" property="deleteTime" />
<result column="delete_type" jdbcType="VARCHAR" property="deleteType" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, delete_time, delete_type
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List" />
from scheduling_delete
where id = #{id,jdbcType=VARCHAR}
</select>
<select id="selectByType" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from scheduling_delete
<where>
<!-- 删除类型 -->
<if test="deleteType != null and deleteType !=''">
delete_type = #{deleteType,jdbcType=VARCHAR}
</if>
</where>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
<!--@mbg.generated-->
delete from scheduling_delete
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
<!--@mbg.generated-->
insert into scheduling_delete (id, delete_time, delete_type
)
values (#{id,jdbcType=VARCHAR}, #{deleteTime,jdbcType=INTEGER}, #{deleteType,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
<!--@mbg.generated-->
insert into scheduling_delete
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="deleteTime != null">
delete_time,
</if>
<if test="deleteType != null">
delete_type,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="deleteTime != null">
#{deleteTime,jdbcType=INTEGER},
</if>
<if test="deleteType != null">
#{deleteType,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
<!--@mbg.generated-->
update scheduling_delete
<set>
<if test="deleteTime != null">
delete_time = #{deleteTime,jdbcType=INTEGER},
</if>
<if test="deleteType != null">
delete_type = #{deleteType,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.sunyo.wlpt.message.bus.service.domain.SchedulingDelete">
<!--@mbg.generated-->
update scheduling_delete
set delete_time = #{deleteTime,jdbcType=INTEGER},
delete_type = #{deleteType,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>
\ No newline at end of file
... ...