use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.
the class DistributionQueueMBeanTest method verifyMBeanExposedValuesWhenHeadIsNotNull.
@Test
public void verifyMBeanExposedValuesWhenHeadIsNotNull() {
DistributionQueueStatus status = new DistributionQueueStatus(1, RUNNING);
Calendar joined = Calendar.getInstance();
DistributionQueueEntry entry = new DistributionQueueEntry("#entry", new DistributionQueueItem("#package", 1000L, new HashMap<String, Object>()), new DistributionQueueItemStatus(joined, QUEUED, 1, "#queue"));
DistributionQueue distributionQueue = mock(DistributionQueue.class);
when(distributionQueue.getName()).thenReturn("#distributionQueue");
when(distributionQueue.getHead()).thenReturn(entry);
when(distributionQueue.getType()).thenReturn(PARALLEL);
when(distributionQueue.getStatus()).thenReturn(status);
DistributionQueueMBean mBean = new DistributionQueueMBeanImpl(distributionQueue);
assertEquals(distributionQueue.getName(), mBean.getName());
assertEquals(distributionQueue.getType().name().toLowerCase(), mBean.getType());
assertEquals(1, mBean.getSize());
assertFalse(mBean.isEmpty());
assertEquals("#entry", mBean.getHeadId());
assertEquals(1, mBean.getHeadDequeuingAttempts());
assertEquals(QUEUED.name().toLowerCase(), mBean.getHeadStatus());
assertEquals(joined, mBean.getHeadEnqueuingDate());
}
use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.
the class SingleQueueDistributionStrategyTest method testPackageAdditionWithFailingItemDelivery.
@Test
public void testPackageAdditionWithFailingItemDelivery() throws Exception {
SingleQueueDispatchingStrategy singleQueueDistributionStrategy = new SingleQueueDispatchingStrategy();
DistributionPackage distributionPackage = mock(DistributionPackage.class);
DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
DistributionQueue queue = mock(DistributionQueue.class);
DistributionQueueItem queueItem = mock(DistributionQueueItem.class);
when(queueProvider.getQueue(DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME)).thenReturn(queue);
DistributionQueueItemStatus state = mock(DistributionQueueItemStatus.class);
when(queue.add(queueItem)).thenReturn(new DistributionQueueEntry(null, queueItem, state));
Iterable<DistributionQueueItemStatus> returnedStates = singleQueueDistributionStrategy.add(distributionPackage, queueProvider);
assertNotNull(returnedStates);
Iterator<DistributionQueueItemStatus> iterator = returnedStates.iterator();
assertNotNull(iterator);
assertTrue(iterator.hasNext());
assertNotNull(iterator.next());
}
use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.
the class SimpleDistributionAgentQueueProcessor method processQueueItem.
private boolean processQueueItem(String queueName, DistributionQueueEntry queueEntry) throws DistributionException {
boolean removeItemFromQueue = false;
ResourceResolver agentResourceResolver = null;
DistributionPackage distributionPackage = null;
DistributionQueueItem queueItem = queueEntry.getItem();
DistributionQueueItemStatus queueItemStatus = queueEntry.getStatus();
try {
String callingUser = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_USER, String.class);
String requestId = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_ID, String.class);
Long globalStartTime = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_START_TIME, Long.class);
agentResourceResolver = DistributionUtils.getResourceResolver(callingUser, authenticationInfo.getAgentService(), authenticationInfo.getSlingRepository(), authenticationInfo.getSubServiceName(), authenticationInfo.getResourceResolverFactory());
final long startTime = System.currentTimeMillis();
distributionPackage = distributionPackageExporter.getPackage(agentResourceResolver, queueItem.getPackageId());
if (distributionPackage != null) {
final long packageSize = distributionPackage.getSize();
DistributionPackageUtils.mergeQueueEntry(distributionPackage.getInfo(), queueEntry);
final DistributionRequestType requestType = distributionPackage.getInfo().getRequestType();
final String[] paths = distributionPackage.getInfo().getPaths();
try {
// import package
distributionPackageImporter.importPackage(agentResourceResolver, distributionPackage);
// generated event
distributionEventFactory.generatePackageEvent(DistributionEventTopics.AGENT_PACKAGE_DISTRIBUTED, DistributionComponentKind.AGENT, agentName, distributionPackage.getInfo());
removeItemFromQueue = true;
final long endTime = System.currentTimeMillis();
distributionLog.info("[{}] PACKAGE-DELIVERED {}: {} paths={}, importTime={}ms, execTime={}ms, size={}B", queueName, requestId, requestType, paths, endTime - startTime, endTime - globalStartTime, packageSize);
} catch (RecoverableDistributionException e) {
distributionLog.error("[{}] PACKAGE-FAIL {}: could not deliver {}, {}", queueName, requestId, distributionPackage.getId(), e.getMessage());
distributionLog.debug("could not deliver package {}", distributionPackage.getId(), e);
log.error("could not deliver package {}", distributionPackage.getId(), e);
} catch (Throwable e) {
distributionLog.error("[{}] PACKAGE-FAIL {}: could not deliver package {} {}", queueName, requestId, distributionPackage.getId(), e.getMessage(), e);
log.error("could not deliver package {} from queue {}", new Object[] { distributionPackage.getId(), queueName }, e);
if (errorQueueStrategy != null && queueItemStatus.getAttempts() > retryAttempts) {
removeItemFromQueue = reEnqueuePackage(distributionPackage);
distributionLog.info("[{}] PACKAGE-QUEUED {}: distribution package {} was enqueued to an error queue", queueName, requestId, distributionPackage.getId());
}
}
} else {
// return success if package does not exist in order to clear the queue.
removeItemFromQueue = true;
distributionLog.error("distribution package with id {} does not exist. the package will be skipped.", queueItem.getPackageId());
}
} finally {
if (removeItemFromQueue) {
DistributionPackageUtils.releaseOrDelete(distributionPackage, queueName);
} else {
DistributionPackageUtils.closeSafely(distributionPackage);
}
DistributionUtils.ungetResourceResolver(agentResourceResolver);
}
// return true if item should be removed from queue
return removeItemFromQueue;
}
use of org.apache.sling.distribution.queue.DistributionQueueItem 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);
}
use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.
the class JobHandlingUtilsTest method testFullPropertiesFromPackageCreation.
@Test
public void testFullPropertiesFromPackageCreation() throws Exception {
DistributionPackageInfo packageInfo = new DistributionPackageInfo("vlt");
packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_PATHS, new String[] { "/foo" });
packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_TYPE, DistributionRequestType.ADD);
packageInfo.put(DistributionPackageInfo.PROPERTY_PACKAGE_TYPE, "vlt");
DistributionQueueItem queueItem = new DistributionQueueItem("an-id", packageInfo);
Map<String, Object> fullPropertiesFromPackage = JobHandlingUtils.createFullProperties(queueItem);
assertNotNull(fullPropertiesFromPackage);
assertEquals(5, fullPropertiesFromPackage.size());
assertNotNull(fullPropertiesFromPackage.get("distribution.request.paths"));
assertNotNull(fullPropertiesFromPackage.get("distribution.item.id"));
assertNotNull(fullPropertiesFromPackage.get("distribution.package.size"));
assertNotNull(fullPropertiesFromPackage.get("distribution.package.type"));
assertNotNull(fullPropertiesFromPackage.get("distribution.request.type"));
}
Aggregations