Using WorkManager in JBoss

Friday, 27 April 2007, 23:00 | Category : J2EE, Java, Technology
Tags :

You can schedule any work (thread that needs to be run) in Jboss using WorkManager API (JCA).
Below is a sample code that can be called from any ejb/servlet/mdb or any server side application running in jboss container.

Context jndiContext = new InitialContext();
MBeanServerConnection mconn = (MBeanServerConnection) jndiContext.lookup( “jmx/invoker/RMIAdaptor”);
ObjectName objectName = new ObjectName(“jboss.jca:service=WorkManager”);
JBossWorkManagerMBean jwm = (JBossWorkManagerMBean) MBeanServerInvocationHandler.newProxyInstance(mconn,objectName,
WorkManager workmanager = jwm.getInstance();
Work work = new WorkImpl();
WorkListener listener = new WorkListenerImpl();
System.out.println(” Scheduling some work”);
workmanager.scheduleWork(work,1000,new ExecutionContext(),listener);
System.out.println(“MDB:: Scheduled some Work”);
//Sample code for WorkListenerImpl
class WorkListenerImpl implements WorkListener
public void workAccepted(WorkEvent workEvent) {
System.out.println(“WorkListener:: work accepted by container”);
public void workRejected(WorkEvent workEvent) {
System.out.println(“WorkListener:: work rejected by container”);
public void workStarted(WorkEvent workEvent) {
System.out.println(“WorkListener:: work started by container”);
public void workCompleted(WorkEvent workEvent) {
System.out.println(“WorkListener:: work completed by container”);
//Sample code for WorkImpl 
public class WorkImpl implements Work, Serializable
public void release()
System.out.println(“Work:: Somebody want to kill me. I am releasing resources.”);
public void run()
System.out.println(“Work:: I am here for some work”);
try {
} catch (InterruptedException e) {
System.out.println(“Work:: Work finished”);