Wednesday, January 7, 2009

Quartz Sample application in Websphere 6.1

I have been working on setting up Quartz Scheduler in Websphere6.1 with Servlet, EJB and Database (Java DB Derby, formerly cloudscape). Quartz is a open source scheduler that can be used for Asynchronous processing of Jobs in background. Quartz supports both enterprise and stand alone implementation. For more information visit http://www.opensymphony.com/quartz/.


I started off by installing Java DB from http://developers.sun.com/javadb/downloads/index.jsp . You can start the database either in Single User mode or Multiuser mode (Network Server). There is a good documentation with examples available in the installed directory (/docs/pdf/getstart/getstartderby.pdf). You will have set up couple of System variables before creating the database using batch tool named ij. Once, the Database is created, go ahead and create the table (SECONDTABLE with 2 columns ID and NAME) from the scripts that provided in the documentation.


I then have a batch script that starts the network database server whenever I need to.


Next off, I downloaded the quartz1.6.4 version from http://www.opensymphony.com/quartz/download.action and extracted the Jar file to a temp location. Use the script provided in the docs/dbtables/tables_cloudscape.sql to create QRTZ tables needed for storing Job information in the event of System crash/shut down.


Created Wesphere server in RAD/Eclipse and configured the datasource with the Database created above after starting the server and running the admin console. Also created Web, EJB and EAR projects. Added quartz-1.6.4 to the WEB-INF/lib. You will also need Apache common jars in the same location.


The datasource needs to be configured via Resource Reference settings in Web.xml and as JNDI name must be referenced in the ibm-web-bnd.xmi file as below:


In web.xml:


< id="ResourceRef_1231272653234">
<>
<>quartzds
<>javax.sql.DataSource
<>Container
<>Shareable



In ibm-web-bnd.xmi:


< id="ResourceRefBinding_1231272653234" jndiname="jdbc/quartzds">
< href="WEB-INF/web.xml#ResourceRef_1231272653234">



Configured the quartz.properties as below (showing key properties only)

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.CloudscapeDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = quartzDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.dataSource.quartzDS.jndiURL=java:comp/env/quartzds



Shut down and Start of scheduler can be specified as init config parameters in Web.xml so that scheduler can be started when web module is deployed and shutdown when it is shut down. I used QuartzInitializerListener for this purpose.


I created another servlet that actually creates the job which invokes the EJB(Session Bean) with parameters as needed (this can be struts action class that can pass neccessary parameters to kick off the batch job that utilize the EJB bean). Set the request recovery flag to true so that Job can recover in the event of System Crash or Shutdown.


jobDetail.setRequestsRecovery(true);


For testing purposes, I specified the start time of the job to be 30 seconds later.

trigger.setStartTime(new Date(System.currentTimeMillis() + 30000L));


Then, created the EJB class that does the actual work in the background. Remember to reference the datasource in similiar fashion in the EJB Deployment Descriptor (ejb-jar.xml).


EJB invokes the DAO after getting the connection from Datasource. DAO utilises the connection to insert a new row in sample table (SECONDTABLE) passed as arguments from the Job.


Now, as a test you can invoke the Servlet from browser and shut down the App Server within 30 seconds so that Job information the QRTZ tables (Specifically QRTZ_SIMPLE_TRIGGERS, QRTZ_TRIGGERS, QRTZ_JOB_DETAILS). After 30 seconds have passed, restart the app server and see the job runs automatically and inserts the new row. Once the job runs successfully, data is deleted from the above mentioned QRTZ tables.


That's all folks. This is simple sample real world application that demostrates how quartz can recover in the event of system crash/shut down and re run the job.


There a lot of other features that Quartz offers that is well documented in their website.


The complete source code and batch script can be download from here (Retry download if it fails to download).

5 comments:

  1. Thông tin thật bổ ích, cảm ơn bạn đã share.
    Tìm hiểu website : Giá đá thạch anh

    ReplyDelete
  2. Kiến thức của Admin rất hữu ích, cám ơn bạn đã chia sẻ.
    Xem tại website : Thạch anh vụn

    ReplyDelete
  3. Kiến thức của tác giả hay, thank bạn đã share.
    Xem tại website : Gia công đá thạch anh

    ReplyDelete
  4. Trí tuệ của anh cũng cần thiết, thank anh vừa chia sẻ.
    Xem thêm tại website: Dây chuyền đá thạch anh

    ReplyDelete
  5. ***
    Bài post của Chủ thớt thật hay, cám ơn bạn đã share.
    Trang tham khảo : Tỳ hưu thạch anh

    ReplyDelete