Skip to content


Using WorkManager in JBoss

Posted in J2EE, Java, Technology.

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,
JBossWorkManagerMBean.class,false);
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 {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(“Work:: Work finished”);

}
}

Note from Author :
I maintain my website, write articles and reply comments because of my interest/hobby to share information and knowledge.

If you liked the post, Please
Follow me on twitter: http://twitter.com/pankajbatracom
Join My facebook page: http://www.facebook.com/pankajbatra.blog
Join me on LinkedIn: http://www.linkedin.com/in/batrapankaj
Or subscribe to updates by Email by clicking here

To subscribe for updates, Enter your email address:

Related posts:

  1. Sample java code to read mails from your gmail account
  2. Reading Mails from secure exchange server in Java
  3. Executing Javascript in Java code
  4. Covariant return types
  5. Number of processors on a machine


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.







Some HTML is OK

or, reply to this post via trackback.