import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.you9.base.WorkQueue;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class RanksServlet extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
private boolean isSyn = true;
//Initialize global variables
public void init() throws ServletException
{
System.out.println("start RanksServlet...");
super.init();
RequestTask task = new RequestTask();
task.start();
}
/**
* 定时启动器
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
private class RequestTask extends Thread
{
public void run()
{
try
{
int INIT_SLEEP_INTERVAL = 10 * 1000; //首次启动时睡眠10S后再启动
int EXEC_INTERVAL = 15 * 60 * 1000; //每15分钟运行一次,更新数据
sleep(INIT_SLEEP_INTERVAL);
while (true)
{
syncData();
sleep(EXEC_INTERVAL);
}
}
catch (Exception ex4)
{
ex4.printStackTrace();
}
}
}
/**
* 同步或异步调用更新Ranks的方法
*/
private void syncData()
{
if (isSyn)
{
//同步调用
reload();
}
else
{
//异步调用
WorkQueue.getInstance().excute(new LoadRanksData());
}
}
class LoadRanksData implements Runnable
{
public void run()
{
reload();
}
}
/**
* 更新ranks的数据
*/
private void reload()
{
int ranksNum = 31;
for (int i = 0; i <= ranksNum; i++)
{
Ranks ranks = RanksFactory.getInstance().getRanks(i);
ranks.getRanks(null);
}
}
//Clean up resources
public void destroy()
{
}
}