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;
}
}
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);
}
}
}
});
}
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);
}
}
Aggregations