Joke Collection Website - Cold jokes - Explain in detail how Shi Ying went from entry to mastery.

Explain in detail how Shi Ying went from entry to mastery.

Quartz is an open source job scheduling framework, which is completely written in Java and designed for J SE and J EE applications. It provides great flexibility without sacrificing simplicity. You can use it to create simple or complex job execution plans. It has many features, such as database supporting EJB jobs of cluster plug-ins, pre-built JavaMail and other expressions similar to cron.

Have you ever needed an application to perform a task? This task is automatically executed every day or every Tuesday night or the last day of every month without intervention. If a serious error occurs during execution, can the application know that its execution failed and try to execute it again? Are you and your team programmed in Java? If you answer yes to any of these questions, then you should use Quartz scheduler.

At present, Shi Ying is widely used in matrices, for example, to realize the function of ranking statistics. In Jmatrix, Quartz defines a scheduling job, which starts at 1 am every day, re-counts everyone's karma and ranking, and generates RSS files. Quartz also defines a job to generate RSS XML files every half hour.

Quartz makes job scheduling simple.

Quartz is an open source job scheduling framework written entirely by Java. Don't let the term homework plan scare you. Although Quartz framework integrates many extra functions, in its simple form, you will find that it is simply unbearable to use! Simply creating a Java class job interface that implements the quartz job interface contains unique methods.

Add some logic to the execute () method in the job interface implementation class. Once you configure the job implementation class and set the scheduling plan, Quartz will pay close attention to the remaining time. When the scheduler determines that you are notified of a job, Quartz framework will call execute () on your job implementation class. Method and allow it to do what it should do without reporting anything to the scheduler or calling anything specific, just executing the task and ending it. If you configure the job to be called again later, the Quartz framework will call it again at an appropriate time.

If you use other popular open source frameworks, such as struts, you will be satisfied with the design and components of Quartz. Although the two open source projects solve completely different problems, there are still many similarities, that is, users of open source software feel comfortable every day. Quartz can be used as an independent J SE application, RMI server, web application and even J2EE application server.

The Development History of Shi Ying

Although Shi Ying only started to attract people's attention this year, it is still popular for the time being. Quartz was founded by James House and first joined the sourcefe project in the spring of 2008. In the following years, many new functions and versions appeared, but it was not until the project moved to a new location and became a member of the OpenSymphony project family that it really started and got the attention it deserved.

James House is still involved in a lot of development work with several amateur developers who assist him. The Quartz development team can release several new versions this year, including the version currently in the release candidate stage.

Introduction to Shi Ying

The Quartz project is located on the OpenSymphony website, where you can find many useful resources. JavaDocs contains guide documents, CVS visits user and developer forums, and of course downloads.

Get the released version of Quartz from the download connection and extract it to the local directory. This download file contains a pre-built quartz jar, which you can put into your own application. Quartz framework only needs a few third-party libraries, and these third-party libraries are necessary. You may already be using these libraries.

You should add the third-party libraries in the /lib/core and /lib/optional directories of the Quartz installation directory to your own projects. Most third-party libraries are standard Jakarta Commons libraries that we are familiar with and like, such as Commons Logging Commons Beantuils and so on.

Quartz properties file

Quartz has a configuration file called quartz properties that allows you to modify the framework runtime environment. By default, the Quartz property file in quartz jar is used. Of course, you should create a copy of the quartz property file and put it in the class directory of the project so that the class loader can find it.

Once the Quartz jar file and third-party libraries are added to your own project, and the Quartz property file is located in the classes directory of the project, you can create jobs. However, before doing this, let's briefly discuss the Shi Ying framework for the time being.

Shi Ying internal structure

In terms of scale, Quartz is similar to most open source frameworks, with about 10 Java classes and interfaces organized into packages, which can be compared with Apache Struts with about 10 classes and interfaces organized into packages. Although scale can hardly be used as a feature to measure the quality of a framework, the key here is whether Quartz contains many functions, and whether the function set becomes or should become a factor to judge the quality of an open source or non-open source framework.

Shi Ying scheduler

The core of the Quartz framework is the scheduler, which is responsible for managing the runtime environment of Quartz applications. The scheduler itself does not do all the work, but relies on some very important components in the framework. Quartz is more than just thread and thread management. In order to ensure scalability, Quartz adopts a multi-thread architecture.

At startup, the framework initializes a set of worker threads, which are used by the scheduler to execute scheduled jobs. This is the principle that Quartz can run multiple tasks at the same time. Quartz relies on a loosely coupled thread pool management component to manage the thread environment. In this article, we will mention thread pool management many times, but every object in Quartz is configurable or customizable, so, for example, if you want your own thread pool management tool, I think you can!

homework

In Quartz jargon, a job is a simple Java class that performs a task. The task can be any Java code, as long as the quartz job interface is implemented and the JobExecutionException is thrown when a serious error occurs.

The job interface contains only one method, Execute (). Once the job interface and the execute () method are implemented, Quartz will call your job when determining when the job will run. The execute () method is exactly what you want to do. Here are some examples of what you want to do at work.

Use JavaMail (or use other mail frameworks, such as Commons Net) to send mail.

Create a remote interface and call the method on EJB.

Get Hibernate session to query and update data in relational database.

Use OSWorkflow and call the workflow from the job.

Use FTP and move files.

Call the Ant build script to start the planned build.

This possibility is endless business. This infinite possibility makes the framework so powerful. Quartz provides a mechanism for you to build repeatable schedules with different granularity, so you only need to create a Java class and call it to perform tasks.

Job management and storage

Once a job is scheduled, the scheduler needs to remember and track these jobs and their execution time. This is not very useful if your work is called in a few minutes or every second. In fact, job execution needs to call the execute () method Quartz on the scheduled job very accurately and immediately, and store and manage the job through a concept called JobStore.

Effective working storage

Quartz provides two basic types of job storage. The first type, called RAMJobStore, uses ordinary memory to store scheduler information. This type of job storage is the easiest to configure, build and run, and is sufficient for many applications.

However, because the scheduler information is stored in the memory allocated to the JVM, all the scheduler information will be lost when the application stops running. If you need to save scheduling information between restarts, you will need a second type of job storage.

The second type of job store actually provides two different implementations, but both implementations are usually called JDBC job stores. Both JDBC job stores need JDBC drivers and background databases to store scheduler information. The difference between these two types is whether you want to control database transactions or release control to application servers such as WebLogic or Jboss of BEA (this is similar to the difference between Bean-managed transactions and container-managed transactions in J2EE fields). These two JDBC job shops are

You can use Jobstoretx when you want to control transactions or work in a non-application server environment.

You can use Jobstorecmt when you work in an application server environment and want a container to control transactions.

JDBC Job Store is designed for users who need scheduler to maintain scheduling information.

Jobs and triggers

Quartz designers made a design choice to separate work from scheduling. The trigger in Quartz is used to tell the scheduler when the job is triggered. The framework provides a trigger type, but the two most commonly used types are SimpleTrigger and CronTrigger SimpleTrigger, which are designed for simple scheduling.

In general, if you need to light a job at a given time and number of repetitions or the number of seconds to wait between lighting, then SimpleTrigger is for you. On the other hand, if you have many complicated job plans, you may need CronTrigger.

CronTrigger is a calendar-like schedule. When you need to do half a homework every morning except Saturday and Sunday, you should use CronTrigger. As the name implies, CronTrigger is based on Unix clone expressions.

For example, the following Quartz clone expression will execute a job every morning from Monday to Friday.

? * Monday to Friday

The following expression

? * L

Homework will be performed at 8: 00 am on the last Friday of each month, from to. You can't use SimpleTrigger to do these things. You can use any one, but which one is suitable depends on your scheduling needs.

arrange work

Let's get into the actual discussion by looking at an example. Now suppose you manage a department. Every time a customer stores a file on his FTP server, he must inform it by email, and our job will log in to the remote server through FTP and download all the found files.

Then, it will send an email containing the number of files found and downloaded. This work can easily help people get rid of manually performing this task all day, even at night. We can set the job cycle to check once every second and work in X mode. This is the whole purpose of the Shi Ying Framework.

First, create a job class to perform FTP and email logic. The following example shows the Quartz job class, which implements the quartz job interface.

We intend to keep ScanFTPSiteJob simple. For this example, we created a utility class called JobUtil. It is not a part of Quartz, but it makes sense to build a utility library that can be reused by all kinds of work. We can easily organize this code into a job class and use it like Quartz scheduler. Because we have been using Quartz, those codes can continue to be reused.

The parameters used by the jobutil check for files () and jobutil send email () methods are instances of JobDataMap created by Quartz, which are created for the execution of each job. This is a way to pass configuration parameters to the job class.

The implementation of JobUtil is not shown here, but we can easily implement FTP and Email functions with Commons Net on Jakarta.

Call a job using the scheduler

First, create a job, but in order for the scheduler to call the job, you must explain to the scheduler when and how often the job is called. This is done by job-related triggers. Because we are only interested in calling jobs in cycles every second, we intend to use SimpleTrigger.

Jobs and triggers are scheduled through the Quartz scheduler interface. We need to get an instance of the scheduler from the scheduler factory class. The simplest method is to call the static method getDefaultScheduler () on the StdSchedulerFactory class.

Using the Quartz framework, you need to call the start () method to start the scheduler. The code of this example follows the general pattern of most Quartz applications, creating one or more jobs, creating and setting triggers, scheduling jobs with a scheduler, and starting the scheduler with triggers.

Programming scheduling is the same as declarative scheduling.

We schedule our ScanFTPSiteJob through programming, that is, we use Java code to set jobs and triggers. The Quartz framework also supports declarative settings for job scheduling in xml files. Declarative methods allow us to modify which job executes faster.

The Quartz framework has a plug-in that reads the xml configuration file. The xml configuration file contains information about launching Quartz applications and triggers. All jobs in the xml file are added to the scheduler along with related triggers. You still need to write job classes, but the scheduler that configures these job classes is very dynamic. You can compare the elements in the xml file with the sample code. Conceptually, they are However, the advantage of using declarative methods is that maintenance becomes very simple. Just change the xml configuration file and restart the Quartz application without modifying the code, recompiling or redeploying.

Stateful and Stateless Jobs

In this article, the job you see is stateless, which means that the state changes of JobDataMap are not maintained between job executions. If you need to add or delete the value of JobDataMap and let the job see this state change next time, you need to use Quartz stateful job.

If you are an experienced EJB developer, I believe you will shrink back immediately, because declarative has a negative meaning, which is mainly due to the scalability problems brought by EJB. Quartz StatefulJob implements quartz stateful job interface.

The main difference between stateless jobs and stateful jobs is that stateful jobs have only one instance at each execution. In most cases, stateful jobs will not cause big problems. However, if you have a job that needs to be executed frequently or takes a long time to complete, stateful jobs may bring you scalability problems.

Other features of the Shi Ying framework.

Quartz framework has a rich feature set. In fact, Quartz has too many functions to understand all the functions in one situation. Here are some interesting features, but there is no time to discuss them in detail here.

Listeners and plug-ins

Everyone likes listening to songs and plug-ins. Today, almost any open source framework can be downloaded. You will find that the listening that supports these two concepts is a Java class that you created yourself. When a critical event occurs, you will receive a callback from the framework. For example, these can be set to notify your listeners when a job is scheduled without scheduling, or when the trigger terminates and no longer triggers. Quartz framework includes scheduler listening job and trigger monitoring. You can configure job and trigger monitoring as global monitoring or job and trigger specific monitoring.

Once a specific monitor is called, you can use this technology to do what you want in the monitor class. For example, if you want to send an e-mail at the end of each job, you can write this logic to the job or to JobListener. Forced loose coupling is beneficial to better design.

The Quartz plug-in is a new feature that can be created and added to the Quartz framework without modifying the Quartz source code. It is designed for developers who want to extend the Quartz framework but have no time to submit changes to the Quartz development team and wait for a new version. If you are familiar with Struts plug-ins, you can fully understand the use of Quartz plug-ins.

Instead of Quartz providing limited extension points that can't meet your needs, it is better to have a modifiable extension point by using plug-ins.

Cluster Shi Ying application

Whether Quartz applications can be clustered horizontally or vertically depends on your own needs. Clustering provides the following benefits.

Measurability

Participation availability

load balancing

At present, Quartz can only support future versions of the cluster with the help of relational database and JDBC job storage. This restriction will disappear, and RAMJobStore cluster can be used without database support.

Quartz web application

After using this framework for several weeks or months, one of the demands of Quartz users is to integrate Quartz into the graphical user interface. At present, the Quartz framework has some tools that allow you to initialize and start Quartz with Java servlet. Once the scheduler instance can be accessed, it can be stored in the ServletContext of the web container, and the scheduling environment can be managed through the scheduler interface.

Fortunately, some developers are already influencing the stand-alone Quartz web application, which is used to better manage the scheduler environment. Graphical user interfaces built on several popular open source frameworks (such as Struts and Spring) support many functions, all of which are packaged into a simple interface.

Shi Ying's next plan.

Quartz is an active project. The Quartz development team made it clear that the next major version of Quartz has been launched. You can experience the design and function of Quartz on OpenSymphony's wiki. In short, users of Quartz are free to add functional suggestions and design ideas every day so that they can be considered (valued) by the core framework.

Learn more about Quartz features

Lishi Xinzhi/Article/program/Java/ky/20 13 1 1/27892