Friday, 4 September 2009

Using a WebSphere Scheduler

This posting demonstrates using a WebSphere Application Server Scheduler (tested in 6.0.2.35, but highly likely to be applicable upto 7.0) in the following way:
  1. Setting up a standard apache Derby database to hold the scheduler tables.
  2. Creating a WAS scheduler that is callable via JNDI (and uses tables in the derby database).
  3. Producing an .ear file containing three components: a servlet and two, minimalist, stateless EJB's that demonstrate the important scheduler API's.
  4. Invoking the API and seeing the expected output.
Code in the doGet of the servlet registers the EJB's with the scheduler. Doing this allows the scheduler to callback relevant methods either at the scheduled time or for when the state of the scheduled task changes...
  • The optional EJB uses a public void handleEvent(TaskNotificationInfo arg) method that gets invoked by the scheduler so that events related to the management of the scheduled task can be intercepted (if you want) by your business logic - e.g. a TaskNotificationInfo.PURGED notification.
  • The mandatory EJB implements a public void process(TaskStatus arg) method, that gets invoked by the scheduler and carries out your business logic.

Set up the Derby database

The SQL used for creating the derby tables, required by the scheduler, is shipped in the Scheduler folder - for me that was /home/jsears/IBM/WebSphere/7/AppServer1/Scheduler.

Basically I took the createSchemaMod1Derby.ddl file from my WAS 7 instance, created the database and then pointed my WAS 6.0 scheduler at it. I used this combination because RAD 6.0 and WAS 6.0 are my primary environments.

The .ddl has to be modified with some variable substitution, for example: "CREATE TABLE @TABLE_QUALIFIER@@TABLE_PREFIX@TASK ("TASKID" BIGINT NOT NULL ," became "CREATE TABLE TBLTASK ("TASKID" BIGINT NOT NULL ,". I saved the result in a derby.sql file.

From the command line (after downloading and extracting derby) I exported the following variables:
  • export DERBY_HOME=/home/jsears/local/tmp3/db-derby-10.5.3.0-bin
  • export CLASSPATH=$DERBY_INSTALL/lib/derbyclient.jar:$DERBY_INSTALL/lib/derbytools.jar:.
I then started the derby server:
  • java -jar $DERBY_HOME/lib/derbyrun.jar server start
I then ran the ddl file (via a new terminal with the export's):
  • java -jar $DERBY_HOME/lib/derbyrun.jar ij
  • connect 'jdbc:derby://localhost:1527//home/jsears/local/tmp3/MyDbTest;create=true';
  • run '/home/jsears/local/tmp3/derby.sql';
  • show tables;
I then set up security:
  • CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.user.user1', 'password1');
  • CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.connection.requireAuthentication', 'true');
  • disconnect;
  • connect 'jdbc:derby://localhost:1527//home/jsears/local/tmp3/MyDbTest;user=user1;password=password1';
  • exit;
Create The Scheduler
Through the WebSphere Administrative console I created the database stuff first and then the scheduler stuff:
  • Security > Global security > JAAS Configuration > J2C Authentication data > New ...
  1. Username and Password values as above, from derby setup, for jsears-desktopNode01/MyDerbyUID:
  • Resources > JDBC providers @ Server level > New ...
  1. Name = Derby JSEARS 01
  2. Class path = /home/jsears/local/tmp3/db-derby-10.5.3.0-bin/lib/derbyclient.jar
  3. Implementation class name = org.apache.derby.jdbc.ClientConnectionPoolDataSource
  • JDBC providers > Derby JSEARS 01 > Data sources > New ...
  1. Name = Derby Network Server DataSource
  2. JNDI name = jdbc/DerbyNSDS
  3. Component-managed authentication alias = jsears-desktopNode01/MyDerbyUID
  4. When created press the "Test connection" button!
  • JDBC providers > Derby JSEARS 01 > Data sources > Derby Network Server DataSource > Custom properties
  1. databaseName = /home/jsears/local/tmp3/MyDbTest
  2. serverName = localhost
  3. portNumber = 1527
  • Resources > Schedulers @ Server Level > New ...
  1. Name = MyScheduler
  2. JNDI name = sched/MyScheduler
  3. Data source JNDI name = jdbc/DerbyNSDS
  4. Data source alias = jsears-desktopNode01/MyDerbyUID
  5. Table prefix = tbl
  6. Work managers = Default Work Manager
  7. When completed press the "Verify tables" button!
Use the API's
The servlet looks like this, in my mind the important bit is how the call backs are registered:

The first EJB is the optional one:

And here is the mandatory EJB:

What is most important is how the deployment descriptor looks (take into account that this was coded in a RAD 6 IDE). In particular how the tag values mean that these interfaces do not have to be coded in java.

NOTE: use the following in RAD as the JDBC Driver class if you want to connect RAD to derby: org.apache.derby.jdbc.ClientDriver

Invoke the API
Figure out what the appropriate BOOTSTRAP_ADDRESS + WC_defaulthost ports are of the servlet:
  • Servers > Application Servers > server1 > Ports
And then call the servlet:
  • http://127.0.0.1:9081/SchedWebApp/Main/
Finally, you should end up with something like this:

0 comments:

Post a Comment