Search in sources :

Example 1 with RemoteServiceCancelRequest

use of org.redisson.remote.RemoteServiceCancelRequest 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 RemoteServiceCancelRequest

use of org.redisson.remote.RemoteServiceCancelRequest 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 3 with RemoteServiceCancelRequest

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

the class BaseRemoteService method cancelExecution.

private void cancelExecution(RemoteInvocationOptions optionsCopy, String responseName, RemoteServiceRequest request, boolean mayInterruptIfRunning, String canceRequestName, RemotePromise<Object> remotePromise) {
    RBlockingQueue<RemoteServiceCancelRequest> cancelRequestQueue = redisson.getBlockingQueue(canceRequestName, getCodec());
    cancelRequestQueue.putAsync(new RemoteServiceCancelRequest(mayInterruptIfRunning));
    cancelRequestQueue.expireAsync(60, TimeUnit.SECONDS);
    // subscribe for async result if it's not expected before
    if (!optionsCopy.isResultExpected()) {
        RemoteInvocationOptions options = new RemoteInvocationOptions(optionsCopy);
        options.expectResultWithin(60, TimeUnit.SECONDS);
        awaitResultAsync(options, remotePromise, request, responseName);
    }
}
Also used : RemoteServiceCancelRequest(org.redisson.remote.RemoteServiceCancelRequest) RemoteInvocationOptions(org.redisson.api.RemoteInvocationOptions)

Aggregations

RemoteServiceCancelRequest (org.redisson.remote.RemoteServiceCancelRequest)3 RemoteServiceCancelResponse (org.redisson.remote.RemoteServiceCancelResponse)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RemoteInvocationOptions (org.redisson.api.RemoteInvocationOptions)1 RRemoteServiceResponse (org.redisson.remote.RRemoteServiceResponse)1 RemoteServiceKey (org.redisson.remote.RemoteServiceKey)1 RemoteServiceMethod (org.redisson.remote.RemoteServiceMethod)1 RemoteServiceRequest (org.redisson.remote.RemoteServiceRequest)1