cxf 实例解读

本文详细介绍了Java Web服务的四种调用方式及其优化策略,包括使用Cxf、JAX-WS、Spring等框架进行服务发布与客户端调用,并通过实例展示了如何减少调用时间,提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.sample 实例之一---java_first_pojo

 服务端发布服务的方法:

复制代码
1         HelloWorldImpl helloworldImpl = new HelloWorldImpl();
         //cxf发布服务的工厂bean
2         ServerFactoryBean svrFactory = new ServerFactoryBean();
     //设置服务类
3         svrFactory.setServiceClass(HelloWorld.class);
     //设置服务地址
4         svrFactory.setAddress("http://localhost:9000/Hello");
     //设置服务bean
5         svrFactory.setServiceBean(helloworldImpl);
6         svrFactory.create();
复制代码

客户度调用的方法:

复制代码
//创建服务代理工程bean
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
//设置服务代理地址
factory.setAddress("http://localhost:9000/Hello");
//创建代理服务
HelloWorld client = factory.create(HelloWorld.class);
//调用代理服务
System.out.println(client.sayHi(System.getProperty("user.name")));
复制代码

2.sample实例之二---java_first_jaxws

服务端发布服务的方法:

1         HelloWorldImpl implementor = new HelloWorldImpl();
2         String address = "http://localhost:9000/helloWorld";
3         Endpoint.publish(address, implementor);

客户端调用的方法:

复制代码
    private static final QName SERVICE_NAME  = new QName("http://server.hw.demo/", "HelloWorld");
    private static final QName PORT_NAME = new QName("http://server.hw.demo/", "HelloWorldPort");
        Service service = Service.create(SERVICE_NAME);
        // Endpoint Address
        String endpointAddress = "http://localhost:9000/helloWorld";
        // If web service deployed on Tomcat deployment, endpoint should be changed to:
        // String endpointAddress = "http://localhost:8080/java_first_jaxws/services/hello_world";

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
        
        HelloWorld hw = service.getPort(HelloWorld.class);
复制代码

 

3. sample实例之---java_first_jaxws_factory_bean

服务端发布服务的方法:

复制代码
1         HelloWorldImpl implementor = new HelloWorldImpl();
2         JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
3         svrFactory.setServiceClass(HelloWorld.class);
4         svrFactory.setAddress("http://localhost:9000/helloWorld");
5         svrFactory.setServiceBean(implementor);
6         svrFactory.getInInterceptors().add(new LoggingInInterceptor());
7         svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
8         svrFactory.create();
复制代码

客户端调用的方法:

1         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
2         factory.getInInterceptors().add(new LoggingInInterceptor());
3         factory.getOutInterceptors().add(new LoggingOutInterceptor());
4         factory.setAddress("http://localhost:9000/helloWorld");
5         HelloWorld client = factory.create(HelloWorld.class);
6         System.out.println(client.sayHi("World"));

4.sample实例之一---java_first_spring_support

服务端发布服务

复制代码
 1         /**
 2          * Important: This code simply starts up a servlet container and adds
 3          * the web application in src/webapp to it. Normally you would be using
 4          * Jetty or Tomcat and have the webapp packaged as a WAR. This is simply
 5          * as a convenience so you do not need to configure your servlet
 6          * container to see CXF in action!
 7          */
 8         org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server();
 9 
10         SelectChannelConnector connector = new SelectChannelConnector();
11         connector.setPort(9002);
12         server.setConnectors(new Connector[] {connector});
13 
14         WebAppContext webappcontext = new WebAppContext();
15         webappcontext.setContextPath("/");
16 
17         webappcontext.setWar("target/JavaFirstSpringSupport.war");
18 
19         HandlerCollection handlers = new HandlerCollection();
20         handlers.setHandlers(new Handler[] {webappcontext, new DefaultHandler()});
21 
22         server.setHandler(handlers);
23         server.start();
24         System.out.println("Server ready...");
25         server.join();
复制代码

客户度调用服务:

1         ClassPathXmlApplicationContext context 
2             = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
3 
4         HelloWorld client = (HelloWorld)context.getBean("client");
5 
6         String response = client.sayHi("Joe");

 

客户度调用小结

(引用http://blog.youkuaiyun.com/liaomin416100569/article/details/5503410)

 public static void invokeMethod1() {
  long s=new Date().getTime();
  UserServiceImplService serivce = new UserServiceImplService();
  UserServiceImpl impl = serivce.getUserServiceImplPort();
  User u = new User();
  impl.addUser(u);
  long s1=new Date().getTime();
  System.out.println(s1-s);
  //第一次去调用1297
  //1437 1062 1063 1078 1047
 }
  public static void invokeMethod2() {
  long s=new Date().getTime();
  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  factory.setAddress("http://localhost:8088/abc");
   QName SERVICE = new QName("http://liaomin", "UserServiceImplService");
   factory.setServiceName(SERVICE);
  factory.setServiceClass(UserService.class);
  UserService us = (UserService) factory.create();
  User u = new User();
  // us.addUser(u);
  us.addUser(u);
  long s1=new Date().getTime();
  System.out.println(s1-s);
  //第一次去调用1265
  //1047 1047(比较稳定)
 }
  public static void invokeMethod3() throws MalformedURLException {
  long s=new Date().getTime();
     QName SERVICE = new QName("http://liaomin", "UserServiceImplService");
     QName UserServiceImplPort = new QName("http://liaomin", "UserServiceImplPort");
  URL url = new URL("http://localhost:8088/abc?wsdl");
  ServiceDelegate dele=Provider.provider().createServiceDelegate(url,SERVICE,Service.class);
  UserService us = (UserService) dele.getPort(UserServiceImplPort,UserService.class);
  User u = new User();
  us.addUser(u);
  long s1=new Date().getTime();
  System.out.println(s1-s);
  //第一次去调用 1281 
  //1047 1031 1047 1016 1032
 }
 public static void invokeMethod4()
 {
  long s=new Date().getTime();
  ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        factory.setServiceClass(UserService.class);
        factory.setAddress("http://localhost:8088/abc");
       // factory.getServiceFactory().setDataBinding(new AegisDatabinding());
        UserService client = (UserService) factory.create();
        User u = new User();
        client.addUser(u);
        long s1=new Date().getTime();
  System.out.println(s1-s);
  //第一次去调用 1188 
  //调用一次后   1016 1000 1016 1015
 }

=======================================================================================================

通过实践去测试 只有第四种调用耗费的时间最少的

如果在频繁取数据的系统中 能优化100ms的速度 对性能就会有大大的提高

比如我调用 100次webservice发送消息  每发送一次 是 1200ms(1.2s) 总共耗时 120s

如果采用第四种 只需要 1000ms(1s) 总共耗时 100s  那么优化了20s时间 20s是个什么概念

假如是调用 1000 10000次发送了  优化的效率就更大

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值