当前位置 : 主页 > 编程语言 > java >

如何使用JSP / Servlets应用程序在特定时间运行服务?

来源:互联网 收集:自由互联 发布时间:2021-06-25
我正在开发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应用程序中执行
我正在开发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应用程序中执行此操作?
我使用Glassfish作为服务器.

> EJB3 Timer service或
> 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>
网友评论