@Test public void testUnSubscribe() throws Exception{ ResponseHandler responseHandler = new ResponseHandler() { @Override public void sendSuccessMessage(int command, ByteString requestBody, Packet responsePacket) { receivePacket = (SamplePacket) responsePacket; } @Override public void sendFailureMessage(int command, Throwable error) { } }; router.subscribe(this, SampleCmd.NOTIFY_PRICE.getValue(), responseHandler); SamplePacket packet = new SamplePacket(SampleCmd.NOTIFY_PRICE.getValue(),ByteString.encodeUtf8("{}")); for (int i = 0; i < 5; i++) { if (i == 0) { receivePacket = null; router.route(SampleCmd.NOTIFY_PRICE.getValue(),packet); assertEquals(packet,receivePacket); router.unsubscribe(responseHandler); receivePacket = null; } else { router.route(SampleCmd.NOTIFY_PRICE.getValue(),packet); assertNull(receivePacket); } } }
@Override public void unsubscribe(Object tagOrResponseHandler) { if (tagOrResponseHandler == null) { return; } List<NotifyContext> preDelList = new ArrayList<NotifyContext>(); for (NotifyContext notifyContext : notifyContexts) { if (notifyContext.tag == tagOrResponseHandler || notifyContext.responseHandler == tagOrResponseHandler) { preDelList.add(notifyContext); } } notifyContexts.removeAll(preDelList); }
DefaultOne2ManyNotifyRouter implements One2ManyNotifyRouter { @Override public void unsubscribe(Object tagOrResponseHandler) { if (tagOrResponseHandler == null) { return; } List<NotifyContext> preDelList = new ArrayList<NotifyContext>(); for (NotifyContext notifyContext : notifyContexts) { if (notifyContext.tag == tagOrResponseHandler || notifyContext.responseHandler == tagOrResponseHandler) { preDelList.add(notifyContext); } } notifyContexts.removeAll(preDelList); } }
DefaultOne2ManyNotifyRouter implements One2ManyNotifyRouter { @Override public void unsubscribe(Object tagOrResponseHandler) { if (tagOrResponseHandler == null) { return; } List<NotifyContext> preDelList = new ArrayList<NotifyContext>(); for (NotifyContext notifyContext : notifyContexts) { if (notifyContext.tag == tagOrResponseHandler || notifyContext.responseHandler == tagOrResponseHandler) { preDelList.add(notifyContext); } } notifyContexts.removeAll(preDelList); }  }
DefaultOne2ManyNotifyRouter implements One2ManyNotifyRouter { @Override public void unsubscribe(Object tagOrResponseHandler) { if (tagOrResponseHandler == null) { return; } List<NotifyContext> preDelList = new ArrayList<NotifyContext>(); for (NotifyContext notifyContext : notifyContexts) { if (notifyContext.tag == tagOrResponseHandler || notifyContext.responseHandler == tagOrResponseHandler) { preDelList.add(notifyContext); } } notifyContexts.removeAll(preDelList); }  @Override void addStickyCmd(int cmd,PacketValidator triggerPacketValidator); @Override void removeStickyCmd(int command); @Override void subscribe(Object tag, int cmd, ResponseHandler responseHandler); @Override void unsubscribe(Object tagOrResponseHandler); @Override void route(int command, Packet packet); void sendSuccessMessage(NotifyContext notifyContext, int command, Packet packet); }
DefaultOne2ManyNotifyRouter implements One2ManyNotifyRouter { @Override public void unsubscribe(Object tagOrResponseHandler) { if (tagOrResponseHandler == null) { return; } List<NotifyContext> preDelList = new ArrayList<NotifyContext>(); for (NotifyContext notifyContext : notifyContexts) { if (notifyContext.tag == tagOrResponseHandler || notifyContext.responseHandler == tagOrResponseHandler) { preDelList.add(notifyContext); } } notifyContexts.removeAll(preDelList); }  @Override void addStickyCmd(int cmd,PacketValidator triggerPacketValidator); @Override void removeStickyCmd(int command); @Override void subscribe(Object tag, int cmd, ResponseHandler responseHandler); @Override void unsubscribe(Object tagOrResponseHandler); @Override void route(int command, Packet packet); void sendSuccessMessage(NotifyContext notifyContext, int command, Packet packet);  }