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

Commit

Permalink
+ 增加定时任务,每30-60分钟请求一下接口,确保 Refresh Token 不过期
Browse files Browse the repository at this point in the history
  • Loading branch information
eritpchy committed Oct 31, 2021
1 parent a9c7a73 commit 1e1b7e3
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 26 deletions.
1 change: 1 addition & 0 deletions proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
-keep class net.sf.webdav.WebdavServlet
-keep class com.github.zxbu.webdavteambition.store.AliYunDriverFileSystemStore
-keep class com.github.zxbu.webdavteambition.filter.ErrorFilter
-keep class com.github.zxbu.webdavteambition.store.StartupService
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,57 @@

import com.github.zxbu.webdavteambition.model.result.TFile;
import com.github.zxbu.webdavteambition.store.AliYunDriverClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class AliYunDriverCronTask {

@Autowired
private AliYunDriverClientService aliYunDriverClientService;
private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriverCronTask.class);

private final AliYunDriverClientService mAliYunDriverClientService;

private ScheduledExecutorService mTaskPool = Executors.newScheduledThreadPool(1);


public AliYunDriverCronTask(AliYunDriverClientService service) {
mAliYunDriverClientService = service;
}

/**
* 每隔5分钟请求一下接口,保证token不过期
* 每隔30-60分钟请求一下接口,保证token不过期
*/
@Scheduled(initialDelay = 30 * 1000, fixedDelay = 5 * 60 * 1000)
public void refreshToken() {
try {
TFile root = aliYunDriverClientService.getTFileByPath("/");
aliYunDriverClientService.getTFiles(root.getFile_id());
} catch (Exception e) {
// nothing
LOGGER.info("定时刷新 Refresh Token 任务开始");
TFile root = mAliYunDriverClientService.getTFileByPath("/");
mAliYunDriverClientService.getTFiles(root.getFile_id());
} catch (Throwable e) {
LOGGER.error("", e);
} finally {
LOGGER.info("定时刷新 Refresh Token 任务结束");
}
}

public void start() {
mTaskPool.schedule(new Runnable() {
@Override
public void run() {
refreshToken();
mTaskPool.schedule(this, getRandomNumber(30, 60), TimeUnit.MINUTES);
}
}, 10, TimeUnit.SECONDS);
}

public void stop() {
mTaskPool.shutdownNow();
}

private int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@

public class AliYunDriverClientService {

private static class Holder {
private static AliYunDriverClientService sAliYunDriverClientService;

static {
ContextHandler.Context webContext = WebAppContext.getCurrentContext();
Context context = (Context) webContext.getAttribute("org.mortbay.ijetty.context");
AliYunDriveProperties properties = new AliYunDriveProperties();
properties.setRefreshToken(String.valueOf(webContext.getAttribute(context.getString(net.xdow.library.R.string.config_refresh_token))));
properties.setWorkDir(context.getFilesDir().getAbsolutePath() + File.separator);
sAliYunDriverClientService = new AliYunDriverClientService(new AliYunDriverClient(properties));
}
}

private static AliYunDriverClientService sInstance;
public static AliYunDriverClientService getInstance() {
return Holder.sAliYunDriverClientService;
if (sInstance == null) {
synchronized (AliYunDriverClientService.class) {
if (sInstance == null) {
ContextHandler.Context webContext = WebAppContext.getCurrentContext();
Context context = (Context) webContext.getAttribute("org.mortbay.ijetty.context");
AliYunDriveProperties properties = new AliYunDriveProperties();
properties.setRefreshToken(String.valueOf(webContext.getAttribute(context.getString(net.xdow.library.R.string.config_refresh_token))));
properties.setWorkDir(context.getFilesDir().getAbsolutePath() + File.separator);
sInstance = new AliYunDriverClientService(new AliYunDriverClient(properties));
}
}
}
return sInstance;
}

private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriverClientService.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.zxbu.webdavteambition.store;

import com.github.zxbu.webdavteambition.config.AliYunDriverCronTask;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class StartupService extends GenericServlet {

private AliYunDriverCronTask mAliYunDriverCronTask;

@Override
public void init() throws ServletException {
super.init();
AliYunDriverCronTask task = mAliYunDriverCronTask;
if (task != null) {
task.stop();
}
task = new AliYunDriverCronTask(AliYunDriverClientService.getInstance());
mAliYunDriverCronTask = task;
task.start();
}

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {

}

@Override
public void destroy() {
super.destroy();
AliYunDriverCronTask task = mAliYunDriverCronTask;
if (task != null) {
task.stop();
mAliYunDriverCronTask = null;
}
}
}
5 changes: 5 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
<servlet-name>StartupService</servlet-name>
<servlet-class>com.github.zxbu.webdavteambition.store.StartupService</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>WebdavServlet</servlet-name>
<servlet-class>net.sf.webdav.WebdavServlet</servlet-class>
Expand Down

0 comments on commit 1e1b7e3

Please sign in to comment.