synapse的代理功能


1. 发布一个Web Services工程。
    1) 通过MyEclipse写一个Hello的ws。
        接口文件:IHello.java
        实现文件:HelloImpl.java
        XFire配置文件: services.xml
        Web配置文件:web.xml

接口文件:IHello.java
public interface IHello {
	public String sayHello(String name);
}


实现文件:HelloImpl.java
public class HelloImpl implements IHello {
	
	public String sayHello(String name) {
		return "Hello "+name;
	}
	
}


XFire配置文件: services.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

	<service>
		<name>Hello</name>
		<serviceClass>org.conan.IHello</serviceClass>
		<implementationClass>org.conan.HelloImpl</implementationClass>
		<style>document</style>
		<use>literal</use>
		<scope>request</scope>
	</service>
</beans>


Web配置文件:web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>


2) 发布web工程到Tomcat服务器:Tomcat端口8088
WS的访问地址为:http://localhost:8088/HelloESB1/services/Hello?wsdl


2. 修改synapse.xml的配置文件,和刚才发布的Web Services服务建立关联。
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <proxy name="Hello">
        <target>
            <endpoint>
                <address uri="http://localhost:8088/HelloESB1/services/Hello"/>
            </endpoint>
            <outSequence>
                <send/>
            </outSequence>
        </target>
        <publishWSDL uri="http://localhost:8088/HelloESB1/services/Hello?wsdl"/>
    </proxy>
</definitions>


4. 创建synapse发布的服务器的客户端程序。
通过MyEclipse,客户端生成工具:使用http://localhost:8080/soap/Hello?wsdl生成客户端代码
因为代码都是生成的,只贴出main()调用代码:
    public static void main(String[] args) {
        HelloClient client = new HelloClient();
        
		//create a default service endpoint
        HelloPortType service0 = client.getHelloSOAP11port_https();
        HelloPortType service1 = client.getHelloSOAP11port_http1();
        
        String tmp = service1.sayHello("abc");
        System.out.println(tmp);
    }


5. 启动客户端程序,得到结果。
2008-6-28 12:09:20 org.apache.commons.httpclient.HttpMethodBase writeRequest
信息: 100 (continue) read timeout. Resume sending the request
Hello abc

你可能感兴趣的:(java,tomcat,xml,Web,MyEclipse)