use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.
the class SimpleDistributionQueue method getState.
@Nonnull
private DistributionQueueState getState() {
DistributionQueueItem firstItem = queue.peek();
DistributionQueueItemStatus firstItemStatus = firstItem != null ? statusMap.get(firstItem) : null;
return DistributionQueueUtils.calculateState(firstItem, firstItemStatus);
}
use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.
the class SimpleDistributionQueueCheckpoint method run.
@Override
public void run() {
String fileName = queue.getName() + "-checkpoint";
File checkpointFile = new File(checkpointDirectory, fileName + "-new");
log.debug("started checkpointing");
try {
if (checkpointFile.exists()) {
assert checkpointFile.delete();
}
assert checkpointFile.createNewFile();
Collection<String> lines = new LinkedList<String>();
FileOutputStream fileOutputStream = new FileOutputStream(checkpointFile);
for (DistributionQueueEntry queueEntry : queue.getItems(0, -1)) {
DistributionQueueItem item = queueEntry.getItem();
String packageId = item.getPackageId();
StringWriter w = new StringWriter();
JsonGenerator jsonWriter = Json.createGenerator(w);
jsonWriter.writeStartObject();
for (Map.Entry<String, Object> entry : item.entrySet()) {
Object value = entry.getValue();
boolean isArray = value instanceof String[];
if (isArray) {
jsonWriter.writeStartArray(entry.getKey());
for (String s : ((String[]) value)) {
jsonWriter.write(s);
}
jsonWriter.writeEnd();
} else {
jsonWriter.write(entry.getKey(), (String) value);
}
}
jsonWriter.writeEnd();
jsonWriter.close();
lines.add(packageId + " " + w.toString());
}
log.debug("parsed {} items", lines.size());
IOUtils.writeLines(lines, Charset.defaultCharset().name(), fileOutputStream, Charset.defaultCharset());
fileOutputStream.flush();
fileOutputStream.close();
boolean success = checkpointFile.renameTo(new File(checkpointDirectory, fileName));
log.debug("checkpoint succeeded: {}", success);
} catch (Exception e) {
log.error("failed checkpointing for queue {}", queue.getName());
}
}
use of org.apache.sling.distribution.queue.DistributionQueueItem 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.distribution.queue.DistributionQueueItem 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());
}
use of org.apache.sling.distribution.queue.DistributionQueueItem 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);
}
Aggregations