On Fri, 2010-04-09 at 17:45 -0500, Brian Stansberry wrote:
On 04/09/2010 02:59 PM, Paul Ferraro wrote:
> I've modified Bela's existing sample code to address the issues Brian
> and I discussed below.
Thanks!
Here's a summary of the changes:
>
> * Refactored pieces into separate classes with nicer names
> * ScopedRpcDispatcher registers its scope with a ScopedUpHandler
> * Added ScopeUpHandler deregistration on ScopedRpcDispatcher.stop().
> * ScopedUpHandler accepts an optional default handler, which will be
> triggered for non-message events or messages with no scope header
> * ScopedUpHandler returns NoHandlerForScope object if message is
> received for an unknown scope (e.g. its handler was not yet registered
> or was already deregistered)
> * Automatically adds RspFilter to RequestOptions that rejects
> NoHandlerForScope responses, decorating any existing filter, if
> necessary.
> * Added ScopedMessageDispatcher analogue
>
> So, usage would look like:
>
The following is problematic because it requires passing around refs to
'h' through independent libraries that don't expose an appropriate API.
Good point...
> Channel c = new JChannel(...);
>
> ScopedUpHandler h = new ScopedUpHandlerImpl();
>
> ScopedRpcDispatcher d1 = new ScopedRpcDispatcher((short)1, h, c, null, null,
target1);
^^^ done by whatever service (e.g. web session manager) that wants to
send use Infinispan and send RPCs over the same Channel
> ScopedRpcDispatcher d2 = new ScopedRpcDispatcher((short)2, h, c, null, null,
target2);
>
^^^^ done by Infinispan, which has an API to pass in a ref to a Channel
but there's no API to pass in a ScopedUpHandler.
> // This must be set after all rpc dispatchers are created
> c.setUpHandler(h);
^^^ done by whatever service (e.g. web session manager) that wants to
send RPCs over the same Channel
> c.connect(...);
^^^^ done by Infinispan
>
It's cleaner if it can be:
Channel c = new JChannel(...);
ScopedUpHandler h = new ScopedUpHandlerImpl();
c.setUpHandler(h);
ScopedRpcDispatcher d1 = new ScopedRpcDispatcher((short)1, c, null,
null, target1); // note no "h" param
^^^ done by whatever service (e.g. web session manager) that wants to
send RPCs over the same Channel
ScopedRpcDispatcher d2 = new ScopedRpcDispatcher((short)2, c, null,
null, target2);
c.connect(...);
^^^^ done by Infinispan, which has an API to pass in a ref to a Channel
The problem is MessageDispatcher calls channel.setUpHandler() in its
constructor.
The other problem is that Channel does not expose a public mechanism to
see the current UpHandler, i.e. while Channel.up_handler is protected,
there is no public Channel.getUpHandler().
If this existed, we could replace the channel.setUpHandler(...) w/a
protected method like:
protected void setUpHandler(UpHandler handler) {
channel.setUpHandler(handler);
}
where ScopedRpcDispatcher can overload it using:
protected void setUpHandler(UpHandler handler) {
UpHandler currentHandler = channel.getUpHandler(); // NoSuchMethod
if ((currentHandler != null) && (currentHandler instanceof ScopedUpHandler)) {
((ScopedUpHandler) currentHandler).add(this.scope, handler);
} else {
channel.setUpHandler(handler);
}
}
We also wouldn't yet have a reference to the scope at the time this
method is normally called (i.e. from MessageDispatcher's constructor) -
though we could work around this by passing a null channel to the super
constructor, and calling setChannel(...) at the end of our constructor.
> This will also work in conjunction with a standard rpc
dispatcher:
>
> Channel c = new JChannel(...);
>
> RpcDispatcher d = new RpcDispatcher(c, null, null, target);
> ScopedUpHandler h = new ScopedUpHandlerImpl(d.getProtocolAdapter());
>
> ScopedRpcDispatcher d1 = new ScopedRpcDispatcher((short)1, h, c, null, null,
target1);
> ScopedRpcDispatcher d2 = new ScopedRpcDispatcher((short)2, h, c, null, null,
target2);
>
> c.setUpHandler(h);
> c.connect(...);
>
>
> I've attached the changes in the form of a patch to JGRP-1177.
> Comments are welcome.
>
> Paul