About Question enthuware.ocejws.v6.2.278 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
disznoperzselo
Posts: 28
Joined: Fri Jan 02, 2015 12:13 pm
Contact:

About Question enthuware.ocejws.v6.2.278 :

Post by disznoperzselo »

Why do you cast Future<?> .get() ? You cast it in enthuware.ocejws.v6.2.277 as well. In the explanation you argue against it:
"Client code should not attempt to cast the object to any particular type as this will result in non-portable behavior."

The Dispatch API is even more restrictive:
"This object MUST NOT be used to try to obtain the results of the operation"
https://docs.oracle.com/javaee/6/api/ja ... ncHandler)

How to get back the response from a callback? Do we really need it outside of it?
I think we don't. All the response handling should be done in the callback method.
Future<?> should be used only for polling scenarios.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.278 :

Post by fjwalraven »

You are absolutely right!

The JAX-WS User guide shows the correct way of dealing with the response (via the Handler):
https://jax-ws.java.net/2.2.1/docs/asynch.html

Code: Select all

1.2 Async Callback

    //async callback Method
    public Future<?> addNumbers(int number1, int number2, AsyncHandler<AddNumbersResponse>);

Here the client application provides an AsyncHandler by implementing the javax.xml.ws.AsyncHandler<T> interface.

    /**
     *
     * Async callback handler
     */
    class AddNumbersCallbackHandler implements AsyncHandler<AddNumbersResponse> {
        private AddNumbersResponse output;
        /*
         *
         * @see javax.xml.ws.AsyncHandler#handleResponse(javax.xml.ws.Response)
         */
        public void handleResponse(Response<AddNumbersResponse> response) {
            try {
                output = response.get();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        AddNumbersResponse getResponse(){
            return output;
        }
    }

The async handler is then passed as the last parameter of the async callback method:

    //instantiates the callback handler
    AddNumbersCallbackHandler callbackHandler = new AddNumbersCallbackHandler();

    //invoke the async callback method
    Future<?> resp = port.addNumbersAsync(number1, number2, callbackHandler);
    while(!resp.isDone()){
           //do something
    }
    System.out.println("The sum is: " + callbackHandler.getResponse().getReturn());
I will change the question accordingly.

Thank you for your feedback!

Regards,
Frits

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests