Search in sources :

Example 6 with AsyncHandler

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

the class DispatchClientServerTest method testSOAPMessageInvokeToOneWay.

@Test
public void testSOAPMessageInvokeToOneWay() throws Exception {
    SOAPService service = new SOAPService(null, SERVICE_NAME);
    service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, "http://localhost:" + greeterPort + "/SOAPDispatchService/SoapDispatchPort");
    assertNotNull(service);
    Dispatch<SOAPMessage> disp = service.createDispatch(PORT_NAME, SOAPMessage.class, Service.Mode.MESSAGE);
    // message is "one way", but there really isn't a way for us to know that
    // as we don't have a wsdl or other source of operation information to
    // compare the payload to.
    InputStream is1 = getClass().getResourceAsStream("resources/GreetMe1WDocLiteralReq2.xml");
    SOAPMessage soapReqMsg1 = MessageFactory.newInstance().createMessage(null, is1);
    assertNotNull(soapReqMsg1);
    // Version 1:
    // we'll just call invoke
    disp.invoke(soapReqMsg1);
    // Version 2:
    // We want to handle things asynchronously
    AsyncHandler<SOAPMessage> callback = new AsyncHandler<SOAPMessage>() {

        public void handleResponse(Response<SOAPMessage> res) {
            synchronized (this) {
                notifyAll();
            }
        }
    };
    synchronized (callback) {
        disp.invokeAsync(soapReqMsg1, callback);
        callback.wait();
    }
}
Also used : SOAPService(org.apache.hello_world_soap_http.SOAPService) GreetMeResponse(org.apache.hello_world_soap_http.types.GreetMeResponse) Response(javax.xml.ws.Response) AsyncHandler(javax.xml.ws.AsyncHandler) InputStream(java.io.InputStream) SOAPMessage(javax.xml.soap.SOAPMessage) Test(org.junit.Test)

Example 7 with AsyncHandler

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

the class AsyncHTTPConduitTest method testCallAsync.

@Test
public void testCallAsync() throws Exception {
    updateAddressPort(g, PORT);
    GreetMeResponse resp = (GreetMeResponse) g.greetMeAsync(request, new AsyncHandler<GreetMeResponse>() {

        public void handleResponse(Response<GreetMeResponse> res) {
            try {
                res.get().getResponseType();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }).get();
    assertEquals("Hello " + request, resp.getResponseType());
    g.greetMeLaterAsync(1000, new AsyncHandler<GreetMeLaterResponse>() {

        public void handleResponse(Response<GreetMeLaterResponse> res) {
        }
    }).get();
}
Also used : GreetMeLaterResponse(org.apache.hello_world_soap_http.types.GreetMeLaterResponse) GreetMeResponse(org.apache.hello_world_soap_http.types.GreetMeResponse) Response(javax.xml.ws.Response) AsyncHandler(javax.xml.ws.AsyncHandler) ExecutionException(java.util.concurrent.ExecutionException) GreetMeResponse(org.apache.hello_world_soap_http.types.GreetMeResponse) Test(org.junit.Test)

Example 8 with AsyncHandler

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

the class AsyncHTTPConduitTest method testCallAsyncCallbackInvokedOnlyOnce.

@Test
public void testCallAsyncCallbackInvokedOnlyOnce() throws Exception {
    // This test is especially targeted for RHEL 6.8
    updateAddressPort(g, PORT_INV);
    int repeat = 100;
    final AtomicInteger count = new AtomicInteger(0);
    for (int i = 0; i < repeat; i++) {
        try {
            g.greetMeAsync(request, new AsyncHandler<GreetMeResponse>() {

                public void handleResponse(Response<GreetMeResponse> res) {
                    count.incrementAndGet();
                }
            }).get();
        } catch (Exception e) {
        }
    }
    Thread.sleep(1000);
    assertEquals("Callback should be invoked only once per request", repeat, count.intValue());
}
Also used : GreetMeLaterResponse(org.apache.hello_world_soap_http.types.GreetMeLaterResponse) GreetMeResponse(org.apache.hello_world_soap_http.types.GreetMeResponse) Response(javax.xml.ws.Response) AsyncHandler(javax.xml.ws.AsyncHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(javax.xml.ws.Endpoint) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 9 with AsyncHandler

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

the class JMSClientServerTest method testAsyncCall.

@Ignore
@Test
public void testAsyncCall() throws Exception {
    QName serviceName = new QName("http://cxf.apache.org/hello_world_jms", "HelloWorldService");
    QName portName = new QName("http://cxf.apache.org/hello_world_jms", "HelloWorldPort");
    URL wsdl = getWSDLURL("/wsdl/jms_test.wsdl");
    HelloWorldService service = new HelloWorldService(wsdl, serviceName);
    HelloWorldPortType greeter = service.getPort(portName, HelloWorldPortType.class);
    final Thread thread = Thread.currentThread();
    class TestAsyncHandler implements AsyncHandler<String> {

        String expected;

        TestAsyncHandler(String x) {
            expected = x;
        }

        public String getExpected() {
            return expected;
        }

        public void handleResponse(Response<String> response) {
            try {
                Thread thread2 = Thread.currentThread();
                assertNotSame(thread, thread2);
                assertEquals("Hello " + expected, response.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
    TestAsyncHandler h1 = new TestAsyncHandler("Homer");
    TestAsyncHandler h2 = new TestAsyncHandler("Maggie");
    TestAsyncHandler h3 = new TestAsyncHandler("Bart");
    TestAsyncHandler h4 = new TestAsyncHandler("Lisa");
    TestAsyncHandler h5 = new TestAsyncHandler("Marge");
    Future<?> f1 = greeter.greetMeAsync("Santa's Little Helper", new TestAsyncHandler("Santa's Little Helper"));
    f1.get();
    f1 = greeter.greetMeAsync("PauseForTwoSecs Santa's Little Helper", new TestAsyncHandler("Santa's Little Helper"));
    long start = System.currentTimeMillis();
    f1 = greeter.greetMeAsync("PauseForTwoSecs " + h1.getExpected(), h1);
    Future<?> f2 = greeter.greetMeAsync("PauseForTwoSecs " + h2.getExpected(), h2);
    Future<?> f3 = greeter.greetMeAsync("PauseForTwoSecs " + h3.getExpected(), h3);
    Future<?> f4 = greeter.greetMeAsync("PauseForTwoSecs " + h4.getExpected(), h4);
    Future<?> f5 = greeter.greetMeAsync("PauseForTwoSecs " + h5.getExpected(), h5);
    long mid = System.currentTimeMillis();
    assertEquals("Hello " + h1.getExpected(), f1.get());
    assertEquals("Hello " + h2.getExpected(), f2.get());
    assertEquals("Hello " + h3.getExpected(), f3.get());
    assertEquals("Hello " + h4.getExpected(), f4.get());
    assertEquals("Hello " + h5.getExpected(), f5.get());
    long end = System.currentTimeMillis();
    assertTrue("Time too long: " + (mid - start), (mid - start) < 1000);
    assertTrue((end - mid) > 1000);
    f1 = null;
    f2 = null;
    f3 = null;
    f4 = null;
    f5 = null;
    ((java.io.Closeable) greeter).close();
    greeter = null;
    service = null;
    System.gc();
}
Also used : AsyncHandler(javax.xml.ws.AsyncHandler) QName(javax.xml.namespace.QName) HelloWorldService(org.apache.cxf.hello_world_jms.HelloWorldService) Closeable(java.io.Closeable) HelloWorldPortType(org.apache.cxf.hello_world_jms.HelloWorldPortType) URL(java.net.URL) Response(javax.xml.ws.Response) ExecutionException(java.util.concurrent.ExecutionException) Ignore(org.junit.Ignore) Test(org.junit.Test)

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