"org.b3log.latke.cron.Cron import org.b3log.latke.servlet.HTTPRequestMethod; import org.b3log.latke.urlfetch.HTTPRequest; import org.b3log.latke.urlfetch.URLFetc .."

Latke CodeView - 2 Cron

本贴最后更新于 903 天前,其中的信息可能已经时移世易

org.b3log.latke.cron.Cron

import org.b3log.latke.servlet.HTTPRequestMethod;
import org.b3log.latke.urlfetch.HTTPRequest;
import org.b3log.latke.urlfetch.URLFetchService;
import org.b3log.latke.urlfetch.URLFetchServiceFactory;
public final class Cron extends TimerTask {
    public static final int TEN = 10;
    public static final int SIXTY = 60;
    public static final int THOUSAND = 1000;
    private String url;
    private String description;
    private String schedule;
    private long period;
    public Cron(final String url, final String description, final String schedule) {
        this.url = url;
        this.description = description;
        this.schedule = schedule;
        parse(schedule);
    }
    public void run() {
        final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
        final HTTPRequest request = new HTTPRequest();
        try {
            request.setURL(new URL(url));
            request.setRequestMethod(HTTPRequestMethod.GET);
            urlFetchService.fetchAsync(request);
        } catch (final Exception e) {
        }
    }
    private void parse(final String schedule) {
        final int num = Integer.valueOf(StringUtils.substringBetween(schedule, " ", " "));
        final String timeUnit = StringUtils.substringAfterLast(schedule, " ");
        if ("hours".equals(timeUnit)) {
            period = num * SIXTY * SIXTY * THOUSAND;
        } else if ("minutes".equals(timeUnit)) {
            period = num * SIXTY * THOUSAND;
        } else if ("seconds".equals(timeUnit)) {
            period = num * THOUSAND;
        }
    }
    public long getPeriod() {
        return period;
    }
    public String getDescription() {
        return description;
    }
    public String getSchedule() {
        return schedule;
    }
    public String getURL() {
        return url;
    }
    public void setURL(final String url) {
        this.url = url;
    }
}

org.b3log.latke.cron.CronService

import org.b3log.latke.Latkes;
import org.b3log.latke.RuntimeEnv;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public final class CronService {
    private static final List<Cron> CRONS = new ArrayList<Cron>();
    private static final List<Timer> TIMERS = new ArrayList<Timer>();
    public static void start() {
        shutdown();
        final RuntimeEnv runtimeEnv = Latkes.getRuntimeEnv();
        try {
            switch (runtimeEnv) {
                case LOCAL:
                    loadCronXML();
                    for (final Cron cron : CRONS) {
                        cron.setURL(Latkes.getServer() + Latkes.getContextPath() + cron.getURL());
                        final Timer timer = new Timer();
                        TIMERS.add(timer);
                        timer.scheduleAtFixedRate(cron, Cron.TEN * Cron.THOUSAND, cron.getPeriod());
                    }
                    break;
                default:
                    throw new RuntimeException("Latke runs in the hell.... Please set the enviornment correctly");
            }
        } catch (final Exception e) {
            throw new IllegalStateException(e);
        }
    }
    public static void shutdown() {
        CRONS.clear();
        for (final Timer timer : TIMERS) {
            timer.cancel();
        }
        TIMERS.clear();
    }
    private static void loadCronXML() {
        final File cronXML = Latkes.getWebFile("/WEB-INF/cron.xml");
        if (null == cronXML || !cronXML.exists()) {
            return;
        }
        final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        try {
            final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            final Document document = documentBuilder.parse(cronXML);
            final Element root = document.getDocumentElement();
            root.normalize();
            final NodeList crons = root.getElementsByTagName("cron");
            for (int i = 0; i < crons.getLength(); i++) {
                final Element cronElement = (Element) crons.item(i);
                final Element urlElement = (Element) cronElement.getElementsByTagName("url").item(0);
                final Element descriptionElement = (Element) cronElement.getElementsByTagName("description").item(0);
                final Element scheduleElement = (Element) cronElement.getElementsByTagName("schedule").item(0);
                final String url = urlElement.getTextContent();
                final String description = descriptionElement.getTextContent();
                final String schedule = scheduleElement.getTextContent();
                CRONS.add(new Cron(url, description, schedule));
            }
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
    private CronService() {
    }
}

  • Latke

    Latke 是一款以 JSON 为主的 Java Web 框架。

    48 引用 • 357 回帖 • 507 关注
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    2175 引用 • 7387 回帖 • 1037 关注
  • 定时任务
    9 引用
感谢    关注    收藏    赞同    反对    举报    分享
回帖    
请输入回帖内容...