為卸載建立並佔用作業 creating-and-consuming-jobs-for-offloading
Apache Sling Discovery功能提供Java API,可讓您建立使用JobManager作業和JobConsumer服務。
如需建立卸載拓撲和配置主題消耗的相關資訊,請參閱 卸載作業.
處理作業負載 handling-job-payloads
卸載框架定義了用於標識作業裝載的兩個作業屬性。 卸載複製代理使用這些屬性來標識要複製到拓撲中實例的資源:
offloading.job.input.payload
:以逗號分隔的內容路徑清單。 內容會複製到執行作業的執行個體。offloading.job.output.payload
:以逗號分隔的內容路徑清單。 作業執行完成後,作業裝載將複製到建立作業的執行個體上的這些路徑。
使用 OffloadingJobProperties
枚舉以引用屬性名稱:
OffloadingJobProperties.INPUT_PAYLOAD.propertyName()
OffloadingJobProperties.OUTPUT_PAYLOAD.propetyName()
作業不需要裝載。 但是,如果作業需要操作資源,並且作業被卸載到未建立該作業的電腦上,則需要裝載。
建立卸載作業 creating-jobs-for-offloading
建立調用JobManager.addJob方法的客戶端,以建立自動選擇的JobConsumer執行的作業。 提供以下資訊以建立作業:
- 主題:工作主題。
- 名稱:(可選)
- 屬性映射:A
Map<String, Object>
包含任何數量屬性的物件,例如輸入裝載路徑和輸出裝載路徑。 此映射對象可用於執行該作業的JobConsumer對象。
下列範例服務會為指定主題和輸入裝載路徑建立工作。
package com.adobe.example.offloading;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Reference;
import java.util.HashMap;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceResolver;
import com.adobe.granite.offloading.api.OffloadingJobProperties;
@Component
@Service
public class JobGeneratorImpl implements JobGenerator {
@Reference
private JobManager jobManager;
@Reference ResourceResolverFactory resolverFactory;
public String createJob(String topic, String payload) throws Exception {
Job offloadingJob;
ResourceResolver resolver = resolverFactory.getResourceResolver(null);
if(resolver.getResource(payload)!=null){
HashMap<String, Object> jobprops = new HashMap<String, Object>();
jobprops.put(OffloadingJobProperties.INPUT_PAYLOAD.propertyName(), payload);
offloadingJob = jobManager.addJob(topic, null, jobprops);
} else {
throw new Exception("Payload for job cannot be found");
}
if (offloadingJob == null){
throw new Exception ("Offloading job could not be created");
}
return offloadingJob.getId();
}
}
為 com/adobe/example/offloading
主題和 /content/geometrixx/de/services
裝載:
10.06.2013 15:43:33.868 *INFO* [JobHandler: /etc/workflow/instances/2013-06-10/model_1554418768647484:/content/geometrixx/en/company] com.adobe.example.offloading.JobGeneratorImpl Received request to make job for topic com/adobe/example/offloading and payload /content/geometrixx/de/services
發展就業消費者 developing-a-job-consumer
若要使用工作,請開發實施 org.apache.sling.event.jobs.consumer.JobConsumer
介面。 識別要使用的主題,使用 JobConsumer.PROPERTY_TOPICS
屬性。
下列範例JobConsumer實作註冊至 com/adobe/example/offloading
主題。 使用者只會將裝載內容節點的「已使用」屬性設為true。
package com.adobe.example.offloading;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Session;
import javax.jcr.Node;
import com.adobe.granite.offloading.api.OffloadingJobProperties;
@Component
@Service
public class MyJobConsumer implements JobConsumer {
public static final String TOPIC = "com/adobe/example/offloading";
@Property(value = TOPIC)
static final String myTopic = JobConsumer.PROPERTY_TOPICS;
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
private JobManager jobManager;
private final Logger log = LoggerFactory.getLogger(getClass());
public JobResult process(Job job) {
JobResult result = JobResult.FAILED;
String topic = job.getTopic();
log.info("Consuming job of topic: {}", topic);
String payloadIn = (String) job.getProperty(OffloadingJobProperties.INPUT_PAYLOAD.propertyName());
String payloadOut = (String) job.getProperty(OffloadingJobProperties.OUTPUT_PAYLOAD.propertyName());
log.info("Job has Input Payload {} and Output Payload {}",payloadIn, payloadOut);
ResourceResolver resolver = null;
try {
resolver = resolverFactory.getAdministrativeResourceResolver(null);
Session session = resolver.adaptTo(Session.class);
Node inNode = session.getNode(payloadIn);
inNode.getNode(Node.JCR_CONTENT).setProperty("consumed",true);
result = JobResult.OK;
}catch (Exception e){
log.info("ERROR -- JOB RESULT IS FAILURE " + e.getMessage());
result = JobResult.FAILED;
}
log.info("Job OK for payload {}",payloadIn);
return result;
}
}
MyJobConsumer類別會為/content/geometrixx/de/services的輸入裝載產生下列記錄訊息:
10.06.2013 16:02:40.803 *INFO* [pool-7-thread-17-<main queue>(com/adobe/example/offloading)] com.adobe.example.offloading.MyJobConsumer Consuming job of topic: com/adobe/example/offloading
10.06.2013 16:02:40.803 *INFO* [pool-7-thread-17-<main queue>(com/adobe/example/offloading)] com.adobe.example.offloading.MyJobConsumer Job has Input Payload /content/geometrixx/de/services and Output Payload /content/geometrixx/de/services
10.06.2013 16:02:40.884 *INFO* [pool-7-thread-17-<main queue>(com/adobe/example/offloading)] com.adobe.example.offloading.MyJobConsumer Job OK for payload /content/geometrixx/de/services
可使用CRXDE Lite來觀察已使用屬性:
Maven相依性 maven-dependencies
將下列相依性防禦項目新增至您的pom.xml檔案,讓Maven可解析卸載相關類別。
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.event</artifactId>
<version>3.1.5-R1485539</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.adobe.granite</groupId>
<artifactId>com.adobe.granite.offloading.core</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
前面的示例還需要以下依賴項定義:
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
<version>2.4.3-R1488084</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.jcr.jcr-wrapper</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
recommendation-more-help
2315f3f5-cb4a-4530-9999-30c8319c520e