📄 bayeuxservice.java
字号:
c=c.getSuperclass(); } if (method==null) throw new NoSuchMethodError(methodName); int params=method.getParameterTypes().length; if (params<2 || params>4) throw new IllegalArgumentException("Method '"+methodName+"' does not have 2or3 parameters"); if (!Client.class.isAssignableFrom(method.getParameterTypes()[0])) throw new IllegalArgumentException("Method '"+methodName+"' does not have Client as first parameter"); Channel channel=_bayeux.getChannel(channelId,true); if (((ChannelImpl)channel).getChannelId().isWild()) { final Method m=method; Client wild_client=_bayeux.newClient(_name+"-wild"); wild_client.addListener(_listener instanceof MessageListener.Asynchronous?new AsyncWildListen(wild_client,m):new SyncWildListen(wild_client,m)); channel.subscribe(wild_client); } else { _methods.put(channelId,method); channel.subscribe(_client); } } /* ------------------------------------------------------------ */ /** Send data to a individual client. * The data passed is sent to the client as the "data" member of a message * with the given channel and id. The message is not published on the channel and is * thus not broadcast to all channel subscribers. However to the target client, the * message appears as if it was broadcast. * <p> * Typcially this method is only required if a service method sends response(s) to * channels other than the subscribed channel. If the response is to be sent to the subscribed * channel, then the data can simply be returned from the subscription method. * * @param toClient The target client * @param onChannel The channel the message is for * @param data The data of the message * @param id The id of the message (or null for a random id). */ protected void send(Client toClient, String onChannel, Object data, String id) { toClient.deliver(getClient(),onChannel,data,id); } /* ------------------------------------------------------------ */ /** Handle Exception. * This method is called when a mapped subscription method throws * and exception while handling a message. * @param fromClient * @param toClient * @param msg * @param th */ protected void exception(Client fromClient, Client toClient, Map<String, Object> msg,Throwable th) { System.err.println(msg); th.printStackTrace(); } /* ------------------------------------------------------------ */ private void invoke(final Method method,final Client fromClient, final Client toClient, final Message msg) { if (_threadPool==null) doInvoke(method,fromClient,toClient,msg); else { _threadPool.dispatch(new Runnable() { public void run() { try { ((MessageImpl)msg).incRef(); doInvoke(method,fromClient,toClient,msg); } finally { ((MessageImpl)msg).decRef(); } } }); } } /* ------------------------------------------------------------ */ private void doInvoke(Method method,Client fromClient, Client toClient, Message msg) { String channel=(String)msg.get(Bayeux.CHANNEL_FIELD); Object data=msg.get(Bayeux.DATA_FIELD); String id=msg.getId(); if (method!=null) { try { Class<?>[] args = method.getParameterTypes(); Object arg=Message.class.isAssignableFrom(args[1])?msg:data; Object reply=null; switch(method.getParameterTypes().length) { case 2: reply=method.invoke(this,fromClient,arg); break; case 3: reply=method.invoke(this,fromClient,arg,id); break; case 4: reply=method.invoke(this,fromClient,channel,arg,id); break; } if (reply!=null) send(fromClient,channel,reply,id); } catch (Exception e) { System.err.println(method); exception(fromClient,toClient,msg,e); } catch (Error e) { System.err.println(method); exception(fromClient,toClient,msg,e); } } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class AsyncListen implements MessageListener, MessageListener.Asynchronous { public void deliver(Client fromClient, Client toClient, Message msg) { if (!_seeOwn && fromClient==getClient()) return; String channel=(String)msg.get(Bayeux.CHANNEL_FIELD); Method method=_methods.get(channel); invoke(method,fromClient,toClient,msg); } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class SyncListen implements MessageListener, MessageListener.Synchronous { public void deliver(Client fromClient, Client toClient, Message msg) { if (!_seeOwn && fromClient==getClient()) return; String channel=(String)msg.get(Bayeux.CHANNEL_FIELD); Method method=_methods.get(channel); invoke(method,fromClient,toClient,msg); } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class SyncWildListen implements MessageListener, MessageListener.Synchronous { Client _client; Method _method; public SyncWildListen(Client client,Method method) { _client=client; _method=method; } public void deliver(Client fromClient, Client toClient, Message msg) { if (!_seeOwn && (fromClient==_client || fromClient==getClient())) return; invoke(_method,fromClient,toClient,msg); } }; /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class AsyncWildListen implements MessageListener, MessageListener.Asynchronous { Client _client; Method _method; public AsyncWildListen(Client client,Method method) { _client=client; _method=method; } public void deliver(Client fromClient, Client toClient, Message msg) { if (!_seeOwn && (fromClient==_client || fromClient==getClient())) return; invoke(_method,fromClient,toClient,msg); } };}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -