1 2 Previous Next 17 Replies Latest reply: Jan 28, 2013 7:38 AM by Marc Gagnon RSS

How to get a WorkManager from a Bean

christian malolepszy Newbie

Helo all,

I have written a MessageDrivenBean and inside this process i will call some other Beans / Works parallel, wait some time, get the results from the ready processes and cancel the others.
I read about the jca WorkManager and this were a very good solution for my problem.

But I can't find any example how to work on jboss with a WorkManager, lookup etc. so I do it in this way:

JBossWorkManager jwm = new JBossWorkManager();
ThreadPool tp = new BasicThreadPool();
jwm.setThreadPool(tp);

Work work = new MyWork(1);
Work work2 = new MyWork(2);

ExecutionContext ec = new ExecutionContext();

jwm.startWork(work,0,ec,null);
jwm.startWork(work2,0,ec,null);


But I think, this is not the right way!

My question: How can i get a Workmanager from JBoss, through a JNDI lookup for example.

best regards
christian


  • 1. Re: How to get a WorkManager from a Bean
    Hannes Koller Newbie

    Hi,
    I know your post was a long time ago. I was asking myself the same Question today, and I could not find a simple example of this..so it took me a rather long time to come up with a solution..I thought it could not hurt to post it here...


    //get MBean Server
    MBeanServer server = MBeanServerLocator.locateJBoss();
    
    //get WorkManager
    ObjectName objectName = new ObjectName("jboss.jca:service=WorkManager");
    JBossWorkManagerMBean jwm = (JBossWorkManagerMBean)
     MBeanServerInvocationHandler.newProxyInstance(server,objectName,JBossWorkManagerMBean.class,false);
    WorkManager wm = jwm.getInstance();
    
    //schedule Work for Execution
    wm.scheduleWork(new SomeWork());
    


    Note that there is a difference between scheduleWork() (call returns immediately) doWork(call returns when work is finished) and startWork() (call returns when work is started).

    Btw. If there is anything wrong with this solution, I would appreciate it if someone could tell me. TIA :)


  • 2. Re: How to get a WorkManager from a Bean
    Novice

    As there is no documentation on WorkManager at all it would be very nice of the JBoss people to at least respond to forum postings. Can anybody tell me in what kind of security and transaction context the work thread will run? E.g. will the task have to authenticate for EJB calls?

  • 3. Re: How to get a WorkManager from a Bean
    Weston M. Price Master

    The WorkManager is reserved for ResourceAdapters and is not applicable to other EE components (Servlets/EJB). At this point, there is no generalized notion of a WorkManagement API across the entire application environment. A JSR has been submitted but there is no work currently being done on it.

  • 4. Re: How to get a WorkManager from a Bean
    madhav Newbie

    I have read that we can do parallel processing in J2EE Container using WorkManger API(JSR 237).But as per weston comment below,In Jboss Workmanager API is only for resource adapters.Can anyone please suggest me how we can do parallel processing in JBoss J2EE Container.

  • 5. How to get a WorkManager instance?
    lpmon Novice

    Seems like there should be a JNDI/context-based lookup to get the WorkManager.

    I want to launch some worker threads. The page below and other reading suggests a lookup can be done:

    http://www.java-tips.org/java-ee-tips/enterprise-java-beans/j2ee-connector-architecture-1.5.html

    ....Maybe just not in JBoss yet.

    Do I get a work manager using the methods in the previous posts?

  • 6. Re: How to get a WorkManager from a Bean
    Weston M. Price Master

    Again, the WorkManager is reserved for ResourceAdapters (ie JCA deployments) it is not available via JNDI or to any other type of application component.

    The documentation link you posted is relevant to JCA not as a general solution for all application components.

    Technically, there is nothing to prevent you from getting the WorkManager via JMX (as per the example below), but this is not standard and not portable. In other words, you shouldn't really be doing this. A generalized WorkManagement API has been submitted as a JSR (237)

    http://jcp.org/en/jsr/detail?id=237

    However, it is currently in limbo right now due to legal disputes between the major vendors involved.

  • 7. Re: How to get a WorkManager from a Bean
    Novice

    Portability is not much of an issue for me. We are quite committed to using JBoss, and moving to another appserver would mean lots of man-years of work anyway. So a JBoss-only solution that is available now is more welcome than a portable one that is not available. Anyway, JBoss specific code can always be wrapped behind a custom interface if portability is a real concern.

  • 8. Re: How to get a WorkManager from a Bean (worker threads)
    lpmon Novice

    Thank You Weston.

    Then let me ask the more generic question:

    What is the recommendation to start/manage background worker threads in JBoss 4.0.x?

    Using JMS for a long running process seems to be discouraged (like 5 minutes of background work)

    I can implement a Runnable/Thread class and manage it myself but per J2EE protocol this is not recommended. Threads are supposed to be managed by the app server.

    Thanks in advance!

  • 9. Re: How to get a WorkManager from a Bean
    Weston M. Price Master

    Best of luck ;-)

    As far as the


    Can anybody tell me in what kind of security and transaction context the work thread will run?


    The answer is that it will be unspecified being that the WorkManager API is meant to be consumed solely from a ResourceAdapter which would have it's own transaction/security context. In other words, it is unknown and there are no guarantees because this is a non standard use of the API.






  • 10. Re: How to get a WorkManager from a Bean
    Novice

    So we're left with managing our own threads? To be safe, I suppose we should perform a JAAS logon, manage a client transaction and use remote interfaces only, basically acting like an application client.

  • 11. Re: How to get a WorkManager from a Bean
    Weston M. Price Master

     


    What is the recommendation to start/manage background worker threads in JBoss 4.0.x?


    There is nothing to prevent you from developing a custom MBean that uses threads or a thread pool to preform background tasks. Simple wrap your Runnable/Thread stuff in an MBean and that's that. Also, JMX is nice for configuration/management as well. This would be my suggestion.

    You could develop a custom JCA adapter that's sole purpose is to give EJB and Servlets access to the WorkManagement API but this is a bit heavy when a simple MBean would really suffice.

  • 12. Re: How to get a WorkManager from a Bean
    michele curioni Newbie

    Hi there,
    has any progress been made in JBoss on this front?

    Other vendors allow to use Workmanager from every component.

    It would be good to have the same feature in JBoss.

    Thanks,
    Michele


  • 14. Re: How to get a WorkManager from a Bean
    Chris Meadows Newbie

    +1

    But until then, the MBean approach is a good approach.

    If, however, your system is mostly asynchronous/event driven, then J2EE isn't really the way to proceed; you may want to use the JBoss JSLEE container instead, http://labs.jboss.com/mobicents

1 2 Previous Next