Search in sources :

Example 1 with RemoteServiceMethod

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

the class RedissonRemoteService method register.

@Override
public <T> void register(Class<T> remoteInterface, T object, int workers, ExecutorService executor) {
    if (workers < 1) {
        throw new IllegalArgumentException("executorsAmount can't be lower than 1");
    }
    for (Method method : remoteInterface.getMethods()) {
        RemoteServiceMethod value = new RemoteServiceMethod(method, object);
        RemoteServiceKey key = new RemoteServiceKey(remoteInterface, method.getName(), getMethodSignatures(method));
        if (beans.put(key, value) != null) {
            return;
        }
    }
    for (int i = 0; i < workers; i++) {
        String requestQueueName = getRequestQueueName(remoteInterface);
        RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName, getCodec());
        subscribe(remoteInterface, requestQueue, executor);
    }
}
Also used : RemoteServiceRequest(org.redisson.remote.RemoteServiceRequest) RemoteServiceKey(org.redisson.remote.RemoteServiceKey) RemoteServiceMethod(org.redisson.remote.RemoteServiceMethod) Method(java.lang.reflect.Method) RemoteServiceMethod(org.redisson.remote.RemoteServiceMethod)

Example 2 with RemoteServiceMethod

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

Aggregations

RemoteServiceKey (org.redisson.remote.RemoteServiceKey)2 RemoteServiceMethod (org.redisson.remote.RemoteServiceMethod)2 Method (java.lang.reflect.Method)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RRemoteServiceResponse (org.redisson.remote.RRemoteServiceResponse)1 RemoteServiceCancelRequest (org.redisson.remote.RemoteServiceCancelRequest)1 RemoteServiceCancelResponse (org.redisson.remote.RemoteServiceCancelResponse)1 RemoteServiceRequest (org.redisson.remote.RemoteServiceRequest)1