Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Reuse CompletableFuture?

Discussion in 'Java Update' started by pinokio, 4/1/16.

  1. pinokio

    pinokio New Member

    Hi all,
    I am currently working in Java client library that sends a lot of asynchronous requests to remote server, then return a Future-like object to user side (the future is set completed whenever client receives response from server). In my initial implementation, the send function looks like this:
    Code:
    private CompletableFuture send(Request request) {
        CompletableFuture future = new CompletableFuture<>();
        request.setFuture(future);
        getSession().send(request); // Issue an async IO here
        return request.getFuture();
    }
    Here, a new CompletableFuture is allocated which burdens Java GC a lot if send() is called frequently.
    Is there any better option to utilize CompletableFuture? Like CompletableFuture pool?
    Does anybody experience the difference between CompletableFuture and equivalents like Future of Netty/Akka/Finagle?
    Thank you.
     
  2. pinokio

    pinokio New Member

    If the approach is UN-Kalashnikov, so how we can make it more Kalashnikov-friendly?
    The problem is simple: User send a lot (hundreds) of requests simultaneously using SocketChannel (non-blocking mode), and define the callback when the response associated to this request arrives.
    For eg: Send (ASK_NAME, "Who are you?"), and when received "Joe", print "Hello Joe".
    Send (ASK_AGE, "How old are you?") and when received "18", print "18 year old".
    But what if two send commands are sent asynchronously, then the second response "18" arrived before "Joe"? We must implement the callback for "Hello X" for the ASK_NAME request and "Y year old" for the ASK_AGE. These callbacks are implemented by using kind of CompletabeFuture. I cannot figure out the better way to achieve the simplicity.
     
    Last edited: 6/1/16
  3. pinokio

    pinokio New Member

    ForkJoinPool?? Hmm, I am sorry that I don't know how to to get a CompletableFuture from a ForkJoinPool. In my implementation, I would like to return a CompletableFuture for each request sent, so that user can call thenXXX with any handler he want to do with the response corresponding to this request. In addition, how we can release this CompletableFuture to ForkJoinPool once user finishes using it?

    It is not as simple as chatting. For chatting, just show up every response received, regardless its type. The handler behavior is determined. But I am writing something like library or API, that does not know what user want to do with the response. CompletableFuture lets user do whatever he wants to do using thenXXX function.
    How about this scenario: There are two type of request to OODB: INIT and COUNT. My client library to OODB server must serve two corresponding functions.
    Now user want
    Code:
    1. Send INIT -> then (()->doFirst()); // Send the first INIT then call doFirst whenever receiving response for first INIT request.
    2.Send INIT -> then (()->doSecond()); // Send the second INIT then call doSecond whenever receiving response for second INIT request.
    2.Send COUNT -> then (()->doCount()); // Send the first COUNT then call doCount whenever receiving response for first COUNT request.
    
    Then, the server return response data in corresponding order of requests: INIT(for 2nd request), INIT(for 1st request), COUNT. Hence, the order of handler execution is doSecond(), doFirst(), doCount(). Remember as API developer, we do not know the exact handler in advance. All we know is there is a handler associated with each request sent, and user must define this handler for each request sent.
    Clearly chatting implementation is not enough, I think.
    I hope I can explain clearly enough. Sorry for my bad writing skill.
     
  4. Nancru

    Nancru CongDongJava Project Leader Staff Member

    So, pinokio wants such thing that when a user send request to server -> then server return a response, what user can do whatever with it, until he closes or terminates the connection(response) ->then server will pay the CompleleableFuture to the pool once user closes or terminates it.

    If it's the case, I think you are thinking too much, pinokio. OODB is simple, server returns data with their serialized objects. They will execute on their own computer or mobile or whatever it is. The moment connection is closed when server transfer all requested data to their client.

    Give them permission to continuing play with the connections is dangerous and complex.
     
    Joe likes this.
  5. pinokio

    pinokio New Member

    Thank Joe and Nancru. What I mean about the request and response is about the data to be sent/received, not the connection. There are many requests sent in one connection (one channel). The time and order of response data paired with each request is unpredictable, hence CF comes into play. Btw, Thank Joe for the hint of supplyAsync, how neglect I am about not considering it.
     

Chia sẻ trang này

Loading...