use of org.wildfly.common.function.ExceptionSupplier in project wildfly by wildfly.
the class PrimaryOwnerScheduler method executeOnPrimaryOwner.
private CompletionStage<Void> executeOnPrimaryOwner(I id, Command<Void, Scheduler<I, M>> command) throws CommandDispatcherException {
K key = this.keyFactory.apply(id);
Function<K, Node> primaryOwnerLocator = this.primaryOwnerLocator;
CommandDispatcher<Scheduler<I, M>> dispatcher = this.dispatcher;
ExceptionSupplier<CompletionStage<Void>, CommandDispatcherException> action = new ExceptionSupplier<CompletionStage<Void>, CommandDispatcherException>() {
@Override
public CompletionStage<Void> get() throws CommandDispatcherException {
Node node = primaryOwnerLocator.apply(key);
// This should only go remote following a failover
return dispatcher.executeOnMember(command, node);
}
};
return INVOKER.invoke(action);
}
use of org.wildfly.common.function.ExceptionSupplier in project wildfly by wildfly.
the class ContextReferenceExecutorTestCase method test.
@Test
public void test() throws Exception {
Object original = new Object();
Object target = new Object();
Object result = new Object();
AtomicReference<Object> resultRef = new AtomicReference<>();
ContextReference<Object> contextRef = new AtomicContextReference<>(original);
Contextualizer contextualizer = new ContextReferenceExecutor<>(target, contextRef);
Runnable runner = new Runnable() {
@Override
public void run() {
assertSame(target, contextRef.get());
resultRef.set(result);
}
};
assertSame(original, contextRef.get());
contextualizer.contextualize(runner).run();
assertSame(original, contextRef.get());
assertSame(result, resultRef.get());
resultRef.set(null);
ExceptionRunnable<Exception> exceptionRunner = new ExceptionRunnable<Exception>() {
@Override
public void run() throws Exception {
assertSame(target, contextRef.get());
resultRef.set(result);
}
};
assertSame(original, contextRef.get());
contextualizer.contextualize(exceptionRunner).run();
assertSame(original, contextRef.get());
assertSame(result, resultRef.get());
resultRef.set(null);
Callable<Object> caller = new Callable<Object>() {
@Override
public Object call() {
assertSame(target, contextRef.get());
return result;
}
};
assertSame(original, contextRef.get());
assertSame(result, contextualizer.contextualize(caller).call());
assertSame(original, contextRef.get());
Supplier<Object> supplier = new Supplier<Object>() {
@Override
public Object get() {
assertSame(target, contextRef.get());
return result;
}
};
assertSame(original, contextRef.get());
assertSame(result, contextualizer.contextualize(supplier).get());
assertSame(original, contextRef.get());
ExceptionSupplier<Object, Exception> exceptionSupplier = new ExceptionSupplier<Object, Exception>() {
@Override
public Object get() {
assertSame(target, contextRef.get());
return result;
}
};
assertSame(original, contextRef.get());
assertSame(result, contextualizer.contextualize(exceptionSupplier).get());
assertSame(original, contextRef.get());
}
use of org.wildfly.common.function.ExceptionSupplier in project wildfly by wildfly.
the class ChannelCommandDispatcherFactory method read.
private ExceptionSupplier<Object, Exception> read(Message message) throws IOException {
ByteBuffer buffer = ByteBuffer.wrap(message.getRawBuffer(), message.getOffset(), message.getLength());
@SuppressWarnings("unchecked") Map.Entry<Object, MarshalledValue<Command<Object, Object>, Object>> entry = (Map.Entry<Object, MarshalledValue<Command<Object, Object>, Object>>) this.marshaller.read(buffer);
Object clientId = entry.getKey();
CommandDispatcherContext<?, ?> context = this.contexts.get(clientId);
if (context == null)
return NO_SUCH_SERVICE_SUPPLIER;
Object commandContext = context.getCommandContext();
Contextualizer contextualizer = context.getContextualizer();
MarshalledValue<Command<Object, Object>, Object> value = entry.getValue();
Command<Object, Object> command = value.get(context.getMarshalledValueFactory().getMarshallingContext());
ExceptionSupplier<Object, Exception> commandExecutionTask = new ExceptionSupplier<Object, Exception>() {
@Override
public Object get() throws Exception {
return context.getMarshalledValueFactory().createMarshalledValue(command.execute(commandContext));
}
};
ServiceExecutor executor = this.executor;
return new ExceptionSupplier<Object, Exception>() {
@Override
public Object get() throws Exception {
return executor.execute(contextualizer.contextualize(commandExecutionTask)).orElse(NO_SUCH_SERVICE);
}
};
}
use of org.wildfly.common.function.ExceptionSupplier in project wildfly by wildfly.
the class ServerAdd method addBridgeCredentialStoreReference.
private static void addBridgeCredentialStoreReference(ActiveMQServerService amqService, Configuration configuration, ObjectTypeAttributeDefinition credentialReferenceAttributeDefinition, OperationContext context, ModelNode model, ServiceBuilder<?> serviceBuilder) throws OperationFailedException {
for (BridgeConfiguration bridgeConfiguration : configuration.getBridgeConfigurations()) {
String name = bridgeConfiguration.getName();
InjectedValue<ExceptionSupplier<CredentialSource, Exception>> injector = amqService.getBridgeCredentialSourceSupplierInjector(name);
String[] modelFilter = { CommonAttributes.BRIDGE, name };
ModelNode filteredModelNode = model;
if (modelFilter != null && modelFilter.length > 0) {
for (String path : modelFilter) {
if (filteredModelNode.get(path).isDefined())
filteredModelNode = filteredModelNode.get(path);
else
break;
}
}
ModelNode value = credentialReferenceAttributeDefinition.resolveModelAttribute(context, filteredModelNode);
if (value.isDefined()) {
injector.inject(CredentialReference.getCredentialSourceSupplier(context, credentialReferenceAttributeDefinition, filteredModelNode, serviceBuilder));
}
}
}
use of org.wildfly.common.function.ExceptionSupplier in project wildfly by wildfly.
the class CommandDispatcherTransport method sendMessage.
@Override
protected Serializable sendMessage(Node physicalAddress, Request request, Serializable... parameters) throws WorkException {
Command<?, CommandDispatcherTransport> command = createCommand(request, parameters);
CommandDispatcher<CommandDispatcherTransport> dispatcher = this.dispatcher;
ExceptionSupplier<Optional<Serializable>, WorkException> task = new ExceptionSupplier<Optional<Serializable>, WorkException>() {
@Override
public Optional<Serializable> get() throws WorkException {
try {
CompletionStage<?> response = dispatcher.executeOnMember(command, physicalAddress);
return Optional.ofNullable((Serializable) response.toCompletableFuture().join());
} catch (CancellationException e) {
return Optional.empty();
} catch (CommandDispatcherException | CompletionException e) {
throw new WorkException(e);
}
}
};
Optional<Serializable> val = this.executor.execute(task).orElse(null);
return val != null ? val.orElse(null) : null;
}
Aggregations