Search in sources :

Example 1 with RemoteServiceCancelResponse

use of org.redisson.remote.RemoteServiceCancelResponse in project redisson by redisson.

the class ScheduledExecutorRemoteService method cancelExecution.

public boolean cancelExecution(String requestId) {
    Class<?> syncInterface = RemoteExecutorService.class;
    String requestQueueName = getRequestQueueName(syncInterface);
    String cancelRequestName = getCancelRequestQueueName(syncInterface, requestId);
    if (!redisson.getMap(schedulerTasksName, LongCodec.INSTANCE).containsKey(requestId)) {
        return false;
    }
    RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName, getCodec());
    RemoteServiceRequest request = new RemoteServiceRequest(requestId);
    if (remove(requestQueue, request)) {
        return true;
    }
    RBlockingQueue<RemoteServiceCancelRequest> cancelRequestQueue = redisson.getBlockingQueue(cancelRequestName, getCodec());
    cancelRequestQueue.putAsync(new RemoteServiceCancelRequest(true, requestId + ":cancel-response"));
    cancelRequestQueue.expireAsync(60, TimeUnit.SECONDS);
    String responseQueueName = getResponseQueueName(syncInterface, requestId + ":cancel-response");
    RBlockingQueue<RemoteServiceCancelResponse> responseQueue = redisson.getBlockingQueue(responseQueueName, getCodec());
    try {
        RemoteServiceCancelResponse response = responseQueue.poll(60, TimeUnit.SECONDS);
        if (response == null) {
            return false;
        }
        return response.isCanceled();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return false;
    }
}
Also used : RemoteServiceCancelRequest(org.redisson.remote.RemoteServiceCancelRequest) RemoteServiceRequest(org.redisson.remote.RemoteServiceRequest) RemoteServiceCancelResponse(org.redisson.remote.RemoteServiceCancelResponse)

Example 2 with RemoteServiceCancelResponse

use of org.redisson.remote.RemoteServiceCancelResponse in project redisson by redisson.

the class BaseRemoteService method awaitResultAsync.

protected void awaitResultAsync(final RemoteInvocationOptions optionsCopy, final RemotePromise<Object> result, final RemoteServiceRequest request, final String responseName) {
    // poll for the response only if expected
    if (!optionsCopy.isResultExpected()) {
        return;
    }
    RBlockingQueue<RRemoteServiceResponse> responseQueue = redisson.getBlockingQueue(responseName, getCodec());
    RFuture<RRemoteServiceResponse> responseFuture = responseQueue.pollAsync(optionsCopy.getExecutionTimeoutInMillis(), TimeUnit.MILLISECONDS);
    responseFuture.addListener(new FutureListener<RRemoteServiceResponse>() {

        @Override
        public void operationComplete(Future<RRemoteServiceResponse> future) throws Exception {
            if (!future.isSuccess()) {
                result.tryFailure(future.cause());
                return;
            }
            if (future.getNow() == null) {
                RemoteServiceTimeoutException e = new RemoteServiceTimeoutException("No response after " + optionsCopy.getExecutionTimeoutInMillis() + "ms for request: " + request);
                result.tryFailure(e);
                return;
            }
            if (future.getNow() instanceof RemoteServiceCancelResponse) {
                result.doCancel();
                return;
            }
            RemoteServiceResponse response = (RemoteServiceResponse) future.getNow();
            if (response.getError() != null) {
                result.tryFailure(response.getError());
                return;
            }
            result.trySuccess(response.getResult());
        }
    });
}
Also used : RemoteServiceTimeoutException(org.redisson.remote.RemoteServiceTimeoutException) RRemoteServiceResponse(org.redisson.remote.RRemoteServiceResponse) RemoteServiceTimeoutException(org.redisson.remote.RemoteServiceTimeoutException) IOException(java.io.IOException) RemoteServiceAckTimeoutException(org.redisson.remote.RemoteServiceAckTimeoutException) RemoteServiceResponse(org.redisson.remote.RemoteServiceResponse) RRemoteServiceResponse(org.redisson.remote.RRemoteServiceResponse) RemoteServiceCancelResponse(org.redisson.remote.RemoteServiceCancelResponse)

Example 3 with RemoteServiceCancelResponse

use of org.redisson.remote.RemoteServiceCancelResponse in project redisson by redisson.

the class RedissonRemoteService method executeMethod.

private <T> void executeMethod(final Class<T> remoteInterface, final RBlockingQueue<RemoteServiceRequest> requestQueue, final ExecutorService executor, final RemoteServiceRequest request) {
    final RemoteServiceMethod method = beans.get(new RemoteServiceKey(remoteInterface, request.getMethodName(), request.getSignatures()));
    final String responseName = getResponseQueueName(remoteInterface, request.getRequestId());
    RBlockingQueue<RemoteServiceCancelRequest> cancelRequestQueue = redisson.getBlockingQueue(getCancelRequestQueueName(remoteInterface, request.getRequestId()), getCodec());
    final RFuture<RemoteServiceCancelRequest> cancelRequestFuture = cancelRequestQueue.takeAsync();
    final AtomicReference<RRemoteServiceResponse> responseHolder = new AtomicReference<RRemoteServiceResponse>();
    final java.util.concurrent.Future<?> submitFuture = executor.submit(new Runnable() {

        @Override
        public void run() {
            invokeMethod(remoteInterface, requestQueue, request, method, responseName, executor, cancelRequestFuture, responseHolder);
        }
    });
    cancelRequestFuture.addListener(new FutureListener<RemoteServiceCancelRequest>() {

        @Override
        public void operationComplete(Future<RemoteServiceCancelRequest> future) throws Exception {
            if (!future.isSuccess()) {
                return;
            }
            boolean res = submitFuture.cancel(future.getNow().isMayInterruptIfRunning());
            if (res) {
                RemoteServiceCancelResponse response = new RemoteServiceCancelResponse();
                if (!responseHolder.compareAndSet(null, response)) {
                    response = new RemoteServiceCancelResponse(false);
                }
                // could be removed not from future object
                if (future.getNow().getResponseId() != null) {
                    String cancelResponseName = getResponseQueueName(remoteInterface, future.getNow().getResponseId());
                    send(60 * 1000, cancelResponseName, response);
                }
            }
        }
    });
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) RemoteServiceCancelResponse(org.redisson.remote.RemoteServiceCancelResponse) RemoteServiceCancelRequest(org.redisson.remote.RemoteServiceCancelRequest) RemoteServiceKey(org.redisson.remote.RemoteServiceKey) RemoteServiceMethod(org.redisson.remote.RemoteServiceMethod) RRemoteServiceResponse(org.redisson.remote.RRemoteServiceResponse)

Example 4 with RemoteServiceCancelResponse

use of org.redisson.remote.RemoteServiceCancelResponse in project redisson by redisson.

the class RedissonRemoteService method invokeMethod.

private <T> void invokeMethod(final Class<T> remoteInterface, final RBlockingQueue<RemoteServiceRequest> requestQueue, final RemoteServiceRequest request, RemoteServiceMethod method, String responseName, final ExecutorService executor, RFuture<RemoteServiceCancelRequest> cancelRequestFuture, final AtomicReference<RRemoteServiceResponse> responseHolder) {
    try {
        if (method.getBean() instanceof RemoteParams) {
            ((RemoteParams) method.getBean()).setRequestId(request.getRequestId());
        }
        Object result = method.getMethod().invoke(method.getBean(), request.getArgs());
        RemoteServiceResponse response = new RemoteServiceResponse(result);
        responseHolder.compareAndSet(null, response);
    } catch (Exception e) {
        RemoteServiceResponse response = new RemoteServiceResponse(e.getCause());
        responseHolder.compareAndSet(null, response);
        log.error("Can't execute: " + request, e);
    }
    if (cancelRequestFuture != null) {
        cancelRequestFuture.cancel(false);
    }
    // send the response only if expected or task was canceled
    if (request.getOptions().isResultExpected() || responseHolder.get() instanceof RemoteServiceCancelResponse) {
        long timeout = 60 * 1000;
        if (request.getOptions().getExecutionTimeoutInMillis() != null) {
            timeout = request.getOptions().getExecutionTimeoutInMillis();
        }
        RFuture<List<?>> clientsFuture = send(timeout, responseName, responseHolder.get());
        clientsFuture.addListener(new FutureListener<List<?>>() {

            @Override
            public void operationComplete(Future<List<?>> future) throws Exception {
                if (!future.isSuccess()) {
                    log.error("Can't send response: " + responseHolder.get() + " for request: " + request, future.cause());
                    if (future.cause() instanceof RedissonShutdownException) {
                        return;
                    }
                }
                // re-subscribe anyways (fail or success) after the send
                // (response)
                subscribe(remoteInterface, requestQueue, executor);
            }
        });
    } else {
        // re-subscribe anyways after the method invocation
        subscribe(remoteInterface, requestQueue, executor);
    }
}
Also used : List(java.util.List) RemoteParams(org.redisson.remote.RemoteParams) RemoteServiceResponse(org.redisson.remote.RemoteServiceResponse) RRemoteServiceResponse(org.redisson.remote.RRemoteServiceResponse) RemoteServiceCancelResponse(org.redisson.remote.RemoteServiceCancelResponse)

Aggregations

RemoteServiceCancelResponse (org.redisson.remote.RemoteServiceCancelResponse)4 RRemoteServiceResponse (org.redisson.remote.RRemoteServiceResponse)3 RemoteServiceCancelRequest (org.redisson.remote.RemoteServiceCancelRequest)2 RemoteServiceResponse (org.redisson.remote.RemoteServiceResponse)2 IOException (java.io.IOException)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RemoteParams (org.redisson.remote.RemoteParams)1 RemoteServiceAckTimeoutException (org.redisson.remote.RemoteServiceAckTimeoutException)1 RemoteServiceKey (org.redisson.remote.RemoteServiceKey)1 RemoteServiceMethod (org.redisson.remote.RemoteServiceMethod)1 RemoteServiceRequest (org.redisson.remote.RemoteServiceRequest)1 RemoteServiceTimeoutException (org.redisson.remote.RemoteServiceTimeoutException)1