Uploaded image for project: 'Nuxeo Platform'
  1. Nuxeo Platform
  2. NXP-16382

Dynamically disable listeners

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Minor
    • Resolution: Duplicate
    • Affects Version/s: 7.1
    • Fix Version/s: None
    • Component/s: Core

      Description

      The context is the following: Update demo data by changing the values of all and every "date" fields. The goal is to avoid listeners to update creation/modif dates and contributors, to start workflows, to try to index text, ...

      So, it would be nice to have something like:

      someMainEventService.disableListeners();
      . . . do the update
      someMainEventService.enableListeners()
      

      Which must be more subtle than just disable/enable all: It must disable only the listeners that were already enabled, then re-enable them (and not enable all listeners including the one that were disabled before the call)

      Some more detaile APIs/doc/explanation would be welcome. For example, here, I'm just changing dates. But if I wanted to bulk change text, how could I handle indexing?

      The code I am working on (in a plug-in, not in the platform ):

      ArrayList<String> enabledListeners = new ArrayList<String>();
      boolean wasBlockAsyncHandlers;
      boolean wasBlockSyncPostCommitHandlers;
      
      protected void disableListeners() {
      
          EventServiceImpl esi = (EventServiceImpl) Framework.getService(EventService.class);
      
      	// Disable async. listeneres
          wasBlockAsyncHandlers = esi.isBlockAsyncHandlers();
          wasBlockSyncPostCommitHandlers = esi.isBlockSyncPostCommitHandlers();
          esi.setBlockAsyncHandlers(true);
          esi.setBlockSyncPostCommitHandlers(true);
      
      	// Disable sync. ("inline") listeners: Get the list of active
      	// listeners, disable each of them, then recompute the thing
          EventListenerList ell = esi.getListenerList();
          ArrayList<EventListenerDescriptor> descs = new ArrayList<EventListenerDescriptor>();
          descs.addAll(ell.getEnabledInlineListenersDescriptors());
          for (EventListenerDescriptor d : descs) {
              enabledListeners.add(d.getName());
              d.setEnabled(false);
          }
          ell.recomputeEnabledListeners();
      }
      
      protected void restoreListeners() {
      
          EventServiceImpl esi = (EventServiceImpl) Framework.getService(EventService.class);
          esi.setBlockAsyncHandlers(wasBlockAsyncHandlers);
          esi.setBlockSyncPostCommitHandlers(wasBlockSyncPostCommitHandlers);
      
          EventListenerList ell = esi.getListenerList();
          ArrayList<EventListenerDescriptor> descs = new ArrayList<EventListenerDescriptor>();
          descs.addAll(ell.getInlineListenersDescriptors());
          for (EventListenerDescriptor d : descs) {
              if (enabledListeners.contains(d.getName())) {
                  d.setEnabled(true);
              }
          }
          ell.recomputeEnabledListeners();
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: