Search in sources :

Example 1 with ExceptionSupplier

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);
}
Also used : ExceptionSupplier(org.wildfly.common.function.ExceptionSupplier) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Node(org.wildfly.clustering.group.Node) CompletionStage(java.util.concurrent.CompletionStage)

Example 2 with ExceptionSupplier

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());
}
Also used : ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) ExceptionSupplier(org.wildfly.common.function.ExceptionSupplier) AtomicReference(java.util.concurrent.atomic.AtomicReference) Callable(java.util.concurrent.Callable) ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) Supplier(java.util.function.Supplier) ExceptionSupplier(org.wildfly.common.function.ExceptionSupplier) Test(org.junit.Test)

Example 3 with ExceptionSupplier

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);
        }
    };
}
Also used : ExceptionSupplier(org.wildfly.common.function.ExceptionSupplier) Contextualizer(org.jboss.as.clustering.context.Contextualizer) MarshalledValue(org.wildfly.clustering.marshalling.spi.MarshalledValue) ByteBuffer(java.nio.ByteBuffer) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) Command(org.wildfly.clustering.dispatcher.Command) StampedLockServiceExecutor(org.wildfly.clustering.service.concurrent.StampedLockServiceExecutor) ServiceExecutor(org.wildfly.clustering.service.concurrent.ServiceExecutor) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 4 with ExceptionSupplier

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));
        }
    }
}
Also used : ExceptionSupplier(org.wildfly.common.function.ExceptionSupplier) BridgeConfiguration(org.apache.activemq.artemis.core.config.BridgeConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ModelNode(org.jboss.dmr.ModelNode)

Example 5 with ExceptionSupplier

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;
}
Also used : Serializable(java.io.Serializable) ExceptionSupplier(org.wildfly.common.function.ExceptionSupplier) Optional(java.util.Optional) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) WorkException(javax.resource.spi.work.WorkException) CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException)

Aggregations

ExceptionSupplier (org.wildfly.common.function.ExceptionSupplier)5 CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Callable (java.util.concurrent.Callable)1 CancellationException (java.util.concurrent.CancellationException)1 CompletionException (java.util.concurrent.CompletionException)1 CompletionStage (java.util.concurrent.CompletionStage)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Supplier (java.util.function.Supplier)1 WorkException (javax.resource.spi.work.WorkException)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 BridgeConfiguration (org.apache.activemq.artemis.core.config.BridgeConfiguration)1 Contextualizer (org.jboss.as.clustering.context.Contextualizer)1