Skip to content

Commit

Permalink
Merge pull request #18 from ecm4u/process-in-thread
Browse files Browse the repository at this point in the history
Unwrap NativeJavaObject and process in thread
  • Loading branch information
jgoldhammer authored Dec 4, 2017
2 parents e613883 + e779aaf commit 9a665e3
Showing 1 changed file with 153 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.mozilla.javascript.NativeJavaObject;

/**
* fascade to a de.jgoldhammer.alfresco.jscript.batch processor implementation.
Expand Down Expand Up @@ -100,7 +101,7 @@ public void runForQuery(String batchName, int workerThreads, final int batchSize
if (searchResult.length() != 0) {
searchResult.setBulkFetch(false);

processor = new BatchProcessor<Collection<NodeRef>>(batchName, transactionService.getRetryingTransactionHelper(),
processor = new BatchProcessor<>(batchName, transactionService.getRetryingTransactionHelper(),
new QueryResultBatchProcessWorkProvider(searchResult, batchSize), workerThreads, FIXED_BATCH_SIZE, null,
null, FIXED_BATCH_SIZE);

Expand Down Expand Up @@ -154,13 +155,135 @@ public void runForNodes(String batchName, int workerThreads, final int batchSize
final Scriptable batchScope = this.scope;
List<NodeRef> nodeRefs = convertScriptNodesArray(scriptNodes);

processor = new BatchProcessor<Collection<NodeRef>>(batchName, transactionService.getRetryingTransactionHelper(),
processor = new BatchProcessor<>(batchName, transactionService.getRetryingTransactionHelper(),
new SimpleListWorkProvider(nodeRefs, batchSize), workerThreads, FIXED_BATCH_SIZE, null, null, FIXED_BATCH_SIZE);

processor.process(new ScriptedBatchProcessWorker(runAsSystem, batchScope, processorFunction, nodeRefs,
beforeProcessFunction, afterProcessFunction, Context.getCurrentContext(), serviceRegistry, scriptService), true);
processor.process(new ScriptedBatchProcessWorker(runAsSystem, batchScope, processorFunction, nodeRefs,
beforeProcessFunction, afterProcessFunction, Context.getCurrentContext(), serviceRegistry, scriptService), true);

}

/**
* process the given processorfunction on a set of nodes which are given as
* native array. The processing takes place in a de.jgoldhammer.alfresco.jscript.batch processor with the
* given batchName, the number of workerthreads and the number of nodes as
* batchsize.
*
* @param batchName
* the name of the de.jgoldhammer.alfresco.jscript.batch
* @param workerThreads
* the number of threads which can be used for the de.jgoldhammer.alfresco.jscript.batch
* processing
* @param batchSize
* size of the batch
* @param nodeRefsAsStrings
* the array of NodeRef strings to process (if you have your own
* logic to determine the nodes)
* @param processorFunction
* the javascript function to process- the function must have the
* named "process"
*
* Example: de.jgoldhammer.alfresco.jscript.batch.runForNodes('MyProcessor', 4, 10, nodes,
* function process(node){ logger.error(node); }, true);
* @param runAsSystem
* true if the processing should be run as system, false to
* process as the current user
* @param beforeProcessFunction
* the function to run before the processing
* @param afterProcessFunction
* the function to run after the processing
*
*/
@ScriptMethod(code = "de.jgoldhammer.alfresco.jscript.batch.run('MyProcessor',4,10,'TEXT:alfresco',function process(node){logger.error(node);}, true);", help = "", output = "nothing", type = ScriptMethodType.WRITE)
public void runForNodeRefs(String batchName, int workerThreads, final int batchSize, final NativeArray nodeRefsAsStrings,
final String processorFunction, final boolean runAsSystem, final String beforeProcessFunction,
final String afterProcessFunction) {

BatchProcessor<Collection<NodeRef>> processor;
final Scriptable batchScope = this.scope;
List<NodeRef> nodeRefs = convertNodeRefsAsStringsArray(nodeRefsAsStrings);

processor = new BatchProcessor<>(batchName, transactionService.getRetryingTransactionHelper(),
new SimpleListWorkProvider(nodeRefs, batchSize), workerThreads, FIXED_BATCH_SIZE, null, null, FIXED_BATCH_SIZE);

processor.process(new ScriptedBatchProcessWorker(runAsSystem, batchScope, processorFunction, nodeRefs,
beforeProcessFunction, afterProcessFunction, Context.getCurrentContext(), serviceRegistry, scriptService), true);

}

/**
* Process the given processorfunction on a set of nodes in which are given as
* native array in a background Thread. The processing takes place in a de.jgoldhammer.alfresco.jscript.batch processor with the
* given batchName, the number of workerthreads and the number of nodes as
* batchsize.
*
* @param batchName
* the name of the de.jgoldhammer.alfresco.jscript.batch
* @param workerThreads
* the number of threads which can be used for the de.jgoldhammer.alfresco.jscript.batch
* processing
* @param batchSize
* size of the batch
* @param scriptNodes
* the array of scriptnodes to process (if you have your own
* logic to determine the nodes)
* @param processorFunction
* the javascript function to process- the function must have the
* named "process"
*
* Example: de.jgoldhammer.alfresco.jscript.batch.runForNodes('MyProcessor', 4, 10, nodes,
* function process(node){ logger.error(node); }, true);
* @param runAsSystem
* true if the processing should be run as system, false to
* process as the current user
* @param beforeProcessFunction
* the function to run before the processing
* @param afterProcessFunction
* the function to run after the processing
*
*/
@ScriptMethod(code = "de.jgoldhammer.alfresco.jscript.batch.run('MyProcessor',4,10,'TEXT:alfresco',function process(node){logger.error(node);}, true);", help = "", output = "nothing", type = ScriptMethodType.WRITE)
public void runForNodesInThread(String batchName, int workerThreads, final int batchSize, final NativeArray scriptNodes,
final String processorFunction, final boolean runAsSystem, final String beforeProcessFunction,
final String afterProcessFunction) {

BatchProcessor<Collection<NodeRef>> processor;
final Scriptable batchScope = this.scope;
List<NodeRef> nodeRefs = convertScriptNodesArray(scriptNodes);

processor = new BatchProcessor<>(batchName, transactionService.getRetryingTransactionHelper(),
new SimpleListWorkProvider(nodeRefs, batchSize), workerThreads, FIXED_BATCH_SIZE, null, null, FIXED_BATCH_SIZE);

final ScriptedBatchProcessWorker scriptedBatchProcessWorker = new ScriptedBatchProcessWorker(
runAsSystem,
batchScope,
processorFunction,
nodeRefs,
beforeProcessFunction,
afterProcessFunction,
Context.getCurrentContext(),
serviceRegistry,
scriptService);

Runner p = new Runner(processor, scriptedBatchProcessWorker);
new Thread(p).start();
}

private static class Runner implements Runnable {

private final BatchProcessor<Collection<NodeRef>> processor;
private final ScriptedBatchProcessWorker scriptedBatchProcessWorker;

private Runner(BatchProcessor<Collection<NodeRef>> processor, ScriptedBatchProcessWorker scriptedBatchProcessWorker) {
this.processor = processor;
this.scriptedBatchProcessWorker = scriptedBatchProcessWorker;
}

@Override
public void run() {
processor.process(scriptedBatchProcessWorker, true);
}
}

/**
* converts the native array of scriptnodes to a list of noderefs
Expand All @@ -169,14 +292,38 @@ public void runForNodes(String batchName, int workerThreads, final int batchSize
* @return list of noderefs
*/
private List<NodeRef> convertScriptNodesArray(NativeArray scriptNodes) {
List<NodeRef> nodes = new ArrayList<NodeRef>();
List<NodeRef> nodes = new ArrayList<>();
for (Object id : scriptNodes.getIds()) {
int index = (Integer) id;
ScriptNode scriptNode = (ScriptNode) scriptNodes.get(index, null);
Object obj = scriptNodes.get(index);
if (obj instanceof NativeJavaObject) {
obj = ((NativeJavaObject) obj).unwrap();
}
ScriptNode scriptNode = (ScriptNode) obj;
nodes.add(scriptNode.getNodeRef());
}
return nodes;
}

/**
* converts the native array of NodeRef strings to a list of noderefs
*
* @param nodeRefsAsStrings
* @return list of noderefs
*/
private List<NodeRef> convertNodeRefsAsStringsArray(NativeArray nodeRefsAsStrings) {
List<NodeRef> nodes = new ArrayList<>();
for (Object id : nodeRefsAsStrings.getIds()) {
int index = (Integer) id;
Object obj = nodeRefsAsStrings.get(index);
if (obj instanceof NativeJavaObject) {
obj = ((NativeJavaObject) obj).unwrap();
}
String nodeRefAsString = (String) obj;
nodes.add(new NodeRef(nodeRefAsString));
}
return nodes;
}

@Override
public void setScope(Scriptable scope) {
Expand Down

0 comments on commit 9a665e3

Please sign in to comment.