Search in sources :

Example 1 with ScheduleOptions

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);
    }
}
Also used : ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions)

Example 2 with ScheduleOptions

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;
        }
    };
}
Also used : ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) Timer(java.util.Timer) TimerTask(java.util.TimerTask) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Map(java.util.Map) Date(java.util.Date)

Example 3 with ScheduleOptions

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);
    }
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FileNotFoundException(java.io.FileNotFoundException) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) LineIterator(org.apache.commons.io.LineIterator) FilenameFilter(java.io.FilenameFilter) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) FileReader(java.io.FileReader) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JsonValue(javax.json.JsonValue) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) JsonArray(javax.json.JsonArray) ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with ScheduleOptions

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();
    }
}
Also used : ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) Scheduler(org.apache.sling.commons.scheduler.Scheduler) File(java.io.File) Test(org.junit.Test)

Example 5 with ScheduleOptions

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);
}
Also used : DistributionQueueProcessor(org.apache.sling.distribution.queue.DistributionQueueProcessor) ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Test(org.junit.Test)

Aggregations

ScheduleOptions (org.apache.sling.commons.scheduler.ScheduleOptions)21 Scheduler (org.apache.sling.commons.scheduler.Scheduler)8 Test (org.junit.Test)7 File (java.io.File)4 Date (java.util.Date)3 DistributionQueueProcessor (org.apache.sling.distribution.queue.DistributionQueueProcessor)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 LoginException (org.apache.sling.api.resource.LoginException)2 DistributionException (org.apache.sling.distribution.common.DistributionException)2 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)2 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)2 DistributionRequestHandler (org.apache.sling.distribution.trigger.DistributionRequestHandler)2 Workspace (com.adobe.acs.commons.workflow.bulk.execution.model.Workspace)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 StringReader (java.io.StringReader)1