Async CXF

1.create client with maven
2.modify pom.xml
 <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.2.3</version>
        </dependency>
...

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
4. add wsdl2java pulgin

<plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>2.2.3</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>

                        <configuration>
                            <sourceRoot>${basedir}/target/generated-sources/cxf/</sourceRoot><!-- in netbeans, must be }/target/generated-sources/{pulgin name} -->
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${basedir}/src/main/resources/helloworld.wsdl</wsdl><!-- wsdl location -->
                                    <extraargs>
                                        <extraarg>-b</extraarg>
                                        <extraarg>${basedir}/src/main/resources/async_binding.xml</extraarg> ><!-- binding config file location -->
                                         <extraarg>-client</extraarg>
                                    </extraargs>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

5. copy wsdl file to src/main/resources

6. create async_binding.xml in src/main/resource
<?xml version="1.0" encoding="utf-8"?>
<bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
          wsdlLocation="helloworld.wsdl"
          xmlns="http://java.sun.com/xml/ns/jaxws">
  <bindings node="wsdl:definitions">
    <enableAsyncMapping>true</enableAsyncMapping>
  </bindings>
</bindings>

7. compile

8. create spring config file and config.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

     <jaxws:client id="cxfClient" serviceClass="com.yy.cxf.HelloWorld"
        address="http://localhost:8080/cxf/webservice/helloworld" />
</beans>

9.main.java

poll:

      AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:cxf-client.xml");
            ctx.start();
            ctx.registerShutdownHook();
            HelloWorld client = (HelloWorld) ctx.getBean("cxfClient");
            User u = new User();
            u.setName("yy");
            u.setType(UserType.MANAGER);
            Response<SayHelloResponse> sayHelloAsync = client.sayHelloAsync(u);
            while(sayHelloAsync.isDone() == false){
                Thread.sleep(1000);
            }
          SayHelloResponse response =  sayHelloAsync.get();
          System.out.println(response.getReturn().getInfo().get(0));

callback:

 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:cxf-client.xml");
            ctx.start();
            ctx.registerShutdownHook();
            HelloWorld client = (HelloWorld) ctx.getBean("cxfClient");
            User u = new User();
            u.setName("yy");
            u.setType(UserType.MANAGER);
            client.sayHelloAsync(u, new AsyncHandler<SayHelloResponse>(){

                public void handleResponse(Response<SayHelloResponse> res) {
                    try {
                        System.out.println(res.get().getReturn().getInfo().get(0));
                    } catch (InterruptedException ex) {
                        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ExecutionException ex) {
                        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });

            Thread.sleep(7000);



done.
posted @ 2009-08-31 16:39  思粮  阅读(453)  评论(0编辑  收藏  举报