4 Replies Latest reply: Mar 9, 2012 8:08 AM by gardellajp RSS

    LocalTaskService don't remove listeners, it is a bug?

    gardellajp Newbie

      Hi,

       

      Recently I've found that when use LocalTaskService don't remove the listeners that are created in:

       

        public void registerForEvent(EventKey key, boolean remove, EventResponseHandler responseHandler) {
          SimpleEventTransport transport =
              new SimpleEventTransport(taskServiceSession, responseHandler, remove);
          taskServiceSession.getService().getEventKeys().register(key, transport);
        }
      

       

      Notice that a client of this method can't remove it later, becouse the method return void.

       

      This method is called by SyncWSHumanTaskHandler.connect()

       

       private void registerTaskEvents() {
       TaskCompletedHandler eventResponseHandler = new TaskCompletedHandler();
              TaskEventKey key = new TaskEventKey(TaskCompletedEvent.class, -1);
              client.registerForEvent(key, false, eventResponseHandler);
              key = new TaskEventKey(TaskFailedEvent.class, -1);
              client.registerForEvent(key, false, eventResponseHandler);
              key = new TaskEventKey(TaskSkippedEvent.class, -1);
              client.registerForEvent(key, false, eventResponseHandler);
          }
      

       

      See that the listeners aren't save to remove later. If don't remove the listeners, they are invoqued later.

       

      I fixed this with an update of LocalTaskService. When register the events, I save each listener to then dispose it.

       

       

        public void registerForEvent(EventKey key, boolean remove, EventResponseHandler responseHandler) {
          SimpleEventTransport transport =
              new SimpleEventTransport(taskServiceSession, responseHandler, remove);
          taskServiceSession.getService().getEventKeys().register(key, transport);
          //ADDED
          eventsRegistered.add(new KeyAndTransport(key, transport));
         //END ADDED
        }
      

       

      The class KeyAndTransport have two instances variables, the key and the transport. The instance variable eventsRegistered is a collection. Then, when is disconnected, I remove the listeners.

       

      public void disconnect() throws Exception {
          // do nothing
          // ADDED
          removeListeners();
         // END ADDED
        }
      

       

       

        private synchronized void removeListeners() {
          for (KeyAndTransport k : eventsRegistered) {
            taskServiceSession.getService().getEventKeys().unregister(k.getKey(), k.getTransport());
          }
          eventsRegistered.clear();
        }
      

       

      I attach the complete file. I want to know if this is a bug or a miss use of this clasess. I have problems when use more than one session, with this change, work well with multiple sessions. Note that in my project I open and close session in each request, so when I close the session (and other resources) I can't remove the listener with the actual API.

       

      Juan