Search in sources :

Example 6 with Scheduler

use of org.apache.sling.commons.scheduler.Scheduler in project sling by apache.

the class MinEventDelayHandler method runAfter.

/**
     * run the runnable after the indicated number of seconds, once.
     * @return true if the scheduling of the runnable worked, false otherwise
     */
private boolean runAfter(long seconds, final Runnable runnable) {
    final Scheduler theScheduler = scheduler;
    if (theScheduler == null) {
        logger.info("runAfter: no scheduler set");
        return false;
    }
    logger.trace("runAfter: trying with scheduler.fireJob");
    final Date date = new Date(System.currentTimeMillis() + seconds * 1000);
    try {
        theScheduler.fireJobAt(null, runnable, null, date);
        return true;
    } catch (Exception e) {
        logger.info("runAfter: could not schedule a job: " + e);
        return false;
    }
}
Also used : Scheduler(org.apache.sling.commons.scheduler.Scheduler) Date(java.util.Date)

Example 7 with Scheduler

use of org.apache.sling.commons.scheduler.Scheduler in project sling by apache.

the class AbstractJcrEventTriggerTest method addToListTest.

//"SLING-6054"
@Test
public void addToListTest() throws Exception {
    SlingRepository repository = mock(SlingRepository.class);
    Scheduler scheduler = mock(Scheduler.class);
    String path = "/";
    String serviceUser = "service-user";
    AbstractJcrEventTrigger trigger = new AbstractJcrEventTrigger(repository, scheduler, rrf, path, serviceUser) {

        @Override
        protected DistributionRequest processEvent(Event event) throws RepositoryException {
            return null;
        }
    };
    String descendant = "/a/b/c/d/e/f/h";
    String ancestor = "/a/b/c/d";
    List<DistributionRequest> requests = new LinkedList<DistributionRequest>();
    requests.add(new SimpleDistributionRequest(DistributionRequestType.ADD, descendant));
    DistributionRequest newRequest = new SimpleDistributionRequest(DistributionRequestType.ADD, ancestor);
    trigger.addToList(newRequest, requests);
    assertEquals(1, requests.size());
    assertEquals(3, requests.get(0).getPaths().length);
    String[] paths = requests.get(0).getPaths();
    assertEquals(ancestor, paths[0]);
    // the missing path is added
    assertEquals("/a/b/c/d/e/f/g", paths[1]);
    assertEquals(descendant, paths[2]);
    // invert order of requests
    requests = new LinkedList<DistributionRequest>();
    requests.add(new SimpleDistributionRequest(DistributionRequestType.ADD, ancestor));
    newRequest = new SimpleDistributionRequest(DistributionRequestType.ADD, descendant);
    trigger.addToList(newRequest, requests);
    assertEquals(1, requests.size());
    assertEquals(3, requests.get(0).getPaths().length);
    paths = requests.get(0).getPaths();
    assertEquals(ancestor, paths[0]);
    // the missing path is added
    assertEquals("/a/b/c/d/e/f/g", paths[1]);
    assertEquals(descendant, paths[2]);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) DistributionRequest(org.apache.sling.distribution.DistributionRequest) SimpleDistributionRequest(org.apache.sling.distribution.SimpleDistributionRequest) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Event(javax.jcr.observation.Event) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 8 with Scheduler

use of org.apache.sling.commons.scheduler.Scheduler in project sling by apache.

the class JcrEventDistributionTriggerTest method testProcessEventOnMultipleIgnoredPattern.

@Test
public void testProcessEventOnMultipleIgnoredPattern() throws Exception {
    SlingRepository repository = mock(SlingRepository.class);
    Scheduler scheduler = mock(Scheduler.class);
    ResourceResolverFactory resolverFactory = mock(ResourceResolverFactory.class);
    String path = "/home/users";
    String serviceName = "serviceId";
    String[] ignoredPaths = new String[] { ".*/.tokens.*", ".*/.rep:cache.*" };
    JcrEventDistributionTrigger jcrEventdistributionTrigger = new JcrEventDistributionTrigger(repository, scheduler, resolverFactory, path, false, serviceName, ignoredPaths);
    Event event = mock(Event.class);
    when(event.getPath()).thenReturn("/home/users/3/3U3HxUUzJJ60BdN4lEDJ/.tokens/2017-01-10T15.52.37.842+01.00");
    DistributionRequest distributionRequest = jcrEventdistributionTrigger.processEvent(event);
    assertNull(distributionRequest);
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Scheduler(org.apache.sling.commons.scheduler.Scheduler) Event(javax.jcr.observation.Event) Test(org.junit.Test)

Example 9 with Scheduler

use of org.apache.sling.commons.scheduler.Scheduler 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 10 with Scheduler

use of org.apache.sling.commons.scheduler.Scheduler 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

Scheduler (org.apache.sling.commons.scheduler.Scheduler)29 Test (org.junit.Test)25 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)10 Event (javax.jcr.observation.Event)9 DistributionRequest (org.apache.sling.distribution.DistributionRequest)9 SlingRepository (org.apache.sling.jcr.api.SlingRepository)9 ScheduleOptions (org.apache.sling.commons.scheduler.ScheduleOptions)8 DistributionRequestHandler (org.apache.sling.distribution.trigger.DistributionRequestHandler)6 Session (javax.jcr.Session)4 File (java.io.File)3 Node (javax.jcr.Node)3 DistributionQueueProcessor (org.apache.sling.distribution.queue.DistributionQueueProcessor)3 Date (java.util.Date)2 Workspace (javax.jcr.Workspace)2 ObservationManager (javax.jcr.observation.ObservationManager)2 DistributionTransportSecretProvider (org.apache.sling.distribution.transport.DistributionTransportSecretProvider)2 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1