Search in sources :

Example 1 with SharedDistributionPackage

use of org.apache.sling.distribution.packaging.impl.SharedDistributionPackage in project sling by apache.

the class AsyncDeliveryDispatchingStrategy method add.

@Override
public Iterable<DistributionQueueItemStatus> add(@Nonnull DistributionPackage distributionPackage, @Nonnull DistributionQueueProvider queueProvider) throws DistributionException {
    if (!(distributionPackage instanceof SharedDistributionPackage) && deliveryMappings.size() > 1) {
        throw new DistributionException("distribution package must be a shared package to be added in multiple queues");
    }
    String distributionPackageId = distributionPackage.getId();
    List<DistributionQueueItemStatus> result = new LinkedList<DistributionQueueItemStatus>();
    for (String referenceQueueName : deliveryMappings.keySet()) {
        DistributionQueue queue = queueProvider.getQueue(referenceQueueName);
        int MAX_QUEUE_ITEMS_THRESHOLD = 100;
        if (queue.getStatus().getItemsCount() > MAX_QUEUE_ITEMS_THRESHOLD) {
            // too many items in the queue, let's send actual packages and references separately
            distributionPackage.getInfo().put("reference-required", true);
            DistributionQueueItem item = getItem(distributionPackage);
            // create and acquire reference package
            ReferencePackage referencePackage = new ReferencePackage(distributionPackage);
            DistributionPackageUtils.acquire(referencePackage, referenceQueueName);
            // acquire actual package
            String deliveryQueueName = deliveryMappings.get(referenceQueueName);
            DistributionPackageUtils.acquire(distributionPackage, deliveryQueueName);
            // add the actual package to the delivery queue
            DistributionQueue deliveryQueue = queueProvider.getQueue(deliveryQueueName);
            DistributionQueueEntry deliveryQueueEntry = deliveryQueue.add(item);
            if (deliveryQueueEntry != null) {
                DistributionQueueItemStatus status = deliveryQueueEntry.getStatus();
                log.debug("item {} added to delivery queue: {}", item, status);
                result.add(status);
            } else {
                DistributionPackageUtils.release(distributionPackage, deliveryQueueName);
                log.error("cannot add package {} to delivery queue {}", distributionPackageId, deliveryQueueName);
                result.add(new DistributionQueueItemStatus(DistributionQueueItemState.ERROR, deliveryQueue.getName()));
            }
            // add the reference package to the reference queue
            DistributionQueue referenceQueue = queueProvider.getQueue(referenceQueueName);
            DistributionQueueItem referenceQueueItem = getItem(referencePackage);
            DistributionQueueEntry referenceQueueEntry = referenceQueue.add(referenceQueueItem);
            if (referenceQueueEntry != null) {
                DistributionQueueItemStatus status = referenceQueueEntry.getStatus();
                log.debug("item {} added to reference queue: {}", referenceQueueItem, status);
            } else {
                DistributionPackageUtils.release(referencePackage, referenceQueueName);
                log.error("cannot add package {} to reference queue {}", distributionPackageId, referenceQueueName);
                result.add(new DistributionQueueItemStatus(DistributionQueueItemState.ERROR, referenceQueue.getName()));
            }
        } else {
            // normal queueing
            DistributionQueueItem item = getItem(distributionPackage);
            DistributionQueueItemStatus status = new DistributionQueueItemStatus(DistributionQueueItemState.ERROR, queue.getName());
            DistributionPackageUtils.acquire(distributionPackage, referenceQueueName);
            DistributionQueueEntry queueEntry = queue.add(item);
            if (queueEntry != null) {
                status = queueEntry.getStatus();
                log.debug("item {} added to queue: {}", item, status);
            } else {
                DistributionPackageUtils.release(distributionPackage, referenceQueueName);
                log.error("cannot add package {} to queue {}", distributionPackageId, referenceQueueName);
            }
            result.add(status);
        }
    }
    return result;
}
Also used : ReferencePackage(org.apache.sling.distribution.packaging.impl.ReferencePackage) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) SharedDistributionPackage(org.apache.sling.distribution.packaging.impl.SharedDistributionPackage) DistributionException(org.apache.sling.distribution.common.DistributionException) LinkedList(java.util.LinkedList) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem)

Example 2 with SharedDistributionPackage

use of org.apache.sling.distribution.packaging.impl.SharedDistributionPackage in project sling by apache.

the class MultipleQueueDispatchingStrategy method add.

public Iterable<DistributionQueueItemStatus> add(@Nonnull DistributionPackage distributionPackage, @Nonnull DistributionQueueProvider queueProvider) throws DistributionException {
    if (!(distributionPackage instanceof SharedDistributionPackage) && queueNames.length > 1) {
        throw new DistributionException("distribution package must be a shared package to be added in multiple queues");
    }
    DistributionQueueItem queueItem = getItem(distributionPackage);
    List<DistributionQueueItemStatus> result = new ArrayList<DistributionQueueItemStatus>();
    // first acquire the package for all queues
    DistributionPackageUtils.acquire(distributionPackage, queueNames);
    // second add the package to all queues
    for (String queueName : queueNames) {
        DistributionQueue queue = queueProvider.getQueue(queueName);
        DistributionQueueItemStatus status = new DistributionQueueItemStatus(DistributionQueueItemState.ERROR, queue.getName());
        DistributionQueueEntry queueEntry = queue.add(queueItem);
        if (queueEntry != null) {
            status = queueEntry.getStatus();
        } else {
            DistributionPackageUtils.release(distributionPackage, queueName);
            log.error("cannot add package {} to queue {}", distributionPackage.getId(), queueName);
        }
        result.add(status);
    }
    return result;
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) ArrayList(java.util.ArrayList) SharedDistributionPackage(org.apache.sling.distribution.packaging.impl.SharedDistributionPackage) DistributionException(org.apache.sling.distribution.common.DistributionException) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem)

Example 3 with SharedDistributionPackage

use of org.apache.sling.distribution.packaging.impl.SharedDistributionPackage in project sling by apache.

the class AsyncDeliveryDispatchingStrategyTest method testAddWithOneItemInRunningQueues.

@Test
public void testAddWithOneItemInRunningQueues() throws Exception {
    Map<String, String> deliveryMappings = new HashMap<String, String>();
    String queue1 = "queue1";
    deliveryMappings.put(queue1, "delivery1");
    String queue2 = "queue2";
    deliveryMappings.put(queue2, "delivery2");
    AsyncDeliveryDispatchingStrategy asyncDeliveryDispatchingStrategy = new AsyncDeliveryDispatchingStrategy(deliveryMappings);
    // setup package
    SharedDistributionPackage distributionPackage = mock(SharedDistributionPackage.class);
    when(distributionPackage.getId()).thenReturn("1221312");
    DistributionPackageInfo info = new DistributionPackageInfo("dummy", new HashMap<String, Object>());
    when(distributionPackage.getInfo()).thenReturn(info);
    // setup queues
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueue dq1 = mock(DistributionQueue.class);
    DistributionQueueItemStatus status = mock(DistributionQueueItemStatus.class);
    DistributionQueueEntry entry = new DistributionQueueEntry("1242112", new DistributionQueueItem(distributionPackage.getId(), new HashMap<String, Object>()), status);
    when(dq1.add(any(DistributionQueueItem.class))).thenReturn(entry);
    DistributionQueueStatus status1 = new DistributionQueueStatus(1, DistributionQueueState.RUNNING);
    when(dq1.getStatus()).thenReturn(status1);
    when(queueProvider.getQueue(queue1)).thenReturn(dq1);
    DistributionQueue dq2 = mock(DistributionQueue.class);
    when(dq2.add(any(DistributionQueueItem.class))).thenReturn(entry);
    DistributionQueueStatus status2 = new DistributionQueueStatus(1, DistributionQueueState.RUNNING);
    when(dq2.getStatus()).thenReturn(status2);
    when(queueProvider.getQueue(queue2)).thenReturn(dq2);
    Iterable<DistributionQueueItemStatus> statuses = asyncDeliveryDispatchingStrategy.add(distributionPackage, queueProvider);
    assertNotNull(statuses);
}
Also used : DistributionQueueStatus(org.apache.sling.distribution.queue.DistributionQueueStatus) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) HashMap(java.util.HashMap) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) SharedDistributionPackage(org.apache.sling.distribution.packaging.impl.SharedDistributionPackage) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) Test(org.junit.Test)

Aggregations

SharedDistributionPackage (org.apache.sling.distribution.packaging.impl.SharedDistributionPackage)3 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)3 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)3 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)3 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)3 DistributionException (org.apache.sling.distribution.common.DistributionException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)1 ReferencePackage (org.apache.sling.distribution.packaging.impl.ReferencePackage)1 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)1 DistributionQueueStatus (org.apache.sling.distribution.queue.DistributionQueueStatus)1 Test (org.junit.Test)1