use of groovy.lang.Closure in project groovy-core by groovy.
the class JmxEventListener method handleNotification.
/**
* This is the implemented method for NotificationListener. It is called by an event emitter to dispatch
* JMX events to listeners. Here it handles internal JmxBuilder events.
*
* @param notification the notification object passed to closure used to handle JmxBuilder events.
* @param handback - In this case, the handback is the closure to execute when the event is handled.
*/
public void handleNotification(Notification notification, Object handback) {
Map event = (Map) handback;
if (event != null) {
Object del = event.get("managedObject");
Object callback = event.get("callback");
if (callback != null && callback instanceof Closure) {
Closure closure = (Closure) callback;
closure.setDelegate(del);
if (closure.getMaximumNumberOfParameters() == 1)
closure.call(buildOperationNotificationPacket(notification));
else
closure.call();
}
}
}
use of groovy.lang.Closure in project groovy-core by groovy.
the class StreamingJsonDelegate method cloneDelegateAndGetContent.
public static void cloneDelegateAndGetContent(Writer w, Closure c, boolean first) {
StreamingJsonDelegate delegate = new StreamingJsonDelegate(w, first);
Closure cloned = (Closure) c.clone();
cloned.setDelegate(delegate);
cloned.setResolveStrategy(Closure.DELEGATE_FIRST);
cloned.call();
}
use of groovy.lang.Closure in project indy by Commonjava.
the class PromotionValidationTools method runParallelInBatchAndWait.
private <T> void runParallelInBatchAndWait(Collection<Collection<T>> batches, Closure closure, Logger logger) {
final CountDownLatch latch = new CountDownLatch(batches.size());
batches.forEach(batch -> ruleParallelExecutor.execute(() -> {
try {
logger.trace("The paralleled exe on batch {}", batch);
batch.forEach(e -> {
String depthStr = MDC.get(ITERATION_DEPTH);
RequestContextHelper.setContext(ITERATION_DEPTH, depthStr == null ? "0" : String.valueOf(Integer.parseInt(depthStr) + 1));
RequestContextHelper.setContext(ITERATION_ITEM, String.valueOf(e));
try {
closure.call(e);
} finally {
MDC.remove(ITERATION_ITEM);
MDC.remove(ITERATION_DEPTH);
}
});
} finally {
latch.countDown();
}
}));
waitForCompletion(latch);
}
use of groovy.lang.Closure in project indy by Commonjava.
the class PromotionValidationToolsTest method testParalleledInBatch.
@Test
public void testParalleledInBatch() {
PromoteConfig config = new PromoteConfig();
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
PromotionValidationTools tools = new PromotionValidationTools(null, null, null, null, null, null, null, null, executor, config);
List<String> errors = Collections.synchronizedList(new ArrayList<>());
Closure closure = new Closure<String>(null) {
@Override
public String call(Object arg) {
if (((String) arg).startsWith("err_")) {
errors.add((String) arg);
}
return null;
}
};
tools.paralleledInBatch(array, closure);
verifyIt(errors);
}
use of groovy.lang.Closure in project indy by Commonjava.
the class PromotionValidationToolsTest method testParalleledInBatch_smallSize.
@Test
public void testParalleledInBatch_smallSize() {
PromoteConfig config = new PromoteConfig();
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
PromotionValidationTools tools = new PromotionValidationTools(null, null, null, null, null, null, null, null, executor, config);
List<String> errors = Collections.synchronizedList(new ArrayList<>());
Closure closure = new Closure<String>(null) {
@Override
public String call(Object arg) {
if (((String) arg).startsWith("err_")) {
errors.add((String) arg);
}
return null;
}
};
tools.paralleledInBatch(array, closure);
verifyIt(errors);
}
Aggregations