use of org.apache.sling.commons.scheduler.ScheduleOptions in project sling by apache.
the class WhiteboardHandler method scheduleJob.
private void scheduleJob(final ServiceReference<?> ref, final Object job, final ScheduleOptions scheduleOptions) {
((InternalScheduleOptions) scheduleOptions).providedName = getStringProperty(ref, Scheduler.PROPERTY_SCHEDULER_NAME);
final String name = getServiceIdentifier(ref);
final Boolean concurrent = getBooleanProperty(ref, Scheduler.PROPERTY_SCHEDULER_CONCURRENT);
final String[] runOnOpts = getRunOpts(ref);
final Object poolNameObj = ref.getProperty(Scheduler.PROPERTY_SCHEDULER_THREAD_POOL);
final String poolName;
if (poolNameObj != null && poolNameObj.toString().trim().length() > 0) {
poolName = poolNameObj.toString().trim();
} else {
poolName = null;
}
final ScheduleOptions options = scheduleOptions.name(name).canRunConcurrently((concurrent != null ? concurrent : true)).threadPoolName(poolName).onInstancesOnly(runOnOpts);
final long bundleId = ref.getBundle().getBundleId();
final Long serviceId = getLongProperty(ref, Constants.SERVICE_ID);
if (this.scheduler.schedule(bundleId, serviceId, job, options)) {
this.idToNameMap.put(serviceId, name);
} else {
logger.error("Scheduling service {} failed.", ref);
}
}
use of org.apache.sling.commons.scheduler.ScheduleOptions in project sling by apache.
the class TestInitDelayingTopologyEventListener method createScheduler.
private Scheduler createScheduler() {
return new Scheduler() {
@Override
public boolean unschedule(String jobName) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean schedule(final Object job, ScheduleOptions options) {
if (job instanceof Runnable) {
final Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
((Runnable) job).run();
}
}, 300);
return true;
}
return false;
}
@Override
public void removeJob(String name) throws NoSuchElementException {
// TODO Auto-generated method stub
}
@Override
public boolean fireJobAt(String name, Object job, Map<String, Serializable> config, Date date, int times, long period) {
// TODO Auto-generated method stub
return false;
}
@Override
public void fireJobAt(String name, Object job, Map<String, Serializable> config, Date date) throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean fireJob(Object job, Map<String, Serializable> config, int times, long period) {
// TODO Auto-generated method stub
return false;
}
@Override
public void fireJob(Object job, Map<String, Serializable> config) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void addPeriodicJob(String name, Object job, Map<String, Serializable> config, long period, boolean canRunConcurrently, boolean startImmediate) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void addPeriodicJob(String name, Object job, Map<String, Serializable> config, long period, boolean canRunConcurrently) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void addJob(String name, Object job, Map<String, Serializable> config, String schedulingExpression, boolean canRunConcurrently) throws Exception {
// TODO Auto-generated method stub
}
@Override
public ScheduleOptions NOW(int times, long period) {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduleOptions NOW() {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduleOptions EXPR(String expression) {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduleOptions AT(Date date, int times, long period) {
// TODO Auto-generated method stub
return null;
}
@Override
public ScheduleOptions AT(Date date) {
// TODO Auto-generated method stub
return null;
}
};
}
use of org.apache.sling.commons.scheduler.ScheduleOptions in project sling by apache.
the class SimpleDistributionQueueProvider method enableQueueProcessing.
public void enableQueueProcessing(@Nonnull DistributionQueueProcessor queueProcessor, String... queueNames) {
if (checkpoint) {
// recover from checkpoints
log.debug("recovering from checkpoints if needed");
for (final String queueName : queueNames) {
log.debug("recovering for queue {}", queueName);
DistributionQueue queue = getQueue(queueName);
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return name.equals(queueName + "-checkpoint");
}
};
for (File qf : checkpointDirectory.listFiles(filenameFilter)) {
log.info("recovering from checkpoint {}", qf);
try {
LineIterator lineIterator = IOUtils.lineIterator(new FileReader(qf));
while (lineIterator.hasNext()) {
String s = lineIterator.nextLine();
String[] split = s.split(" ");
String id = split[0];
String infoString = split[1];
Map<String, Object> info = new HashMap<String, Object>();
JsonReader reader = Json.createReader(new StringReader(infoString));
JsonObject jsonObject = reader.readObject();
for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {
if (entry.getValue().getValueType().equals(JsonValue.ValueType.ARRAY)) {
JsonArray value = jsonObject.getJsonArray(entry.getKey());
String[] a = new String[value.size()];
for (int i = 0; i < a.length; i++) {
a[i] = value.getString(i);
}
info.put(entry.getKey(), a);
} else if (JsonValue.NULL.equals(entry.getValue())) {
info.put(entry.getKey(), null);
} else {
info.put(entry.getKey(), ((JsonString) entry.getValue()).getString());
}
}
queue.add(new DistributionQueueItem(id, info));
}
log.info("recovered {} items from queue {}", queue.getStatus().getItemsCount(), queueName);
} catch (FileNotFoundException e) {
log.warn("could not read checkpoint file {}", qf.getAbsolutePath());
} catch (JsonException e) {
log.warn("could not parse info from checkpoint file {}", qf.getAbsolutePath());
}
}
}
// enable checkpointing
for (String queueName : queueNames) {
ScheduleOptions options = scheduler.NOW(-1, 15).canRunConcurrently(false).name(getJobName(queueName + "-checkpoint"));
scheduler.schedule(new SimpleDistributionQueueCheckpoint(getQueue(queueName), checkpointDirectory), options);
}
}
// enable processing
for (String queueName : queueNames) {
ScheduleOptions options = scheduler.NOW(-1, 1).canRunConcurrently(false).name(getJobName(queueName));
scheduler.schedule(new SimpleDistributionQueueProcessor(getQueue(queueName), queueProcessor), options);
}
}
use of org.apache.sling.commons.scheduler.ScheduleOptions in project sling by apache.
the class SimpleDistributionQueueProviderTest method testDisableQueueProcessingWithCheckpointing.
@Test
public void testDisableQueueProcessingWithCheckpointing() throws Exception {
String name = "dummy-agent";
try {
Scheduler scheduler = mock(Scheduler.class);
ScheduleOptions options = mock(ScheduleOptions.class);
when(scheduler.NOW(-1, 10)).thenReturn(options);
when(options.canRunConcurrently(false)).thenReturn(options);
when(options.name(any(String.class))).thenReturn(options);
SimpleDistributionQueueProvider simpledistributionQueueProvider = new SimpleDistributionQueueProvider(scheduler, name, true);
simpledistributionQueueProvider.disableQueueProcessing();
} finally {
new File(name + "-simple-queues-checkpoints").deleteOnExit();
}
}
use of org.apache.sling.commons.scheduler.ScheduleOptions in project sling by apache.
the class SimpleDistributionQueueProviderTest method testEnableQueueProcessing.
@Test
public void testEnableQueueProcessing() throws Exception {
Scheduler scheduler = mock(Scheduler.class);
ScheduleOptions options = mock(ScheduleOptions.class);
when(scheduler.NOW(-1, 1)).thenReturn(options);
when(options.canRunConcurrently(false)).thenReturn(options);
when(options.name(any(String.class))).thenReturn(options);
String name = "dummy-agent";
SimpleDistributionQueueProvider simpledistributionQueueProvider = new SimpleDistributionQueueProvider(scheduler, name, false);
DistributionQueueProcessor processor = mock(DistributionQueueProcessor.class);
simpledistributionQueueProvider.enableQueueProcessing(processor);
}
Aggregations