Passing Arguments to Callback Methods
When you register your callback object, you can specify a client_data object that might be necessary for the callback method to complete its processing. The client_data parameter might refer to state information that the callback method must access and update each time it is invoked.
Assume that you want to count the number of events that your application processes. When you register your callback object, you could use the client_data value to refer to the counter variable. When the callback object's event handling method is invoked, it can increment the counter.
The following example illustrates how to set up an argument for a callback object:
class IntHolder {
int count;
};
static IntHolder counter;
. . .
public static void main(String args[])
{
int n;
counter.count = 0;
SampleCallback sample_callback = new SampleCallback();
. . .
/* Check if can subscribe */
. . .
/* Register callback */
try {
c.registerCallback(sample_callback, counter);
} catch (BrokerException ex) {
System.out.println("Error on registering callback\n"+ex);
return;
}
. . .
The following example illustrates the SampleCallback implementation:
public boolean handleBrokerEvent(BrokerClient c, BrokerEvent e,
Object counter)
{
/* increment counter */
counter.count++;
/* perform rest of event processing */
. . .
}