use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class QueueJobCache method loadJobs.
/**
* Load the next N x numberOf(topics) jobs
* @param checkingTopics The set of topics to check.
*/
private void loadJobs(final String queueName, final Set<String> checkingTopics, final StatisticsManager statisticsManager) {
logger.debug("Starting jobs loading from {}...", checkingTopics);
final Map<String, List<JobImpl>> topicCache = new HashMap<String, List<JobImpl>>();
final ResourceResolver resolver = this.configuration.createResourceResolver();
try {
final Resource baseResource = resolver.getResource(this.configuration.getLocalJobsPath());
// sanity check - should never be null
if (baseResource != null) {
for (final String topic : checkingTopics) {
final Resource topicResource = baseResource.getChild(topic.replace('/', '.'));
if (topicResource != null) {
topicCache.put(topic, loadJobs(queueName, topic, topicResource, statisticsManager));
}
}
}
} finally {
resolver.close();
}
orderTopics(topicCache);
logger.debug("Finished jobs loading {}", this.cache.size());
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class QueueManager method scanTopics.
/**
* Scan the resource tree for topics.
*/
private Set<String> scanTopics() {
final Set<String> topics = new HashSet<>();
final ResourceResolver resolver = this.configuration.createResourceResolver();
try {
final Resource baseResource = resolver.getResource(this.configuration.getLocalJobsPath());
// sanity check - should never be null
if (baseResource != null) {
final Iterator<Resource> topicIter = baseResource.listChildren();
while (topicIter.hasNext()) {
final Resource topicResource = topicIter.next();
final String topic = topicResource.getName().replace('.', '/');
logger.debug("Found topic {}", topic);
topics.add(topic);
}
}
} finally {
resolver.close();
}
return topics;
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class JobSchedulerImpl method setSuspended.
/**
* Change the suspended flag for a scheduled job
* @param info The schedule info
* @param flag The corresponding flag
*/
public void setSuspended(final ScheduledJobInfoImpl info, final boolean flag) {
final ResourceResolver resolver = configuration.createResourceResolver();
try {
final StringBuilder sb = new StringBuilder(this.configuration.getScheduledJobsPath(true));
sb.append(ResourceHelper.filterName(info.getName()));
final String path = sb.toString();
final Resource eventResource = resolver.getResource(path);
if (eventResource != null) {
final ModifiableValueMap mvm = eventResource.adaptTo(ModifiableValueMap.class);
if (flag) {
mvm.put(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED, Boolean.TRUE);
} else {
mvm.remove(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED);
}
resolver.commit();
}
if (flag) {
this.stopScheduledJob(info);
} else {
this.startScheduledJob(info);
}
} catch (final PersistenceException pe) {
// we ignore the exception if removing fails
ignoreException(pe);
} finally {
resolver.close();
}
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class ScheduledJobHandler method remove.
/**
* Remove a scheduled job
* @param info The schedule info
*/
public void remove(final ScheduledJobInfoImpl info) {
final String scheduleKey = ResourceHelper.filterName(info.getName());
final ResourceResolver resolver = configuration.createResourceResolver();
try {
final StringBuilder sb = new StringBuilder(configuration.getScheduledJobsPath(true));
sb.append(scheduleKey);
final String path = sb.toString();
final Resource eventResource = resolver.getResource(path);
if (eventResource != null) {
resolver.delete(eventResource);
resolver.commit();
}
} catch (final PersistenceException pe) {
// we ignore the exception if removing fails
ignoreException(pe);
} finally {
resolver.close();
}
synchronized (this.scheduledJobs) {
final Holder h = scheduledJobs.remove(scheduleKey);
if (h != null && h.info != null) {
jobScheduler.unscheduleJob(h.info);
}
}
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class ScheduledJobHandler method scan.
private void scan() {
final ResourceResolver resolver = configuration.createResourceResolver();
if (resolver != null) {
try {
logger.debug("Scanning for scheduled jobs...");
final String path = this.configuration.getScheduledJobsPath(false);
final Resource startResource = resolver.getResource(path);
if (startResource != null) {
final Map<String, Holder> newScheduledJobs = new HashMap<String, Holder>();
synchronized (this.scheduledJobs) {
for (final Resource rsrc : startResource.getChildren()) {
if (!isRunning.get()) {
break;
}
handleAddOrUpdate(newScheduledJobs, rsrc);
}
if (isRunning.get()) {
for (final Holder h : this.scheduledJobs.values()) {
if (h.info != null) {
this.jobScheduler.unscheduleJob(h.info);
}
}
this.scheduledJobs.clear();
this.scheduledJobs.putAll(newScheduledJobs);
}
}
}
logger.debug("Finished scanning for scheduled jobs...");
} finally {
resolver.close();
}
}
}
Aggregations