Browse Source

1、优化定时任务:执行记录保存到数据库

liutielin 1 year ago
parent
commit
8d5a5ea3af

+ 25 - 3
pom.xml

@@ -20,7 +20,7 @@
 	<dependencies>
 		<dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter</artifactId>
+            <artifactId>spring-boot-starter-web</artifactId>
             <exclusions>
 				<exclusion>
 					<groupId>org.springframework.boot</groupId>
@@ -45,7 +45,11 @@
 			<artifactId>mybatis-spring-boot-starter</artifactId>
 			<version>1.3.2</version>
 		</dependency>
-
+		<!-- 访问静态资源. -->
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-thymeleaf</artifactId>
+		</dependency>
         <!--  cache -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -107,7 +111,25 @@
 		<dependency>
 		    <groupId>org.apache.commons</groupId>
 		    <artifactId>commons-pool2</artifactId>
-		</dependency>        
+		</dependency>  
+		
+		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.28</version>
+        </dependency> 
+        <dependency> 
+		     <groupId>org.springframework.boot</groupId> 
+		     <artifactId>spring-boot-devtools</artifactId> 
+		     <optional>true</optional> 
+		</dependency> 
+		<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
+        <dependency>
+            <groupId>org.bouncycastle</groupId>
+            <artifactId>bcprov-jdk15on</artifactId>
+            <version>1.60</version>
+        </dependency>    
 	</dependencies>
 
 	<build>

+ 8 - 0
src/main/java/com/up/sell/mapper/SchedulerContentMapper.java

@@ -0,0 +1,8 @@
+package com.up.sell.mapper;
+
+import com.up.sell.vo.SchedulerContent;
+
+public interface SchedulerContentMapper {
+
+	void insert(SchedulerContent schedulerConten);
+}

+ 17 - 0
src/main/java/com/up/sell/service/SchedulerContentService.java

@@ -0,0 +1,17 @@
+package com.up.sell.service;
+
+import com.up.sell.vo.SchedulerContent;
+
+public interface SchedulerContentService {
+	
+	/**
+	 * 保存定时执行结果
+	 * @param name 任务名称
+	 * @param resultNum 执行结果数据量
+	 */
+	public void insertResultForJob(String name,Integer resultNum);
+
+	public void insert(SchedulerContent schedulerConten);
+	
+	
+}

+ 7 - 1
src/main/java/com/up/sell/service/impl/MemberOrderSericeImpl.java

@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import com.up.sell.mapper.MemberOrderMapper;
 import com.up.sell.service.MemberOrderSerice;
+import com.up.sell.service.SchedulerContentService;
 
 /**
  * @author zmg
@@ -22,12 +23,17 @@ public class MemberOrderSericeImpl implements MemberOrderSerice {
 	@Autowired
 	private MemberOrderMapper memberOrderMapper;
 	
+	@Autowired
+	private SchedulerContentService schedulerContentService;
+	
 	/* (non-Javadoc)
 	 * @see com.up.sell.service.impl.MemberOrderSerice#cancelOrder(int)
 	 */
 	@Override
 	@Transactional(propagation=Propagation.REQUIRED)
 	public int cancelOrder(int hours) {
-		return this.memberOrderMapper.cancelOrder(hours);
+		int n = this.memberOrderMapper.cancelOrder(hours);
+		schedulerContentService.insertResultForJob("24小时未支付订单自动取消", n);
+		return n;
 	}
 }

+ 8 - 1
src/main/java/com/up/sell/service/impl/MemberSericeImpl.java

@@ -18,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
 import com.up.sell.mapper.MemberMapper;
 import com.up.sell.mapper.MemberOrderMapper;
 import com.up.sell.service.MemberSerice;
+import com.up.sell.service.SchedulerContentService;
 import com.up.sell.vo.MemberOrder;
 
 /**
@@ -34,6 +35,9 @@ public class MemberSericeImpl implements MemberSerice{
 	
 	@Autowired
 	private MemberOrderMapper memberOrderMapper;
+	
+	@Autowired
+	private SchedulerContentService schedulerContentService;
 
 	
 	/**
@@ -41,7 +45,9 @@ public class MemberSericeImpl implements MemberSerice{
 	 */
 	@Transactional(propagation=Propagation.REQUIRED)
 	public int updateViewCount(Integer defaultViewCount) {
-		return this.memberMapper.updateViewCount(defaultViewCount);
+		Integer n = this.memberMapper.updateViewCount(defaultViewCount);
+		schedulerContentService.insertResultForJob("每天零点重设每个会员剩余查看品牌联系人的次数", n);
+		return n;
 	}
 
 	@Override
@@ -61,6 +67,7 @@ public class MemberSericeImpl implements MemberSerice{
 			}
 		}
 		logger.info("会员过期总数:" + count);
+		schedulerContentService.insertResultForJob("每天零点查询会员是否过期", count);
 		return 0;
 	}
 	

+ 35 - 0
src/main/java/com/up/sell/service/impl/SchedulerContentServiceImpl.java

@@ -0,0 +1,35 @@
+package com.up.sell.service.impl;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.up.sell.mapper.SchedulerContentMapper;
+import com.up.sell.service.SchedulerContentService;
+import com.up.sell.vo.SchedulerContent;
+
+@Service
+public class SchedulerContentServiceImpl implements SchedulerContentService{
+
+	@Autowired
+	private SchedulerContentMapper schedulerContentMapper;
+	
+	@Override
+	public void insertResultForJob(String name,Integer resultNum) {
+		// TODO Auto-generated method stub
+		SchedulerContent schedulerContent = new SchedulerContent();
+		schedulerContent.setName(name);
+		schedulerContent.setResultContent(resultNum+"");
+		schedulerContentMapper.insert(schedulerContent);
+	}
+	
+	@Override
+	public void insert(SchedulerContent schedulerConten) {
+		// TODO Auto-generated method stub
+		schedulerContentMapper.insert(schedulerConten);
+	}
+
+	
+	
+	
+
+}

+ 49 - 0
src/main/java/com/up/sell/vo/SchedulerContent.java

@@ -0,0 +1,49 @@
+package com.up.sell.vo;
+
+import java.util.Date;
+
+
+public class SchedulerContent {
+
+	private int id;
+	
+	private String name;
+	
+	private String resultContent;
+	
+	private Date createTime;
+
+	public int getId() {
+		return id;
+	}
+
+	public void setId(int id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getResultContent() {
+		return resultContent;
+	}
+
+	public void setResultContent(String resultContent) {
+		this.resultContent = resultContent;
+	}
+
+	public Date getCreateTime() {
+		return createTime;
+	}
+
+	public void setCreateTime(Date createTime) {
+		this.createTime = createTime;
+	}
+	
+	
+}

+ 9 - 0
src/main/resources/configs/mybatis/xml/SchedulerContent.xml

@@ -0,0 +1,9 @@
+<?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.up.sell.mapper.SchedulerContentMapper">
+  
+  <insert id="insert" parameterType="com.up.sell.vo.SchedulerContent">
+  	insert into scheduler_content(name,result_content,create_time) 
+  	values (#{name},#{resultContent},now())
+  </insert>
+</mapper>