Search in sources :

Example 1 with BatchResourceRemover

use of org.apache.sling.event.impl.support.BatchResourceRemover in project sling by apache.

the class JobQueueImpl method removeAll.

/**
     * @see org.apache.sling.event.jobs.Queue#removeAll()
     */
@Override
public synchronized void removeAll() {
    final Set<String> topics = this.cache.getTopics();
    logger.debug("Removing all jobs for queue {} : {}", queueName, topics);
    if (!topics.isEmpty()) {
        final ResourceResolver resolver = this.services.configuration.createResourceResolver();
        try {
            final Resource baseResource = resolver.getResource(this.services.configuration.getLocalJobsPath());
            // sanity check - should never be null
            if (baseResource != null) {
                final BatchResourceRemover brr = new BatchResourceRemover();
                for (final String t : topics) {
                    final Resource topicResource = baseResource.getChild(t.replace('/', '.'));
                    if (topicResource != null) {
                        JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {

                            @Override
                            public boolean handle(final JobImpl job) {
                                final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
                                // sanity check
                                if (jobResource != null) {
                                    try {
                                        brr.delete(jobResource);
                                    } catch (final PersistenceException ignore) {
                                        logger.error("Unable to remove job " + job, ignore);
                                        topicResource.getResourceResolver().revert();
                                        topicResource.getResourceResolver().refresh();
                                    }
                                }
                                return true;
                            }
                        });
                    }
                }
                try {
                    resolver.commit();
                } catch (final PersistenceException ignore) {
                    logger.error("Unable to remove jobs", ignore);
                }
            }
        } finally {
            resolver.close();
        }
    }
}
Also used : BatchResourceRemover(org.apache.sling.event.impl.support.BatchResourceRemover) JobImpl(org.apache.sling.event.impl.jobs.JobImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) JobTopicTraverser(org.apache.sling.event.impl.jobs.JobTopicTraverser)

Aggregations

PersistenceException (org.apache.sling.api.resource.PersistenceException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 JobImpl (org.apache.sling.event.impl.jobs.JobImpl)1 JobTopicTraverser (org.apache.sling.event.impl.jobs.JobTopicTraverser)1 BatchResourceRemover (org.apache.sling.event.impl.support.BatchResourceRemover)1