Job Scheduler using commonj Timer in weblogic

A Job Scheduler using commonj api on weblogic server. The commonj is supported by websphere also.

Refer Anthony Jen’s Blog for detailed tutorial including configuring the scheduler using JMX & Spring annotations.

The following entry should be in web.xml. This will create a Timer called BatchTimer.

<resource-ref>
<res-ref-name>timer/BatchTimer</res-ref-name>
<res-type>commonj.timers.TimerManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

The batch timer with JNDI name is injected into the Spring bean, which acts as the scheduler.
Spring bean entry in applicationContext.xml

<beans>
<bean id="batchJobs" class="com.batch.BatchJobScheduler" init-method="init">
<property name="jndi" value="java:comp/env/timer/BatchTimer"></property>
</bean>
</beans>
package com.batch;
import javax.naming.InitialContext;
import commonj.timers.TimerManager;     

public class BatchJobScheduler {     

private String _jndi;
private TimerManager timerManager = null;
public void init() {
try {
	InitialContext initContext = new InitialContext();
	timerManager = (TimerManager) initContext.lookup(_jndi);     

	// schedule the batch program here
	// refer the commonj api for more options     

	timerManager.schedule(new BatchJobListener(), 0, 10 * 1000);     

	} catch (Throwable anException) {
		throw new RuntimeException(anException);
	}     

}     

public void setJndi(String aJndi) {
_jndi = aJndi;
}     

}
package com.batch;     

import commonj.timers.Timer;
import commonj.timers.TimerListener;
public class BatchJobListener implements TimerListener {
public void timerExpired(Timer arg0) {
// The batch business logic to be executed
System.out.println("Batch Job executed on " + arg0.getScheduledExecutionTime());
}
}

This entry was posted on Thursday, September 13th, 2007 at 11:01 am and is filed under Java. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply