Using Quartz with the Struts Framework
The first step is to download Apache Struts and create your Web application directory structure. The Struts framework is available from the Apache Struts site, at http://struts.apache.org. You're welcome to grab the source code and build from that, although the binary download of the latest version should be sufficient.
Because Quartz does not directly depend on the Struts framework, you don't have to worry about the version of Struts that you use. Just grab the latest version that's available. You should realize, however, that the Struts and Quartz frameworks share some third-party dependencies. In fact, the required libraries we listed earlier for Quartz are all required by the Struts framework. Just be careful about mixing up different versions, as the note in the last section warned.
Creating Your Web Application Directory Structure
After you download Struts, you can create your directory structure and install the necessary files. For this example, we're going to create a fictitious Web application called Job Management Console. Because this is just a pretend application, we won't be building it to completion. Instead, we use it to explain several key points of integration with Quartz and leave the rest for you to explore. For now, assume that the boss has given us a task to build a GUI around our Job Scheduling framework (which, of course, would be based on Quartz). Figures 13.1 and 13.2 show the login and main screen of the Job Management Console application.
Figure 13.1. The login screen for our Job Management Console application.
When the user presses the Login button, the application should take the user to the Dashboard screen, which is shown in Figure 13.2.
Figure 13.2 shows the Dashboard of the console, where all users are taken after they log in.
The Dashboard page shown in Figure 13.2 is pretty simple but is good enough for our purposes.
Figure 13.2. The Job Management Console Dashboard is the main screen for users.
Creating the Job Management Console Project Structure
The project structure for the Job Management Console is very standard among Java Web applications, and most Java Web developers will recognize the purpose of most of the directories shown. The tags directory underneath WEB-INF will house the .tld files, used by applications to reference the custom tag libraries. The Struts framework provides several tag libraries that can be used to make JSP development easier; the .tld directories hold the descriptor files for those tags. Figure 13.3 shows the project structure for the application.
Figure 13.3. The project structure for the Job Management Console.
Initializing Quartz Within the Web Application
When Quartz is used from the command line, a Java class is used to create a SchedulerFactory and instantiate a Scheduler instance. Because Quartz will now be running within a Web application, you don't have easy access to the main() method because the application is started by the container, and main() is buried in code, possibly even behind an actual executable. Fortunately, the solution is easy: All you have to do is ensure that when the container first starts the Web application, you have some code that performs the factory-creation logic. That is to say, when the container first loads the web application, you need to create a SchedulerFactory and start the Scheduler.
The QuartzInitializerServlet to the Rescue
|