文章分类:Java编程
今天奉命写一个监听器监听tomcat,在tomcat启动后隔半个小时生成一些要求的静态页面,因为没写过监听器,所以在网上看了一些资料后觉得不是很难就很兴奋地开始动手,
用了ServletContextListener,可没想到监听是监听到了,但是搞得tomcat启动的时候一路在监听,以为是这个监听器不适合就换成了session的,以为快成功的
时候老大又说不行(后来一想也确实不行),所以又换回ServletContextListener来继续搞,后来上网看来看去原来用这个监听器才是符合要求的,一开始那种
只不过是因为我把定时器Timer定义在了ServletContextListener的初始化函数里面了,搞得tomcat启动时老是在监听而启动不了,后来我把定时器Timer定义在
另外一个类里面,再在ServletContextListener的初始化函数里面调用就搞定了。还是没经验啊。
不过这里还有个问题:如何在这个初始化函数里面获得tomcat的IP和端口呢?也许很难,也许很easy,还不知道!
Listener:
Java代码:
public class GenPagesListener implements ServletContextListener
{
private DoSomething ds = null;
public void contextInitialized(ServletContextEvent sce) {
String rootPath = new File(sce.getServletContext().getRealPath("/")).getParentFile().toString();
if(rootPath.contains("//"))
{
rootPath = rootPath.replace("//", "/")+"/ROOT";
}
String hostPath = AppPropertyUtil.getProperty("webUrl");
GlobeContext.setRootPath(rootPath);
GlobeContext.setHostPath(hostPath);
ds = new DoSomething(1000,1800000);
}
public void contextDestroyed(ServletContextEvent sce) {
ds.destoryTimer();
}
}
==============
中间类:
public class DoSomething {
private java.util.Timer timer;
public DoSomething(int a,int b)
{
timer = new Timer();
timer.schedule(new GenPageTask(), a,b);
}
public void destoryTimer(){
timer.cancel();
}
}
==========
要执行的任务:
public class GenPageTask extends TimerTask{
public void run()
{
ApplicationContext context = GlobeContext.getApplicationContext();
IChannelManager channelManager = (IChannelManager)context.getBean("channelManager");
List<Channel> list = channelManager.getObjectList(Restrictions.eq("type", 1));
for(Channel ch : list)
{
Long lid = ch.getId();
try{
publishChannel(lid);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void publishChannel(Long lid) throws Exception
{
String root = GlobeContext.getRootPath();
String rootpath = GlobeContext.getHostPath();
ApplicationContext context = GlobeContext.getApplicationContext();
IChannelManager channelManager = (IChannelManager)context.getBean("channelManager");
ChannelView chan = channelManager.getView(lid);
String puppath = root + chan.getFullPath();
File file = new File(puppath);
if (!file.exists()) {
file.mkdirs();
}
int pageSize = Integer.valueOf(AppPropertyUtil
.getProperty("listPageSize"));
if (chan.getPageSize() != null) {
pageSize = chan.getPageSize();
}
try {
List<Criterion> cris = new ArrayList<Criterion>();
cris.add(Restrictions.eq("channelId", chan.getId()));
cris.add(Restrictions.eq("struts", 1));
IDocumentManager documentManager = (IDocumentManager)context.getBean("documentManager");
int totalPage = documentManager.getPageObject(0, pageSize, cris)
.getTotalPage();
for (int page = 1; page <= totalPage; page++) {
String index = "";
if (page == 1) {
index = "index";
} else {
index = "list_" + page + "";
}
String url = rootpath
+ "admin/channel/docList.htm?data.channelId="
+ chan.getId() + "&data.page=" + page;
HtmlGenUtil.writeHtml(puppath + index + ".shtml", HtmlGenUtil
.getHtmlCode(url), "YES");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}