Skip to content
This repository was archived by the owner on Jul 21, 2024. It is now read-only.

Commit 1e1b7e3

Browse files
committed
+ 增加定时任务,每30-60分钟请求一下接口,确保 Refresh Token 不过期
1 parent a9c7a73 commit 1e1b7e3

File tree

5 files changed

+104
-26
lines changed

5 files changed

+104
-26
lines changed

proguard-rules.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929
-keep class net.sf.webdav.WebdavServlet
3030
-keep class com.github.zxbu.webdavteambition.store.AliYunDriverFileSystemStore
3131
-keep class com.github.zxbu.webdavteambition.filter.ErrorFilter
32+
-keep class com.github.zxbu.webdavteambition.store.StartupService

src/main/java/com/github/zxbu/webdavteambition/config/AliYunDriverCronTask.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,57 @@
22

33
import com.github.zxbu.webdavteambition.model.result.TFile;
44
import com.github.zxbu.webdavteambition.store.AliYunDriverClientService;
5-
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.scheduling.annotation.Scheduled;
7-
import org.springframework.stereotype.Component;
85

9-
@Component
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.ScheduledExecutorService;
11+
import java.util.concurrent.TimeUnit;
12+
1013
public class AliYunDriverCronTask {
1114

12-
@Autowired
13-
private AliYunDriverClientService aliYunDriverClientService;
15+
private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriverCronTask.class);
16+
17+
private final AliYunDriverClientService mAliYunDriverClientService;
18+
19+
private ScheduledExecutorService mTaskPool = Executors.newScheduledThreadPool(1);
20+
21+
22+
public AliYunDriverCronTask(AliYunDriverClientService service) {
23+
mAliYunDriverClientService = service;
24+
}
1425

1526
/**
16-
* 每隔5分钟请求一下接口,保证token不过期
27+
* 每隔30-60分钟请求一下接口,保证token不过期
1728
*/
18-
@Scheduled(initialDelay = 30 * 1000, fixedDelay = 5 * 60 * 1000)
1929
public void refreshToken() {
2030
try {
21-
TFile root = aliYunDriverClientService.getTFileByPath("/");
22-
aliYunDriverClientService.getTFiles(root.getFile_id());
23-
} catch (Exception e) {
24-
// nothing
31+
LOGGER.info("定时刷新 Refresh Token 任务开始");
32+
TFile root = mAliYunDriverClientService.getTFileByPath("/");
33+
mAliYunDriverClientService.getTFiles(root.getFile_id());
34+
} catch (Throwable e) {
35+
LOGGER.error("", e);
36+
} finally {
37+
LOGGER.info("定时刷新 Refresh Token 任务结束");
2538
}
39+
}
40+
41+
public void start() {
42+
mTaskPool.schedule(new Runnable() {
43+
@Override
44+
public void run() {
45+
refreshToken();
46+
mTaskPool.schedule(this, getRandomNumber(30, 60), TimeUnit.MINUTES);
47+
}
48+
}, 10, TimeUnit.SECONDS);
49+
}
50+
51+
public void stop() {
52+
mTaskPool.shutdownNow();
53+
}
2654

55+
private int getRandomNumber(int min, int max) {
56+
return (int) ((Math.random() * (max - min)) + min);
2757
}
2858
}

src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@
3838

3939
public class AliYunDriverClientService {
4040

41-
private static class Holder {
42-
private static AliYunDriverClientService sAliYunDriverClientService;
43-
44-
static {
45-
ContextHandler.Context webContext = WebAppContext.getCurrentContext();
46-
Context context = (Context) webContext.getAttribute("org.mortbay.ijetty.context");
47-
AliYunDriveProperties properties = new AliYunDriveProperties();
48-
properties.setRefreshToken(String.valueOf(webContext.getAttribute(context.getString(net.xdow.library.R.string.config_refresh_token))));
49-
properties.setWorkDir(context.getFilesDir().getAbsolutePath() + File.separator);
50-
sAliYunDriverClientService = new AliYunDriverClientService(new AliYunDriverClient(properties));
51-
}
52-
}
53-
41+
private static AliYunDriverClientService sInstance;
5442
public static AliYunDriverClientService getInstance() {
55-
return Holder.sAliYunDriverClientService;
43+
if (sInstance == null) {
44+
synchronized (AliYunDriverClientService.class) {
45+
if (sInstance == null) {
46+
ContextHandler.Context webContext = WebAppContext.getCurrentContext();
47+
Context context = (Context) webContext.getAttribute("org.mortbay.ijetty.context");
48+
AliYunDriveProperties properties = new AliYunDriveProperties();
49+
properties.setRefreshToken(String.valueOf(webContext.getAttribute(context.getString(net.xdow.library.R.string.config_refresh_token))));
50+
properties.setWorkDir(context.getFilesDir().getAbsolutePath() + File.separator);
51+
sInstance = new AliYunDriverClientService(new AliYunDriverClient(properties));
52+
}
53+
}
54+
}
55+
return sInstance;
5656
}
5757

5858
private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriverClientService.class);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.github.zxbu.webdavteambition.store;
2+
3+
import com.github.zxbu.webdavteambition.config.AliYunDriverCronTask;
4+
5+
import java.io.IOException;
6+
7+
import javax.servlet.GenericServlet;
8+
import javax.servlet.ServletException;
9+
import javax.servlet.ServletRequest;
10+
import javax.servlet.ServletResponse;
11+
12+
public class StartupService extends GenericServlet {
13+
14+
private AliYunDriverCronTask mAliYunDriverCronTask;
15+
16+
@Override
17+
public void init() throws ServletException {
18+
super.init();
19+
AliYunDriverCronTask task = mAliYunDriverCronTask;
20+
if (task != null) {
21+
task.stop();
22+
}
23+
task = new AliYunDriverCronTask(AliYunDriverClientService.getInstance());
24+
mAliYunDriverCronTask = task;
25+
task.start();
26+
}
27+
28+
@Override
29+
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
30+
31+
}
32+
33+
@Override
34+
public void destroy() {
35+
super.destroy();
36+
AliYunDriverCronTask task = mAliYunDriverCronTask;
37+
if (task != null) {
38+
task.stop();
39+
mAliYunDriverCronTask = null;
40+
}
41+
}
42+
}

src/main/webapp/WEB-INF/web.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
xmlns="http://java.sun.com/xml/ns/javaee"
55
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
66

7+
<servlet>
8+
<servlet-name>StartupService</servlet-name>
9+
<servlet-class>com.github.zxbu.webdavteambition.store.StartupService</servlet-class>
10+
<load-on-startup>1</load-on-startup>
11+
</servlet>
712
<servlet>
813
<servlet-name>WebdavServlet</servlet-name>
914
<servlet-class>net.sf.webdav.WebdavServlet</servlet-class>

0 commit comments

Comments
 (0)