我正在开发JSP / Servlets App,我想在特定时间执行服务,例如: For each day at 10:00 AM , delete any attachment from the “attachment” table in the database where column X== NULL. 如何在JSP / Servlets应用程序中执行
For each day at 10:00 AM , delete any
attachment from the “attachment” table
in the database where column X== NULL.
如何在JSP / Servlets应用程序中执行此操作?
我使用Glassfish作为服务器.
> Use Quartz scheduler library,或
>执行像folliwng这样的黑客攻击:
实现ServletContextListener;在contextInitialized方法中:
ServletContext servletContext = servletContextEvent.getServletContext(); try{ // create the timer and timer task objects Timer timer = new Timer(); MyTimerTask task = new MyTimerTask(); //this class implements Callable. // get a calendar to initialize the start time Calendar calendar = Calendar.getInstance(); Date startTime = calendar.getTime(); // schedule the task to run hourly timer.scheduleAtFixedRate(task, startTime, 1000 * 60 * 60); // save our timer for later use servletContext.setAttribute ("timer", timer); } catch (Exception e) { servletContext.log ("Problem initializing the task that was to run hourly: " + e.getMessage ()); }
编辑web.xml以引用侦听器实现:
<listener> <listener-class>your.package.declaration.MyServletContextListener</listener-class> </listener>