Broker 10.15 | webMethods Broker Documentation | webMethods Broker Client Java API Programmer's Guide | Using Request-Reply | The Requestor | Callbacks with Tags
 
Callbacks with Tags
After the preliminary processing described in the example above in The Requestor has been completed, the callback design involves these steps:
1. Use BrokerClient.publish to publish the request event.
2. Use BrokerClient.registerCallback to register a general callback object.
3. Use BrokerClient.registerCallbackForTag to register a specific callback object for the reply event with the request event's tag.
4. Use one of the callback dispatching methods, described on Dispatching Callback Methods, to receive events and dispatch the appropriate callback object's event handling method.
The following example illustrates receiving a reply event with a callback object:
public static void main(String args[])
{
SampleCallback1 general_callback =
new SampleCallback1(num_to_receive);
SampleCallback2 specific_callback =
new SampleCallback2(num_to_receive);
. . .
/* Publish the request */
try {
c.publish(e);
} catch (BrokerException ex) {
. . .
}
/* Register general callback */
try {
c.registerCallback(general_callback,null);
} catch (BrokerException ex) {
System.out.println("Error on registering general callback\n"+ex);
return;
}
/* Register specific callback for the request_tag */
try {
c.registerCallbackForTag(request_tag, true,
specific_callback,null);
} catch (BrokerException ex) {
System.out.println("Error on registering specific callback\n"+ex);
return;
}
/* Dispatch loop */
try {
BrokerClient.dispatch(-1);
} catch (BrokerException ex) {
System.out.println("Error on dispatch\n"+ex);
return;
}
. . .
The following example illustrates the callback implementation:
public class SampleCallback2 implements BrokerCallback
{
. . .
/* Method to handle the webMethods Broker Server event callbacks. */
public boolean handleBrokerEvent(
BrokerClient client,
BrokerEvent event,
Object client_data)
{
if (event.isNullReply()) {
/* handle null reply */
System.out.println("Null reply received.\n");
} else if (event.isErrorReply()) {
/* handle error reply */
System.out.println("Error reply received.\n");
} else {
/* process the event */
. . .
}
return true;
}
}