Use Quartz Enterprise Planning Scheduler (1) in the J2EE environment (1)

zhaozj2021-02-16  53

Quartz is an enterprise work plan scheduler from an development source code from OpenSymphony. To learn more and download quartz, please visit http://www.quartzscheduler.org/quartz/. You can use Quartz to schedule work programs in your J2EE app such as EJB. This article will introduce in your J2EE app

what

Use Quartz to schedule a work plan. Oracle Application Server will be used in the article

10g

J2EE container (OC4J

9.0.4

Examples of the J2EE container.

Quartz supports multiple types of work and triggers, but the most popular is the trigger of Klon (Time Unit: Million Year) level. To learn about the compatibility of Quartz Work Plan Scheduling, please refer to Quartz's documentation: http://www.quartzscheduler.org/quartz/docs.html. In addition, DEJAN BOSANAC also wrote a very good article Job Scheduling in Java may help you.

Before we enter the details topic, assume that you have a business case, you need to let a work run every 30 minutes. In this article we will discuss

what

This is done using Quartz's gram-level trigger function.

1 Define your work as an EJB method

The first step in using the planned task in J2EE application is to create EJB and encapsulate business logic into EJB. For example, I created a stateless EJB named Testejb, one of which is called YourMethod, I need to be defined as a planned task. In order to be clearer, I will listen to my EJB code snippet and EJB deployment description:

Package howto.quartz.ejb;

Import java.rmi. *;

Import javax.ejb. *;

Public Class Testejbbean Implements SessionBean {

Public Testejbbean () {

}

// EJB Life Cycle Methods Are OMITIted for BREVITY

........

Public void yourmethod () throws remoteException {

System.out.println ("TESTEJB JOB");

}

}

2 Use the Quartz API from a usual servlet to customize your planned task

Quartz uses your own thread pool that is not a container thread. The Servlet API allows the user thread and because you need to create a servlet and use the Quartz API to schedule the planned task. Quartz provides the entrance to QuartzinitializerServlet as its work plan service. In this example we need to submit Testejb's YourMethod method as a task. So we will create a GenericServlet name to howto.quartz.Servlet.quartzServlet, and submit the EJB method as a gram trigger in the init () method. In this example, I set the Croolion Expression to initialize the parameters instead of using the way to encode in servlet. Below is the code of the servlet:

Public class quartzservlet extends genericServlet {

Public void init (servletconfig config) throws servletexception {

Super.init (config);

System.out.println ("Scheduling Job ..");

JobDetail JD = New Jobdetail ("Test Quartz", "My Test Job", EJBINVOKERJOB.CLASS; JD.GetJobDataMap (). PUT ("EJB", "Java: Comp / Env / Ejb / Testejb");

Jd.getJobDataMap (). PUT ("Method", "YourMethod");

Object [] jdargs = new object [0];

Jd.getjobdataMap (). PUT ("args", jdargs;

Crontrigger crontrigger = New crontrigger ("Test Quartz", "Test Quartz");

Try {

String cronexpr = NULL;

// Get the cron expression as an init parameter

cronexpr = GetInitParameter ("cronexpr");

System.out.println (cronexpr);

Crontrigger.setCronExpression (cronexpr);

Scheduler Sched = stdschedulerfactory.getDefaultscheduler ();

Sched.SCHEDULEJOB (JD, crontrigger);

System.out.println ("Job Scheduled Now ..");

} catch (exception e) {

E.PrintStackTrace ();

}

}

Public void service (servletRequest arg0, servletresponse arg1)

Throws servletexception, ioException {

}

Public string getServletInfo () {

Return NULL;

}

}

转载请注明原文地址:https://www.9cbs.com/read-27641.html

New Post(0)