您的位置:首页 > 编程语言 > Java开发

CXF之使用工厂方法(java代码)发布服务与进行客户端调用

2010-07-31 16:45 1031 查看
这是使用工厂方法,并用java代码发布服务的实例:

]package server;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Server {
protected Server() throws Exception {

System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
//服务端:服务发布工厂(JaxWsServerFactoryBean)
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/helloWorld");
svrFactory.setServiceBean(implementor);
svrFactory.getInInterceptors().add(new LoggingInInterceptor());
svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
svrFactory.create();
}
public static void main(String args[]) throws Exception {

new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}


这是使用工厂方法,并用java代码调用服务的实例:

]package client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import server.HelloWorld;
public final class Client {
private Client() {}
public static void main(String args[]) throws Exception {

//客户端:代理工厂(JaxWsProxyFactoryBean)
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:9000/helloWorld");
HelloWorld client = (HelloWorld) factory.create();
System.out.println(client.sayHi("World"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: