Java tutorial
/* * @(#)Program.java 2014-12-10 ?10:35:19 proxy_pool Copyright 2014 Isoftstone, * Inc. All rights reserved. ISOFTSTONE PROPRIETARY/CONFIDENTIAL. Use is subject * to license terms. */ package com.isoftstone.proxy; import java.io.IOException; import java.net.URI; import java.util.Calendar; import java.util.Date; import javax.ws.rs.core.UriBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; import org.glassfish.jersey.server.ResourceConfig; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import com.isoftstone.proxy.api.http.HttpProxyServer; import com.isoftstone.proxy.check.CheckProxyJob; import com.isoftstone.proxy.check.ParseProxyJob; import com.isoftstone.proxy.utils.Config; /** * Program * * @author danhb * @date 2014-12-10 * @version 1.0 * */ public class Program { private static final Log LOG = LogFactory.getLog(Program.class); public static void main(String[] args) throws IOException { int insertIntervalTime = Integer.valueOf(Config.getValue("proxy_insert_minutes")); int checkIntervalTime = Integer.valueOf(Config.getValue("proxy_check_minute")); SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched; try { sched = sf.getScheduler(); // ???? JobDetail parseProxyJob = JobBuilder.newJob(ParseProxyJob.class).withIdentity("parseProxyJob", "Group") .build(); SimpleTrigger parseProxyTrigger = (SimpleTrigger) TriggerBuilder .newTrigger().withIdentity("parseProxyJob", "Group").withSchedule(SimpleScheduleBuilder .simpleSchedule().withIntervalInMinutes(insertIntervalTime).repeatForever()) .startAt(new Date()).build(); sched.scheduleJob(parseProxyJob, parseProxyTrigger); // ?? JobDetail checkProxyJob = JobBuilder.newJob(CheckProxyJob.class).withIdentity("checkProxyJob", "Group") .build(); /** * 10s?. */ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.SECOND, 10); SimpleTrigger checkProxyTrigger = (SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity("checkProxyJob", "Group").withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInMinutes(checkIntervalTime).repeatForever()) .startAt(calendar.getTime()).build(); sched.scheduleJob(checkProxyJob, checkProxyTrigger); sched.start(); String uriHost = Config.getValue("http_server_host"); URI baseUri = UriBuilder.fromUri(uriHost).port(9998).build(); ResourceConfig config = new ResourceConfig(HttpProxyServer.class); HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, config); } catch (SchedulerException e) { LOG.error("SchedulerException", e); } } }