public SchedulerProxy()

in src/main/java/org/apache/sling/commons/scheduler/impl/SchedulerProxy.java [51:102]


    public SchedulerProxy(final ThreadPoolManager manager,
            final String pName) throws SchedulerException {
        // sanity null check
        if ( manager == null ) {
            throw new SchedulerException("Thread pool manager missing");
        }
        if ( pName == null ) {
            throw new SchedulerException("Thread pool name missing");
        }

        this.threadPoolManager = manager;
        this.poolName = pName;

        // create the pool
        this.threadPool = this.threadPoolManager.get(poolName);
        final QuartzThreadPool quartzPool = new QuartzThreadPool(this.threadPool);

        boolean succeeded = false;

        try {
            final String name = QUARTZ_SCHEDULER_NAME + this.poolName.replace(' ', '_');
            final DirectSchedulerFactory factory = DirectSchedulerFactory.getInstance();
            // unique run id
            final String runID = new Date().toString().replace(' ', '_') + this.hashCode();

            factory.createScheduler(name, runID, quartzPool, new RAMJobStore());
            // quartz does not provide a way to get the scheduler by name AND runID, so we have to iterate!
            final Iterator<org.quartz.Scheduler> allSchedulersIter = factory.getAllSchedulers().iterator();
            org.quartz.Scheduler s = null;
            while ( s == null && allSchedulersIter.hasNext() ) {
                final org.quartz.Scheduler current = allSchedulersIter.next();
                if ( name.equals(current.getSchedulerName())
                    && runID.equals(current.getSchedulerInstanceId()) ) {
                    s = current;
                }
            }
            if ( s == null ) {
                throw new SchedulerException("Unable to find new scheduler with name " + name + " and run ID " + runID);
            }

            s.start();
            if ( this.logger.isDebugEnabled() ) {
                this.logger.debug("{}for pool {} started.", PREFIX, poolName);
            }
            this.scheduler = s;
            succeeded = true;
        } finally {
            if ( !succeeded) {
                this.threadPoolManager.release(this.threadPool);
            }
        }
    }