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.
95 lines
3.4 KiB
95 lines
3.4 KiB
package com.fr.plugin.xxxx.bi.sync; |
|
|
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
|
import com.fr.json.JSONObject; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.plugin.context.PluginContexts; |
|
import com.fr.plugin.xxxx.bi.sync.config.SyncConfig; |
|
import com.fr.plugin.xxxx.bi.sync.schedule.SyncThread; |
|
import com.fr.plugin.transform.FunctionRecorder; |
|
import com.fr.scheduler.QuartzContext; |
|
import com.fr.scheduler.ScheduleJobManager; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.stable.fun.Authorize; |
|
import com.fr.third.v2.org.quartz.CronScheduleBuilder; |
|
import com.fr.third.v2.org.quartz.JobKey; |
|
import com.fr.third.v2.org.quartz.SchedulerException; |
|
import com.fr.third.v2.org.quartz.TriggerBuilder; |
|
import com.fr.web.utils.WebUtils; |
|
|
|
import javax.servlet.FilterChain; |
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
|
|
/** |
|
* @Author fr.open |
|
* @Date 2020/9/10 |
|
* @Description |
|
**/ |
|
@FunctionRecorder |
|
@Authorize(groupSignKey = Constants.PLUGIN_ID) |
|
public class SyncFilter extends AbstractGlobalRequestFilterProvider { |
|
|
|
@Override |
|
public String filterName() { |
|
return "bi-sync"; |
|
} |
|
|
|
@Override |
|
public String[] urlPatterns() { |
|
initSchedule(); |
|
return new String[]{"/decision/bi-sync/reset"}; |
|
} |
|
|
|
@Override |
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
|
SyncConfig config = SyncConfig.getInstance(); |
|
try { |
|
if (!config.configed()) { |
|
WebUtils.printAsJSON(res, JSONObject.create().put("mess", "config is not configed").put("state", 0)); |
|
return; |
|
} |
|
initSchedule(); |
|
WebUtils.printAsJSON(res, JSONObject.create().put("mess", "reset successful").put("state", 1)); |
|
} catch (Exception e) { |
|
LogUtils.error(e.getMessage(), e); |
|
} |
|
} |
|
|
|
|
|
public static void initSchedule() { |
|
JobKey jobKey = new JobKey("syncBIRole", "syncGroup"); |
|
try { |
|
if (QuartzContext.getInstance().getScheduler().checkExists(jobKey)) { |
|
ScheduleJobManager.getInstance().removeJob(jobKey.getName(), jobKey.getGroup()); |
|
} |
|
} catch (SchedulerException e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
if (PluginContexts.currentContext().isAvailable()) { |
|
addSchedule(jobKey); |
|
} else { |
|
LogUtils.error("插件已经被禁用或过期"); |
|
} |
|
} |
|
|
|
private static void addSchedule(JobKey jobKey) { |
|
Map<String, Object> param = new HashMap(); |
|
SyncConfig config = SyncConfig.getInstance(); |
|
TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger(); |
|
triggerBuilder.forJob(jobKey.getName(), jobKey.getGroup()).withIdentity(jobKey.getName(), jobKey.getGroup()).startNow(); |
|
if (StringUtils.isBlank(config.getCron())) { |
|
LogUtils.error("cron is null schedule start failed"); |
|
return; |
|
} |
|
CronScheduleBuilder schedule = CronScheduleBuilder.cronSchedule(config.getCron()); |
|
triggerBuilder.withSchedule(schedule); |
|
try { |
|
ScheduleJobManager.getInstance().addJob(jobKey.getName(), jobKey.getGroup(), "sync job", SyncThread.class, triggerBuilder.build(), param); |
|
} catch (Exception e) { |
|
LogUtils.error(e.getMessage(), e); |
|
} |
|
} |
|
}
|
|
|