Search in sources :

Example 11 with CommandDispatcherException

use of org.wildfly.clustering.dispatcher.CommandDispatcherException in project wildfly by wildfly.

the class ChannelCommandDispatcher method executeOnGroup.

@Override
public <R> Map<Node, CompletionStage<R>> executeOnGroup(Command<R, ? super CC> command, Node... excludedMembers) throws CommandDispatcherException {
    Set<Node> excluded = (excludedMembers != null) ? new HashSet<>(Arrays.asList(excludedMembers)) : Collections.emptySet();
    Map<Node, CompletionStage<R>> results = new ConcurrentHashMap<>();
    Buffer buffer = this.createBuffer(command);
    for (Node member : this.group.getMembership().getMembers()) {
        if (!excluded.contains(member)) {
            Address address = this.group.getAddress(member);
            if (this.localAddress.equals(address)) {
                results.put(member, this.localDispatcher.executeOnMember(command, member));
            } else {
                try {
                    ServiceRequest<R, MC> request = new ServiceRequest<>(this.dispatcher.getCorrelator(), this.group.getAddress(member), this.options, this.context);
                    CompletionStage<R> future = request.send(buffer);
                    results.put(member, future);
                    future.whenComplete(new PruneCancellationTask<>(results, member));
                } catch (CommandDispatcherException e) {
                    // Cancel previously dispatched messages
                    for (CompletionStage<R> result : results.values()) {
                        result.toCompletableFuture().cancel(true);
                    }
                    throw e;
                }
            }
        }
    }
    return results;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(org.jgroups.util.Buffer) Address(org.jgroups.Address) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Node(org.wildfly.clustering.group.Node) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionStage(java.util.concurrent.CompletionStage)

Example 12 with CommandDispatcherException

use of org.wildfly.clustering.dispatcher.CommandDispatcherException 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

CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)12 CancellationException (java.util.concurrent.CancellationException)9 Node (org.wildfly.clustering.group.Node)9 CompletionStage (java.util.concurrent.CompletionStage)6 Map (java.util.Map)5 CompletionException (java.util.concurrent.CompletionException)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 Message (org.jgroups.Message)4 RequestOptions (org.jgroups.blocks.RequestOptions)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Address (org.jgroups.Address)3 ArrayList (java.util.ArrayList)2 WorkException (javax.resource.spi.work.WorkException)2 Rsp (org.jgroups.util.Rsp)2 CommandResponse (org.wildfly.clustering.dispatcher.CommandResponse)2 Group (org.wildfly.clustering.group.Group)2 ExceptionSupplier (org.wildfly.common.function.ExceptionSupplier)2