使用CXF中的Aegis开发WS使用

package com.easyway.cxf.service;

import java.util.List;

import com.easyway.cxf.model.User;
/**
 *
 * @author longgangbai
 *
 */
public interface HelloService {
 /**
  * The @WebParam annotation is necessary as java interfaces do not store the Parameter name in the .class file. So if you leave out the annotation your parameter will be named arg0.
  * @param name
  * @return
  */
  public String hello(String name);
 
  /**
   * Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not
   * support interfaces directly.  Special XmlAdapter classes need to
   * be written to handle them
   */
  public String sayHi(User user);

  public String[] getAllUseNames(List<User> userList);
}
package com.easyway.cxf.service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.easyway.cxf.model.User;
/**
 * 采用Aegis CXF 的使用发布WS
 * @author longgangbai
 *
 */
public class HelloServiceImpl implements HelloService {

    Map<Integer, User> users = new LinkedHashMap<Integer, User>();
   

 public String hello(String username) {
        return "Hello " + username;
 }

 public String sayHi(User user) {
         users.put(users.size() + 1, user);
         return "Hello "  + user.getUsername();
 }
 public String[] getAllUseNames(List<User> userList) {
  String[] userListArr=new String[userList.size()];
  for (int i=0;i<userList.size();i++) {
   userListArr[i]=userList.get(i).getUsername();
  }
  return userListArr;
 }

}

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements. See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership. The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<mappings>
    <mapping name="HelloService">
        <method name="hello">
            <parameter index="0" name="greeting" nillable='false' />
        </method>
    </mapping>
</mappings>

 

 

package com.easyway.cxf.test.server;

import org.apache.cxf.aegis.databinding.AegisDatabinding;
import org.apache.cxf.frontend.ServerFactoryBean;

import com.easyway.cxf.service.HelloService;
import com.easyway.cxf.service.HelloServiceImpl;

public class AegisCXFServer {
 public static final String SERVICE_ADDRESS="http://localhost:8080/services/helloService";
      public static void main(String args[]) throws Exception {
       HelloService helloworldImpl = new HelloServiceImpl();
          ServerFactoryBean svrFactory = new ServerFactoryBean();
          svrFactory.setServiceClass(HelloService.class);
          svrFactory.setAddress(SERVICE_ADDRESS);
          svrFactory.setServiceBean(helloworldImpl);
          svrFactory.getServiceFactory().setDataBinding(new AegisDatabinding());
          svrFactory.create();
          System.out.println("Server ready...");
 
          Thread.sleep(5 * 60 * 1000);
          System.out.println("Server exiting");
          System.exit(0);
      }
 }
package com.easyway.cxf.test.client;


import org.apache.cxf.aegis.databinding.AegisDatabinding;
import org.apache.cxf.frontend.ClientProxyFactoryBean;

import com.easyway.cxf.service.HelloService;
import com.easyway.cxf.test.server.AegisCXFServer;

 

public final class AegisCXFClient {

    private AegisCXFClient() {
    }

    public static void main(String args[]) throws Exception {
        ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        factory.setServiceClass(HelloService.class);
        if (args != null && args.length > 0 && !"".equals(args[0])) {
            factory.setAddress(args[0]);
        } else {
            factory.setAddress(AegisCXFServer.SERVICE_ADDRESS);
        }
        factory.getServiceFactory().setDataBinding(new AegisDatabinding());
        HelloService client = (HelloService)factory.create();
        System.out.println("Invoke hello()....");
        System.out.println(client.hello(System.getProperty("user.name")));
        System.exit(0);
    }

}

你可能感兴趣的:(apache,thread,xml,UseCase)