use of org.apache.sling.distribution.queue.DistributionQueueEntry in project sling by apache.
the class SimpleDistributionQueue method remove.
@CheckForNull
public DistributionQueueEntry remove(@Nonnull String id) {
DistributionQueueEntry toRemove = getItem(id);
boolean removed = false;
if (toRemove != null) {
removed = queue.remove(toRemove.getItem());
}
log.debug("item with id {} removed from the queue: {}", id, removed);
if (removed) {
return toRemove;
} else {
return null;
}
}
use of org.apache.sling.distribution.queue.DistributionQueueEntry 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.DistributionQueueEntry 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);
}
use of org.apache.sling.distribution.queue.DistributionQueueEntry in project sling by apache.
the class SimpleDistributionQueueProcessorTest method testRunWithOneNonEmptyQueue.
@Test
public void testRunWithOneNonEmptyQueue() throws Exception {
SimpleDistributionQueueProvider queueProvider = mock(SimpleDistributionQueueProvider.class);
Collection<SimpleDistributionQueue> queues = new LinkedList<SimpleDistributionQueue>();
SimpleDistributionQueue queue = mock(SimpleDistributionQueue.class);
DistributionQueueItem item = mock(DistributionQueueItem.class);
when(queue.getHead()).thenReturn(new DistributionQueueEntry(null, item, null)).thenReturn(null);
queues.add(queue);
when(queueProvider.getQueues()).thenReturn(queues);
DistributionQueueProcessor queueProcessor = mock(DistributionQueueProcessor.class);
SimpleDistributionQueueProcessor simpleDistributionQueueProcessor = new SimpleDistributionQueueProcessor(queue, queueProcessor);
simpleDistributionQueueProcessor.run();
}
use of org.apache.sling.distribution.queue.DistributionQueueEntry in project sling by apache.
the class AgentDistributionPackageExporter method getPackage.
public DistributionPackage getPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull String distributionPackageId) {
try {
log.debug("getting package from queue {}", queueName);
DistributionQueue queue = getQueueOrThrow(queueName);
DistributionQueueEntry entry = queue.getItem(distributionPackageId);
DistributionPackage distributionPackage;
if (entry != null) {
DistributionQueueItem queueItem = entry.getItem();
DistributionPackageInfo info = DistributionPackageUtils.fromQueueItem(queueItem);
DistributionPackageBuilder packageBuilder = packageBuilderProvider.getPackageBuilder(info.getType());
if (packageBuilder != null) {
distributionPackage = packageBuilder.getPackage(resourceResolver, queueItem.getPackageId());
distributionPackage.getInfo().putAll(info);
log.debug("item {} fetched from the queue", info);
if (distributionPackage != null) {
return new AgentDistributionPackage(distributionPackage, queue, entry.getId());
}
} else {
log.warn("cannot find package builder with type {}", info.getType());
}
}
} catch (Exception ex) {
log.error("Error exporting package", ex);
}
return null;
}
Aggregations