Search in sources :

Example 1 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class JobHandlingDistributionQueueProvider method getQueue.

@Nonnull
public DistributionQueue getQueue(@Nonnull String queueName) {
    String topic = JobHandlingDistributionQueue.DISTRIBUTION_QUEUE_TOPIC + '/' + prefix + "/" + queueName;
    boolean isActive = jobConsumer != null && (processingQueueNames == null || processingQueueNames.contains(queueName));
    DistributionQueue queue = new JobHandlingDistributionQueue(queueName, topic, jobManager, isActive, DistributionQueueType.ORDERED);
    queue = new CachingDistributionQueue(topic, queue);
    return queue;
}
Also used : CachingDistributionQueue(org.apache.sling.distribution.queue.impl.CachingDistributionQueue) CachingDistributionQueue(org.apache.sling.distribution.queue.impl.CachingDistributionQueue) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) Nonnull(javax.annotation.Nonnull)

Example 2 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue 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 3 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class SimpleDistributionQueueTest method testPackageAddition.

@Test
public void testPackageAddition() throws Exception {
    DistributionQueue queue = new SimpleDistributionQueue("agentName", "default");
    DistributionQueueItem pkg = mock(DistributionQueueItem.class);
    assertNotNull(queue.add(pkg));
    assertFalse(queue.getStatus().isEmpty());
}
Also used : DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Example 4 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class SimpleDistributionQueueTest method testPackageAdditionAndRemoval.

@Test
public void testPackageAdditionAndRemoval() throws Exception {
    DistributionQueue queue = new SimpleDistributionQueue("agentName", "default");
    DistributionQueueItem pkg = mock(DistributionQueueItem.class);
    when(pkg.getPackageId()).thenReturn("id");
    assertNotNull(queue.add(pkg));
    assertFalse(queue.getStatus().isEmpty());
    assertNotNull(queue.remove(pkg.getPackageId()));
    assertTrue(queue.getStatus().isEmpty());
    DistributionQueueEntry entry = queue.getItem(pkg.getPackageId());
    assertNull(entry);
}
Also used : DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Example 5 with DistributionQueue

use of org.apache.sling.distribution.queue.DistributionQueue in project sling by apache.

the class JobHandlingDistributionQueueTest method testPackageAdditionAndStatusCheck.

@SuppressWarnings("unchecked")
@Test
public void testPackageAdditionAndStatusCheck() throws Exception {
    JobManager jobManager = mock(JobManager.class);
    JobBuilder builder = mock(JobBuilder.class);
    when(builder.properties(any(Map.class))).thenReturn(builder);
    Job job = mock(Job.class);
    when(job.getId()).thenReturn("id-123");
    when(builder.add()).thenReturn(job);
    String topic = JobHandlingDistributionQueue.DISTRIBUTION_QUEUE_TOPIC + "/aname";
    when(jobManager.createJob(topic)).thenReturn(builder);
    when(jobManager.getJobById(anyString())).thenReturn(job);
    when(builder.properties(any(Map.class))).thenReturn(builder);
    DistributionQueue queue = new JobHandlingDistributionQueue("aname", topic, jobManager, true, DistributionQueueType.ORDERED);
    DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
    packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_PATHS, new String[] { "/foo" });
    packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_TYPE, DistributionRequestType.ADD);
    DistributionQueueItem distributionQueueItem = new DistributionQueueItem("an-id", packageInfo);
    assertNotNull(queue.add(distributionQueueItem));
    DistributionQueueItemStatus status = queue.getItem(job.getId()).getStatus();
    assertNotNull(status);
    assertEquals(DistributionQueueItemState.QUEUED, status.getItemState());
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JobBuilder(org.apache.sling.event.jobs.JobBuilder) JobManager(org.apache.sling.event.jobs.JobManager) Matchers.anyString(org.mockito.Matchers.anyString) Job(org.apache.sling.event.jobs.Job) Map(java.util.Map) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Aggregations

DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)40 Test (org.junit.Test)25 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)20 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)18 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)14 HashMap (java.util.HashMap)8 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)8 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)7 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)7 ArrayList (java.util.ArrayList)6 File (java.io.File)5 Map (java.util.Map)5 DistributionAgent (org.apache.sling.distribution.agent.DistributionAgent)5 Matchers.anyString (org.mockito.Matchers.anyString)5 DistributionException (org.apache.sling.distribution.common.DistributionException)4 SharedDistributionPackage (org.apache.sling.distribution.packaging.impl.SharedDistributionPackage)4 DistributionQueueStatus (org.apache.sling.distribution.queue.DistributionQueueStatus)4 Result (org.apache.sling.hc.api.Result)4 LinkedList (java.util.LinkedList)3 Scheduler (org.apache.sling.commons.scheduler.Scheduler)3