Tutorials to .com

Tutorials to .com » Jsp » Xml » Web applications, how to start the background task

Web applications, how to start the background task

Print View , by: iSee ,Total views: 9 ,Word Count: 739 ,Date: Mon, 20 Apr 2009 Time: 10:20 PM

Abstract <br> <br> our Web applications often need to write their own services, the purpose of this paper is to provide you with a solution.

<br> <br> Principle tenets of this program is to write an implementation of the interface ServletContextListener of these two methods: public void contextInitialized (ServletContextEvent sce), it is time to start calling in the application; Another option is to: public void contextDestroyed (ServletContextEvent sce), the method is called in the application of the end. The background we have to start the application logic on the way to achieve contextInitialized; the release of back-office applications on the occupied resources to deal with contextDestroyed. However, we start the background task is often required, such as time, frequency and so on, I used a open-source components: quartz.

Step 1. Write business call categories:

/ / DumbJob.java
import org.quartz .*;
import java.util .*;
public class DumbJob implements Job (
public DumbJob () (
public void execute (JobExecutionContext context)
throws JobExecutionException
/ / Write in the business processing code here. What, you do not know? Then you do not ask me! ! : - <

The primary function of this type by the quartz in the types of scheduling rules in accordance with the specified call to implement the necessary business logic.

2. Write scheduling category / / TestShedule.java
import org.quartz .*;
import java.util .*;

public class TestShedule (
static SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory ();
static Scheduler sched;

public static void run () throws Exception (
sched = schedFact.getScheduler (); / / access Scheduling Manager JobDetail jobDetail = new JobDetail ( "myJob",
DumbJob.class); / / create work CronTrigger trigger = new CronTrigger ( "myTrigger", "test", "0 / 10 * * * * ?");// to create triggers sched.scheduleJob (jobDetail, trigger); / / added to the Scheduling Manager to start scheduling sched.start ();// Manager)

public static void stop () throws Exception (
sched.shutdown ();
The purpose of this rule is set to call, where I used the "0 / 10 * * * *?" That every 10 seconds on the implementation of the first, the description of regular expressions see the api documentation quartz.

3. To start the preparation of service categories:

/ / ServiceLoader.java
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ServiceLoader implements ServletContextListener (
public void contextInitialized (ServletContextEvent sce) (
try (
TestShedule.run ();
) catch (Exception ex) (
System.out.println (ex.getMessage ());

public void contextDestroyed (ServletContextEvent sce) (
try (
TestShedule.stop ();
) catch (Exception ex) (
System.out.println (ex.getMessage ());
ContextInitialized call in TestShedule.run () to start the background task; contextDestroyed call in TestShedule.stop () to stop the background task.

4. The deployment of services to start type <br> <br> in the web.xml file to add the following line to configure:
<? xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE web-app PUBLIC "- / / Sun Microsystems, Inc. / / DTD Web Application 2.3 / / EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name> root </ display-name>
<! - The bottom part is the listener configuration information ->
<listener-class> ServiceLoader </ listener-class>
</ listener>
<welcome-file> index.html </ welcome-file>
</ welcome-file-list>
</ web-app>

5. Start your web services.

In fact, summing up <br> <br> way to achieve this functionality a lot, I applied here ServletContextListener interface and open source api quartz, hope you can help the development.

Java and XML Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.