Programs that are executed at the Java Web App startup interval

Source: Internet
Author: User

Reference: "Java timer timer.schedule perform tasks and timings in the Web" "Java" timer and TimerTask detailed "

Do a demo to complete the following functions:

Having tomcat start a thread pool and a timer thread at the start of the Web program, the timer thread runs once at a time (for example, checking for expired users, like polling), and then triggers another background thread (thrown to the thread pool for processing, such as processing expired users, sending, etc.).

Speak directly in code

Staruplistener.java

Package Listeners;
Import Java.util.Timer;
Import java.util.concurrent.Executors;
Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;
Import Backstage.mytimertask;
Import Support.poolmanager;
@Override
Sce.getservletcontext (). log (" timer Destruction");
@Override
Sce.getservletcontext (). log (" boot thread Pool");
Start a thread pool to deal with different task;
Poolmanager.pool = Executors.newfixedthreadpool (10);
Sce.getservletcontext (). log (" start Timer");
Timer timer=New timer (true); 
Timer.schedule (
Sce.getservletcontext (). log ("The task schedule has been added");
}

Mytimetask.java

Package BackStage;
Import Java.util.TimerTask;
Import Javax.servlet.ServletContext;
Import Support.poolmanager;
Null
           int  param;
This.context = context;
@Override
Context.log (" start execution of specified tasks");
System.err.println (" run every 10 seconds!") I'm going to call the thread pool to perform another task ");
PoolManager.pool.execute (new Workthread (param++));
Context.log (" specified task execution end");
}

Poolmanager.java

Package support;
Import Java.util.concurrent.ExecutorService;
static Executorservice pool;
Package BackStage;
int param;
This.param = param;
@Override
TODO do something
System.err.println (" I am called by the thread pool to execute!") Parameters: "+ param);
}

Web. XML fragment

<listener> 
listener-class>listeners.startuplistener</listener-class>    
Listener>

Output: (I like to use system.err output red!) More festive ╮(╯▽╰)╭)

Info: Start execution of specified scheduled tasks
Claiming to run every 10 seconds Oh! I'm going to call the thread pool to perform another task.
2011-3-31 18:26:46 Org.apache.catalina.core.ApplicationContext Log
Info: Specify the end of the scheduled task execution
I was called by the thread pool to execute! Parameters: 1
2011-3-31 18:26:56 Org.apache.catalina.core.ApplicationContext Log
Info: Start execution of specified scheduled tasks
Claiming to run every 10 seconds Oh! I'm going to call the thread pool to perform another task.
I was called by the thread pool to execute! Parameters: 2
2011-3-31 18:26:56 Org.apache.catalina.core.ApplicationContext Log
Info: Specify the end of the scheduled task execution
2011-3-31 18:27:06 Org.apache.catalina.core.ApplicationContext Log
Info: Start execution of specified scheduled tasks
Claiming to run every 10 seconds Oh! I'm going to call the thread pool to perform another task.
2011-3-31 18:27:06 Org.apache.catalina.core.ApplicationContext Log
Info: Specify the end of the scheduled task execution
I was called by the thread pool to execute! Parameters: 3

Programs that are executed at the Java Web App startup interval

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.