use of java.util.function.Supplier in project wildfly by wildfly.
the class KeyAffinityServiceFactoryBuilder method build.
@Override
public ServiceBuilder<KeyAffinityServiceFactory> build(ServiceTarget target) {
int bufferSize = this.bufferSize;
Function<ExecutorService, KeyAffinityServiceFactory> mapper = executor -> new KeyAffinityServiceFactory() {
@Override
public <K> KeyAffinityService<K> createService(Cache<K, ?> cache, KeyGenerator<K> generator) {
CacheMode mode = cache.getCacheConfiguration().clustering().cacheMode();
return mode.isDistributed() || mode.isReplicated() ? new KeyAffinityServiceImpl<>(executor, cache, generator, bufferSize, Collections.singleton(cache.getCacheManager().getAddress()), false) : new SimpleKeyAffinityService<>(generator);
}
};
Supplier<ExecutorService> supplier = () -> {
ThreadGroup threadGroup = new ThreadGroup("KeyAffinityService ThreadGroup");
String namePattern = "KeyAffinityService Thread Pool -- %t";
PrivilegedAction<ThreadFactory> action = () -> new JBossThreadFactory(threadGroup, Boolean.FALSE, null, namePattern, null, null);
return Executors.newCachedThreadPool(doPrivileged(action));
};
Service<KeyAffinityServiceFactory> service = new SuppliedValueService<>(mapper, supplier, ExecutorService::shutdown);
return new AsynchronousServiceBuilder<>(this.getServiceName(), service).startSynchronously().build(target).setInitialMode(ServiceController.Mode.ON_DEMAND);
}
use of java.util.function.Supplier in project samza by apache.
the class TestOperatorSpecs method testCreateWindowOperatorWithRelaxedTypes.
@Test
public void testCreateWindowOperatorWithRelaxedTypes() throws Exception {
Function<TestMessageEnvelope, String> keyExtractor = m -> m.getKey();
FoldLeftFunction<TestMessageEnvelope, Integer> aggregator = (m, c) -> c + 1;
Supplier<Integer> initialValue = () -> 0;
//instantiate a window using reflection
WindowInternal<TestInputMessageEnvelope, String, Integer> window = new WindowInternal(null, initialValue, aggregator, keyExtractor, null, WindowType.TUMBLING);
MessageStreamImpl<WindowPane<String, Integer>> mockWndOut = mock(MessageStreamImpl.class);
WindowOperatorSpec spec = OperatorSpecs.createWindowOperatorSpec(window, mockWndOut, 1);
assertEquals(spec.getWindow(), window);
assertEquals(spec.getWindow().getKeyExtractor(), keyExtractor);
assertEquals(spec.getWindow().getFoldLeftFunction(), aggregator);
// make sure that the functions with relaxed types work as expected
TestInputMessageEnvelope inputMsg = new TestInputMessageEnvelope("test-input-key1", "test-value-1", 23456L, "input-id-1");
assertEquals("test-input-key1", spec.getWindow().getKeyExtractor().apply(inputMsg));
assertEquals(1, spec.getWindow().getFoldLeftFunction().apply(inputMsg, 0));
}
use of java.util.function.Supplier in project samza by apache.
the class TestOperatorSpecs method testCreateWindowOperator.
@Test
public void testCreateWindowOperator() throws Exception {
Function<TestMessageEnvelope, String> keyExtractor = m -> "globalkey";
FoldLeftFunction<TestMessageEnvelope, Integer> aggregator = (m, c) -> c + 1;
Supplier<Integer> initialValue = () -> 0;
//instantiate a window using reflection
WindowInternal window = new WindowInternal(null, initialValue, aggregator, keyExtractor, null, WindowType.TUMBLING);
MessageStreamImpl<WindowPane<String, Integer>> mockWndOut = mock(MessageStreamImpl.class);
WindowOperatorSpec spec = OperatorSpecs.<TestMessageEnvelope, String, Integer>createWindowOperatorSpec(window, mockWndOut, 1);
assertEquals(spec.getWindow(), window);
assertEquals(spec.getWindow().getKeyExtractor(), keyExtractor);
assertEquals(spec.getWindow().getFoldLeftFunction(), aggregator);
}
use of java.util.function.Supplier in project aries by apache.
the class AbstractPushStreamImpl method window.
@Override
public <R> PushStream<R> window(Supplier<Duration> time, IntSupplier maxEvents, Executor ex, BiFunction<Long, Collection<T>, R> f) {
AtomicLong timestamp = new AtomicLong();
AtomicLong counter = new AtomicLong();
Object lock = new Object();
AtomicReference<Queue<T>> queueRef = new AtomicReference<Queue<T>>(null);
// This code is declared as a separate block to avoid any confusion
// about which instance's methods and variables are in scope
Consumer<AbstractPushStreamImpl<R>> begin = p -> {
synchronized (lock) {
timestamp.lazySet(System.nanoTime());
long count = counter.get();
scheduler.schedule(getWindowTask(p, f, time, maxEvents, lock, count, queueRef, timestamp, counter, ex), time.get().toNanos(), NANOSECONDS);
}
queueRef.set(getQueueForInternalBuffering(maxEvents.getAsInt()));
};
@SuppressWarnings("resource") AbstractPushStreamImpl<R> eventStream = new IntermediatePushStreamImpl<R>(psp, ex, scheduler, this) {
@Override
protected void beginning() {
begin.accept(this);
}
};
AtomicBoolean endPending = new AtomicBoolean(false);
updateNext((event) -> {
try {
if (eventStream.closed.get() == CLOSED) {
return ABORT;
}
Queue<T> queue;
if (!event.isTerminal()) {
long elapsed;
long newCount;
synchronized (lock) {
for (; ; ) {
queue = queueRef.get();
if (queue == null) {
if (endPending.get()) {
return ABORT;
} else {
continue;
}
} else if (queue.offer(event.getData())) {
return CONTINUE;
} else {
queueRef.lazySet(null);
break;
}
}
long now = System.nanoTime();
elapsed = now - timestamp.get();
timestamp.lazySet(now);
newCount = counter.get() + 1;
counter.lazySet(newCount);
// This is a non-blocking call, and must happen in the
// synchronized block to avoid re=ordering the executor
// enqueue with a subsequent incoming close operation
aggregateAndForward(f, eventStream, event, queue, ex, elapsed);
}
// These must happen outside the synchronized block as we
// call out to user code
queueRef.set(getQueueForInternalBuffering(maxEvents.getAsInt()));
scheduler.schedule(getWindowTask(eventStream, f, time, maxEvents, lock, newCount, queueRef, timestamp, counter, ex), time.get().toNanos(), NANOSECONDS);
return CONTINUE;
} else {
long elapsed;
synchronized (lock) {
queue = queueRef.get();
queueRef.lazySet(null);
endPending.set(true);
long now = System.nanoTime();
elapsed = now - timestamp.get();
counter.lazySet(counter.get() + 1);
}
Collection<T> collected = queue == null ? emptyList() : queue;
ex.execute(() -> {
try {
eventStream.handleEvent(PushEvent.data(f.apply(Long.valueOf(NANOSECONDS.toMillis(elapsed)), collected)));
} catch (Exception e) {
close(PushEvent.error(e));
}
});
}
ex.execute(() -> eventStream.handleEvent(event.nodata()));
return ABORT;
} catch (Exception e) {
close(PushEvent.error(e));
return ABORT;
}
});
return eventStream;
}
use of java.util.function.Supplier in project indy by Commonjava.
the class DeprecatedContentAccessResource method doCreate.
@ApiOperation("Store file/artifact content under the given artifact store (type/name) and path.")
@ApiResponses({ @ApiResponse(code = 201, message = "Content was stored successfully"), @ApiResponse(code = 400, message = "No appropriate storage location was found in the specified store (this store, or a member if a group is specified).") })
@PUT
@Path("/{path: (.+)?}")
public Response doCreate(@ApiParam(allowableValues = "hosted,group,remote", required = true) @PathParam("type") final String type, @ApiParam(required = true) @PathParam("name") final String name, @PathParam("path") final String path, @Context final UriInfo uriInfo, @Context final HttpServletRequest request) {
String packageType = MavenPackageTypeDescriptor.MAVEN_PKG_KEY;
final Supplier<URI> uriSupplier = () -> uriInfo.getBaseUriBuilder().path(getClass()).path(path).build(packageType, type, name);
final Consumer<Response.ResponseBuilder> deprecated = builder -> {
String alt = Paths.get("/api/maven", type, name, path).toString();
markDeprecated(builder, alt);
};
return handler.doCreate(packageType, type, name, path, request, new EventMetadata(), uriSupplier, deprecated);
}
Aggregations