WebSphere Application Server Network Deployment, Version 6.1
             Operating Systems: AIX, HP-UX, i5/OS, Linux, Solaris, Windows, z/OS

             Personalize the table of contents and search results

Example: Creating work objects

You can create a work object that dynamically subscribes to a topic and any component that has access to the event source can add an event on demand.

The following is an example of a work object that dynamically subscribes to a topic:
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. See the Samples Gallery for details.




Related concepts
Accessing Samples
Related tasks
Developing work objects to run code in parallel
Reference topic    

Terms of Use | Feedback

Last updated: Feb 25, 2009 9:32:38 AM CST
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.nd.multiplatform.doc/info/ae/asyncbns/xmp/xasb_workobject.html