Search in sources :

Example 1 with AsyncHandler

use of javax.xml.ws.AsyncHandler in project cxf by apache.

the class AsyncHTTPConduitTest method testCallsAsync.

@Test
@Ignore("peformance test")
public void testCallsAsync() throws Exception {
    updateAddressPort(g, PORT);
    final int warmupIter = 5000;
    final int runIter = 5000;
    final CountDownLatch wlatch = new CountDownLatch(warmupIter);
    final boolean[] wdone = new boolean[warmupIter];
    @SuppressWarnings("unchecked") AsyncHandler<GreetMeLaterResponse>[] whandler = new AsyncHandler[warmupIter];
    for (int x = 0; x < warmupIter; x++) {
        final int c = x;
        whandler[x] = new AsyncHandler<GreetMeLaterResponse>() {

            public void handleResponse(Response<GreetMeLaterResponse> res) {
                try {
                    String s = res.get().getResponseType();
                    s = s.substring(s.lastIndexOf(' ') + 1);
                    if (c != Integer.parseInt(s)) {
                        System.out.println("Problem " + c + " != " + s);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                wdone[c] = true;
                wlatch.countDown();
            }
        };
    }
    // warmup
    long start = System.currentTimeMillis();
    for (int x = 0; x < warmupIter; x++) {
        // builder.append("a");
        // long s1 = System.nanoTime();
        // System.out.println("aa1: " + s1);
        g.greetMeLaterAsync(x, whandler[x]);
    // long s2 = System.nanoTime();
    // System.out.println("aa2: " + s2 + " " + (s2 - s1));
    // System.out.println();
    }
    wlatch.await(30, TimeUnit.SECONDS);
    long end = System.currentTimeMillis();
    System.out.println("Warmup Total: " + (end - start) + " " + wlatch.getCount());
    for (int x = 0; x < warmupIter; x++) {
        if (!wdone[x]) {
            System.out.println("  " + x);
        }
    }
    if (wlatch.getCount() > 0) {
        Thread.sleep(1000000);
    }
    final CountDownLatch rlatch = new CountDownLatch(runIter);
    AsyncHandler<GreetMeLaterResponse> rhandler = new AsyncHandler<GreetMeLaterResponse>() {

        public void handleResponse(Response<GreetMeLaterResponse> res) {
            rlatch.countDown();
        }
    };
    start = System.currentTimeMillis();
    for (int x = 0; x < runIter; x++) {
        // builder.append("a");
        // long s1 = System.nanoTime();
        // System.out.println("aa1: " + s1);
        g.greetMeLaterAsync(x, rhandler);
    // long s2 = System.nanoTime();
    // System.out.println("aa2: " + s2 + " " + (s2 - s1));
    // System.out.println();
    }
    rlatch.await(30, TimeUnit.SECONDS);
    end = System.currentTimeMillis();
    System.out.println("Total: " + (end - start) + " " + rlatch.getCount());
}
Also used : AsyncHandler(javax.xml.ws.AsyncHandler) GreetMeLaterResponse(org.apache.hello_world_soap_http.types.GreetMeLaterResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(javax.xml.ws.Endpoint) GreetMeLaterResponse(org.apache.hello_world_soap_http.types.GreetMeLaterResponse) GreetMeResponse(org.apache.hello_world_soap_http.types.GreetMeResponse) Response(javax.xml.ws.Response) ExecutionException(java.util.concurrent.ExecutionException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with AsyncHandler

use of javax.xml.ws.AsyncHandler in project cxf by apache.

the class WSDiscoveryClient method probe.

public ProbeMatchesType probe(ProbeType params, int timeout) {
    Dispatch<Object> disp = this.getDispatchInternal(false, version.getProbeAction());
    if (adHoc) {
        disp.getRequestContext().put("udp.multi.response.timeout", timeout);
        final ProbeMatchesType response = new ProbeMatchesType();
        AsyncHandler<Object> handler = new AsyncHandler<Object>() {

            public void handleResponse(Response<Object> res) {
                try {
                    Object o = res.get();
                    while (o instanceof JAXBElement) {
                        o = ((JAXBElement) o).getValue();
                    }
                    if (o instanceof ProbeMatchesType) {
                        response.getProbeMatch().addAll(((ProbeMatchesType) o).getProbeMatch());
                    } else if (o instanceof HelloType) {
                        HelloType h = (HelloType) o;
                        QName sn = version.getServiceName();
                        if (h.getTypes().contains(sn) || h.getTypes().contains(new QName("", sn.getLocalPart()))) {
                            // A DiscoveryProxy wants us to flip to managed mode
                            uncache();
                            resetDispatch(h.getXAddrs().get(0));
                        }
                    }
                } catch (InterruptedException e) {
                // ?
                } catch (ExecutionException e) {
                // ?
                }
            }
        };
        disp.invokeAsync(new ObjectFactory().createProbe(params), handler);
        return response;
    }
    Object o = disp.invoke(new ObjectFactory().createProbe(params));
    while (o instanceof JAXBElement) {
        o = ((JAXBElement) o).getValue();
    }
    return (ProbeMatchesType) o;
}
Also used : Response(javax.xml.ws.Response) AsyncHandler(javax.xml.ws.AsyncHandler) ObjectFactory(org.apache.cxf.ws.discovery.wsdl.ObjectFactory) QName(javax.xml.namespace.QName) HelloType(org.apache.cxf.ws.discovery.wsdl.HelloType) ProbeMatchesType(org.apache.cxf.ws.discovery.wsdl.ProbeMatchesType) JAXBElement(javax.xml.bind.JAXBElement) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with AsyncHandler

use of javax.xml.ws.AsyncHandler in project cxf by apache.

the class JaxWsClientProxy method invokeAsync.

@SuppressWarnings("unchecked")
private Object invokeAsync(Method method, BindingOperationInfo oi, Object[] params) throws Exception {
    client.setExecutor(getClient().getEndpoint().getExecutor());
    AsyncHandler<Object> handler;
    if (params.length > 0 && params[params.length - 1] instanceof AsyncHandler) {
        handler = (AsyncHandler<Object>) params[params.length - 1];
        Object[] newParams = new Object[params.length - 1];
        for (int i = 0; i < newParams.length; i++) {
            newParams[i] = params[i];
        }
        params = newParams;
    } else {
        handler = null;
    }
    ClientCallback callback = new JaxwsClientCallback<Object>(handler, this);
    Response<Object> ret = new JaxwsResponseCallback<Object>(callback);
    client.invoke(callback, oi, params);
    return ret;
}
Also used : ClientCallback(org.apache.cxf.endpoint.ClientCallback) AsyncHandler(javax.xml.ws.AsyncHandler) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 4 with AsyncHandler

use of javax.xml.ws.AsyncHandler in project cxf by apache.

the class WSAPureWsdlTest method testBasicInvocationTimeouts.

@Test
public void testBasicInvocationTimeouts() throws Exception {
    AddNumbersPortType port = getPort();
    ((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:" + PORT + "/jaxws/add");
    HTTPConduit conduit = (HTTPConduit) ((Client) port).getConduit();
    conduit.getClient().setConnectionTimeout(25);
    conduit.getClient().setReceiveTimeout(10);
    try {
        // read timeout
        port.addNumbersAsync(5092, 25).get();
        fail("should have failed");
    } catch (Exception t) {
        // expected
        assertTrue(t.getCause().toString(), t.getCause() instanceof java.net.SocketTimeoutException);
    }
    AsyncHandler<AddNumbersResponse> handler = new AsyncHandler<AddNumbersResponse>() {

        public void handleResponse(Response<AddNumbersResponse> res) {
            // System.out.println("in handle response");
            synchronized (this) {
                notifyAll();
            }
        }
    };
    synchronized (handler) {
        port.addNumbersAsync(5092, 25, handler);
        handler.wait(1000);
    }
    try {
        // connection timeout
        ((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:" + PORT2 + "/jaxws/add");
        port.addNumbersAsync(25, 25).get();
        fail("should have failed");
    } catch (Exception t) {
        // expected
        assertTrue(t.getCause().getCause().toString(), t.getCause().getCause() instanceof java.net.ConnectException || t.getCause().getCause() instanceof java.net.SocketTimeoutException);
    }
    synchronized (handler) {
        port.addNumbersAsync(25, 25, handler);
        handler.wait(1000);
    }
    MAPCodec mp = getMAPCodec((Client) port);
    assertEquals(0, mp.getUncorrelatedExchanges().size());
}
Also used : AsyncHandler(javax.xml.ws.AsyncHandler) AddNumbersResponse(org.apache.cxf.systest.ws.addr_feature.AddNumbersResponse) MAPCodec(org.apache.cxf.ws.addressing.soap.MAPCodec) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) AddNumbersPortType(org.apache.cxf.systest.ws.addr_feature.AddNumbersPortType) HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) AddNumbersResponse(org.apache.cxf.systest.ws.addr_feature.AddNumbersResponse) Response(javax.xml.ws.Response) Test(org.junit.Test)

Example 5 with AsyncHandler

use of javax.xml.ws.AsyncHandler in project cxf by apache.

the class WSDiscoveryClient method resolve.

public ResolveMatchType resolve(W3CEndpointReference ref, int timeout) {
    Dispatch<Object> disp = this.getDispatchInternal(false, version.getResolveAction());
    ResolveType rt = new ResolveType();
    rt.setEndpointReference(ref);
    if (adHoc) {
        disp.getRequestContext().put("udp.multi.response.timeout", timeout);
        final Holder<ResolveMatchesType> response = new Holder<ResolveMatchesType>();
        AsyncHandler<Object> handler = new AsyncHandler<Object>() {

            public void handleResponse(Response<Object> res) {
                try {
                    Object o = res.get();
                    while (o instanceof JAXBElement) {
                        o = ((JAXBElement) o).getValue();
                    }
                    if (o instanceof ResolveMatchesType) {
                        response.value = (ResolveMatchesType) o;
                    } else if (o instanceof HelloType) {
                        HelloType h = (HelloType) o;
                        QName sn = version.getServiceName();
                        if (h.getTypes().contains(sn) || h.getTypes().contains(new QName("", sn.getLocalPart()))) {
                            // A DiscoveryProxy wants us to flip to managed mode
                            uncache();
                            resetDispatch(h.getXAddrs().get(0));
                        }
                    }
                } catch (InterruptedException e) {
                // ?
                } catch (ExecutionException e) {
                // ?
                }
            }
        };
        disp.invokeAsync(new ObjectFactory().createResolve(rt), handler);
        return response.value == null ? null : response.value.getResolveMatch();
    }
    Object o = disp.invoke(new ObjectFactory().createResolve(rt));
    while (o instanceof JAXBElement) {
        o = ((JAXBElement) o).getValue();
    }
    return o == null ? null : ((ResolveMatchesType) o).getResolveMatch();
}
Also used : AsyncHandler(javax.xml.ws.AsyncHandler) ResolveMatchesType(org.apache.cxf.ws.discovery.wsdl.ResolveMatchesType) QName(javax.xml.namespace.QName) Holder(javax.xml.ws.Holder) ResolveType(org.apache.cxf.ws.discovery.wsdl.ResolveType) HelloType(org.apache.cxf.ws.discovery.wsdl.HelloType) JAXBElement(javax.xml.bind.JAXBElement) Response(javax.xml.ws.Response) ObjectFactory(org.apache.cxf.ws.discovery.wsdl.ObjectFactory) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

AsyncHandler (javax.xml.ws.AsyncHandler)9 Response (javax.xml.ws.Response)8 ExecutionException (java.util.concurrent.ExecutionException)7 Test (org.junit.Test)6 GreetMeResponse (org.apache.hello_world_soap_http.types.GreetMeResponse)4 QName (javax.xml.namespace.QName)3 GreetMeLaterResponse (org.apache.hello_world_soap_http.types.GreetMeLaterResponse)3 JAXBElement (javax.xml.bind.JAXBElement)2 Endpoint (javax.xml.ws.Endpoint)2 HelloType (org.apache.cxf.ws.discovery.wsdl.HelloType)2 ObjectFactory (org.apache.cxf.ws.discovery.wsdl.ObjectFactory)2 Ignore (org.junit.Ignore)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SOAPMessage (javax.xml.soap.SOAPMessage)1 Holder (javax.xml.ws.Holder)1