Java timer web)

Source: Internet
Author: User

It is common to execute tasks by using a timer. The following is a simple example:

The listener, timertask, and timer classes are used for implementation. In addition, Spring provides advanced timer implementation, which seems to be quarts.

Package Com. Rx. timer;

ImportJava. util. date;
ImportJava. util. timertask;

Public   Class Mytask Extends Timertask {
@ Override
Public   Void Run (){
System. Out. println (
" Call "   + ( New Date ()));
}
}
 

PackageCom. Rx. timer;

ImportJava. util. timer;
ImportJavax. servlet. servletcontextevent;
ImportJavax. servlet. servletcontextlistener;

Public ClassMylistenImplementsServletcontextlistener {

PrivateTimer= Null;

@ Override
Public VoidContextdestroyed (servletcontextevent arg0 ){
Timer. Cancel ();
}

@ Override
Public   Void Contextinitialized (servletcontextevent arg0 ){
Timer
=   New Timer ( True );
// Set the task schedule, start time, And Interval
Timer. Schedule ( New Mytask (), 0 , 1000   *   10 );
}
}

 

 

 

<? XML version = "1.0" encoding = "UTF-8" ?>  
< Web-app ID = "Webapp_id" Version = "2.4" Xmlns = "Http://java.sun.com/xml/ns/j2ee" Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance" Xsi: schemalocation = "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >  
< Display-name >  
Webtimer
</ Display-name >  

< Listener >  
< Listener-class > Com. Rx. Timer. mylisten </ Listener-class >  
</ Listener >

welcome-file-list >
welcome-file > index. JSP welcome-file >
welcome-file-list >
Web-app >

 

 

Well, another complicated one! It is basically the same as above, but it is more detailed in task execution control.

 

Public Class Workserviceimpl Implements Workservice, servletcontextlistener {

Public VoidContextdestroyed (servletcontextevent arg0 ){
Timer. Cancel ();
System. Out. println ("timer destroyed ");
}

Public void contextinitialized (servletcontextevent event) {
timer = New JAVA. util. timer ( true );
sampletask = New sampletask (event. getservletcontext ();
system. out. println ("timer started");
timer. schedule (sampletask, 0, 60x60*1000);
system. out. println ("task scheduling table added");
}< BR >}

ClassSampletaskExtendsTimertask {

Private Servletcontext context;
Private Static Boolean Isrunning = False ; // Whether it is running
Private Static Boolean Flag =True ; // Never run once
Private Static Final Int C_schedule_hour = 23; // running time
Private Workserviceimpl workservice;

PublicSampletask (servletcontext context ){
This. Context = context;
}

Public Void Run (){
Workservice = New Workserviceimpl ();
Calendar Cal = calendar. getinstance ();
If (! Isrunning ){
If (C_schedule_hour = Cal. Get (calendar. hour_of_day) & flag ){
Isrunning = True ;
Workservice. autoworkoff ();
Isrunning = False ;
Flag = False ;
Context. Log ("End of task execution ");
}
} Else {
Context. Log ("the last task has not been completed ");
}
If (C_schedule_hour! = Cal. Get (calendar. hour_of_day )){
Flag = True ;
}
}
}

 

Configure in Web. XML as follows:
<Listener>

<Listener-class> com.css. WAM. Service. impl. workserviceimpl </listener-class>

</Listener>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.