Search in sources :

Example 1 with QueueStatusEvent

use of org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent in project sling by apache.

the class QueueManager method outdateQueue.

private void outdateQueue(final JobQueueImpl queue) {
    // remove the queue with the old name
    // check for main queue
    final String oldName = ResourceHelper.filterQueueName(queue.getName());
    this.queues.remove(oldName);
    // check if we can close or have to rename
    if (queue.tryToClose()) {
        // copy statistics
        // update mbeans
        ((QueuesMBeanImpl) queuesMBean).sendEvent(new QueueStatusEvent(null, queue));
    } else {
        queue.outdate();
        // readd with new name
        String newName = ResourceHelper.filterName(queue.getName());
        int index = 0;
        while (this.queues.containsKey(newName)) {
            newName = ResourceHelper.filterName(queue.getName()) + '$' + String.valueOf(index++);
        }
        this.queues.put(newName, queue);
        // update mbeans
        ((QueuesMBeanImpl) queuesMBean).sendEvent(new QueueStatusEvent(queue, queue));
    }
}
Also used : QueueStatusEvent(org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent) QueuesMBeanImpl(org.apache.sling.event.impl.jobs.jmx.QueuesMBeanImpl)

Example 2 with QueueStatusEvent

use of org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent in project sling by apache.

the class QueueManager method start.

/**
     * Start a new queue
     * This method first searches the corresponding queue - if such a queue
     * does not exist yet, it is created and started.
     *
     * @param queueInfo The queue info
     * @param topics The topics
     */
private void start(final QueueInfo queueInfo, final Set<String> topics) {
    final InternalQueueConfiguration config = queueInfo.queueConfiguration;
    // get or create queue
    boolean isNewQueue = false;
    JobQueueImpl queue = null;
    // we synchronize to avoid creating a queue which is about to be removed during cleanup
    synchronized (queuesLock) {
        queue = this.queues.get(queueInfo.queueName);
        // check for reconfiguration, we really do an identity check here(!)
        if (queue != null && queue.getConfiguration() != config) {
            this.outdateQueue(queue);
            // we use a new queue with the configuration
            queue = null;
        }
        if (queue == null) {
            queue = JobQueueImpl.createQueue(queueInfo.queueName, config, queueServices, topics);
            // on startup the queue might be empty and we get null back from createQueue
            if (queue != null) {
                isNewQueue = true;
                queues.put(queueInfo.queueName, queue);
                ((QueuesMBeanImpl) queuesMBean).sendEvent(new QueueStatusEvent(queue, null));
            }
        }
    }
    if (queue != null) {
        if (!isNewQueue) {
            queue.wakeUpQueue(topics);
        }
        queue.startJobs();
    }
}
Also used : QueueStatusEvent(org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent) QueuesMBeanImpl(org.apache.sling.event.impl.jobs.jmx.QueuesMBeanImpl) InternalQueueConfiguration(org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration)

Example 3 with QueueStatusEvent

use of org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent in project sling by apache.

the class QueueManager method maintain.

/**
     * This method is invoked periodically by the scheduler.
     * It searches for idle queues and stops them after a timeout. If a queue
     * is idle for two consecutive clean up calls, it is removed.
     * @see java.lang.Runnable#run()
     */
private void maintain() {
    this.schedulerRuns++;
    logger.debug("Queue manager maintenance: Starting #{}", this.schedulerRuns);
    // queue maintenance
    if (this.isActive.get()) {
        for (final JobQueueImpl jbq : this.queues.values()) {
            jbq.maintain();
        }
    }
    // full topic scan is done every third run
    if (schedulerRuns % 3 == 0 && this.isActive.get()) {
        this.fullTopicScan();
    }
    // we only do a full clean up on every fifth run
    final boolean doFullCleanUp = (schedulerRuns % 5 == 0);
    if (doFullCleanUp) {
        // check for idle queue
        logger.debug("Checking for idle queues...");
        // we synchronize to avoid creating a queue which is about to be removed during cleanup
        synchronized (queuesLock) {
            final Iterator<Map.Entry<String, JobQueueImpl>> i = this.queues.entrySet().iterator();
            while (i.hasNext()) {
                final Map.Entry<String, JobQueueImpl> current = i.next();
                final JobQueueImpl jbq = current.getValue();
                if (jbq.tryToClose()) {
                    logger.debug("Removing idle job queue {}", jbq);
                    // remove
                    i.remove();
                    // update mbeans
                    ((QueuesMBeanImpl) queuesMBean).sendEvent(new QueueStatusEvent(null, jbq));
                }
            }
        }
    }
    logger.debug("Queue manager maintenance: Finished #{}", this.schedulerRuns);
}
Also used : QueueStatusEvent(org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent) QueuesMBeanImpl(org.apache.sling.event.impl.jobs.jmx.QueuesMBeanImpl) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with QueueStatusEvent

use of org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent in project sling by apache.

the class QueueManager method deactivate.

/**
     * Deactivate this component.
     */
@Deactivate
protected void deactivate() {
    logger.debug("Apache Sling Queue Manager stopping on instance {}", Environment.APPLICATION_ID);
    this.configuration.removeListener(this);
    final Iterator<JobQueueImpl> i = this.queues.values().iterator();
    while (i.hasNext()) {
        final JobQueueImpl jbq = i.next();
        jbq.close();
        // update mbeans
        ((QueuesMBeanImpl) queuesMBean).sendEvent(new QueueStatusEvent(null, jbq));
    }
    this.queues.clear();
    this.queueServices = null;
    logger.info("Apache Sling Queue Manager stopped on instance {}", Environment.APPLICATION_ID);
}
Also used : QueueStatusEvent(org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent) QueuesMBeanImpl(org.apache.sling.event.impl.jobs.jmx.QueuesMBeanImpl) Deactivate(org.osgi.service.component.annotations.Deactivate)

Aggregations

QueueStatusEvent (org.apache.sling.event.impl.jobs.jmx.QueueStatusEvent)4 QueuesMBeanImpl (org.apache.sling.event.impl.jobs.jmx.QueuesMBeanImpl)4 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 InternalQueueConfiguration (org.apache.sling.event.impl.jobs.config.InternalQueueConfiguration)1 Deactivate (org.osgi.service.component.annotations.Deactivate)1