You can run work objects in parallel, or in a different Java Platform, Enterprise Edition (Java EE) context, by wrapping the code in a work object.
To run code in parallel, wrap the code in a work object.
class SampleWork implements Work
{
boolean released;
Topic targetTopic;
EventSource es;
TopicConnectionFactory tcf;
public SampleWork(TopicConnectionFactory tcf, EventSource es, Topic targetTopic)
{
released = false;
this.targetTopic = targetTopic;
this.es = es;
this.tcf = tcf;
}
synchronized boolean getReleased()
{
return released;
}
public void run()
{
try
{
// setup our JMS stuff.
TopicConnection tc = tcf.createConnection();
TopicSession sess = tc.createSession(false, Session.AUTOACK);
tc.start();
MessageListener proxy = es.getEventTrigger(MessageListener.class, false);
while(!getReleased())
{
// block for up to 5 seconds.
Message msg = sess.receiveMessage(5000);
if(msg != null)
{
// fire an event when we get a message
proxy.onMessage(msg);
}
}
tc.close();
}
catch (JMSException ex)
{
// handle the exception here
throw ex;
}
finally
{
if (tc != null)
{
try
{
tc.close();
}
catch (JMSExceptin ex1)
{
// handle exception
}
}
}
}
// called when we want to stop the Work object.
public synchronized void release()
{
released = true;
}
}
As a result, any component that has access to the event
source can add an event on demand, which allows components to subscribe
to a topic in a more scalable way than by simply giving each client
subscriber its own thread. The previous example is fully explored
in the WebSphere® Trader
Sample. Refer to the Samples section of the Information Center for
details.InitialContext ic = new InitialContext(); WorkManager wm = (WorkManager)ic.lookup("java:comp/env/wm/myWorkManager");The resource reference for the work manager (in this case, wm/myWorkManager) must be declared as a resource reference in the application deployment descriptor.
Work w = new MyWork(...); WorkItem wi = wm.startWork(w);The startWork() method can take a startTimeout parameter. This specifies a hard time limit for the Work object to be started. The startWork() method returns a work item object. This object is a handle that provides a link from the component to the now running work object.
WorkItem wiA = wm.start(workA); WorkItem wiB = wm.start(workB); ArrayList l = new ArrayList(); l.add(wiA); l.add(wiB); if(wm.join(l, wm.JOIN_AND, 5000)) // block for up to 5 seconds { // both wiA and wiB finished } else { // timeout // we can check wiA.getStatus or wiB.getStatus to see which, if any, finished. }This method takes an array list of work items which your component wants to wait on and a flag that indicates whether the component will wait for one or all of the work objects to complete. You also can specify a timeout value.
public synchronized void release() { released = true; }The Work.run() method can periodically examine this variable to check whether the loop exits or not.