public DistributedEventReceiver()

in src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java [107:173]


    public DistributedEventReceiver(final BundleContext bundleContext,
            final String rootPath,
            final String ownRootPath,
            final int cleanupPeriod,
            final ResourceResolverFactory rrFactory,
            final SlingSettingsService settings) {
        this.rootPath = rootPath;
        this.ownRootPath = ownRootPath;
        this.resourceResolverFactory = rrFactory;
        this.slingId = settings.getSlingId();
        this.cleanupPeriod = cleanupPeriod;

        this.running = true;
        // start writer thread
        final Thread writerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                // create service registration properties
                final Dictionary<String, Object> props = new Hashtable<String, Object>();
                props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");

                // listen for all OSGi events with the distributable flag
                props.put(EventConstants.EVENT_TOPIC, "*");
                props.put(EventConstants.EVENT_FILTER, "(" + DEAConstants.PROPERTY_DISTRIBUTE + "=*)");
                // schedule this service every 30 minutes
                props.put("scheduler.period", 1800L);
                props.put("scheduler.concurrent", Boolean.FALSE);
                props.put("scheduler.threadpool", "org-apache-sling-event-dea");

                final ServiceRegistration<?> reg =
                        bundleContext.registerService(new String[] {EventHandler.class.getName(),
                                                                   Runnable.class.getName(),
                                                                   TopologyEventListener.class.getName()},
                                                      DistributedEventReceiver.this, props);

                DistributedEventReceiver.this.serviceRegistration = reg;

                /**
                 * The writerResolver is a long running resource resolver, which is refreshed before it is used.
                 * We also cannot use try-with-resource here, because writerResolver needs to be global due to this.
                 */
                try {
                    writerResolver = resourceResolverFactory.getServiceResourceResolver(null);
                    ResourceUtil.getOrCreateResource(writerResolver,
                            ownRootPath,
                            DistributedEventAdminImpl.RESOURCE_TYPE_FOLDER,
                            DistributedEventAdminImpl.RESOURCE_TYPE_FOLDER,
                            true);
                } catch (final Exception e) {
                    // there is nothing we can do except log!
                    logger.error("Error during resource resolver creation.", e);
                    running = false;
                }
                try {
                    processWriteQueue(); // this will block until stop() is invoked
                } catch (final Throwable t) { //NOSONAR
                    logger.error("Writer thread stopped with exception: " + t.getMessage(), t);
                    running = false;
                }
                if ( writerResolver != null ) {
                    writerResolver.close();
                    writerResolver = null;
                }
            }
        });
        writerThread.start();
    }