You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
package web.util;
|
|
|
|
import db.model.bilibili.ScheduledTaskEntity;
|
|
import db.model.bilibili.TaskEntity;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.springframework.orm.hibernate5.HibernateTemplate;
|
|
import org.springframework.scheduling.TriggerContext;
|
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
import org.springframework.scheduling.config.TriggerTask;
|
|
import org.springframework.scheduling.support.CronTrigger;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.Resource;
|
|
import java.io.IOException;
|
|
import java.util.Date;
|
|
|
|
/**
|
|
* Created by reborn on 2017/5/8.
|
|
* 动态定时器任务管理类
|
|
*/
|
|
//@Component
|
|
public class DynamicTimer extends ScheduledTaskRegistrar {
|
|
|
|
private static Logger log=LogManager.getLogger();
|
|
|
|
@Resource
|
|
private HibernateTemplate hibernateTemplate;
|
|
|
|
@PostConstruct
|
|
public void init(){
|
|
hibernateTemplate.loadAll(ScheduledTaskEntity.class).forEach(this::addTriggerTask);
|
|
}
|
|
|
|
public void addTriggerTask(ScheduledTaskEntity scheduledTaskEntity) {
|
|
|
|
super.scheduleTriggerTask(new TriggerTask(()->{
|
|
TaskEntity taskEntity=scheduledTaskEntity.getTaskEntity();
|
|
if(taskEntity!=null&&taskEntity.getApi()!=null&&!taskEntity.getApi().isEmpty()) {
|
|
doGet(taskEntity.getApi());
|
|
}
|
|
},(TriggerContext triggerContext)->{
|
|
CronTrigger trigger = new CronTrigger(scheduledTaskEntity.getExpression());
|
|
log.debug(triggerContext+"");
|
|
Date nextExec = trigger.nextExecutionTime(triggerContext);
|
|
return nextExec;
|
|
}));
|
|
|
|
}
|
|
|
|
/**
|
|
* httpclient get请求封装
|
|
* @param href
|
|
* @return
|
|
*/
|
|
private CloseableHttpResponse doGet(String href){
|
|
log.debug("向地址:"+href+"发送get请求");
|
|
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
|
HttpGet httpGet = new HttpGet(href);
|
|
try {
|
|
return closeableHttpClient.execute(httpGet);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
} |