開源ESB--Mule與CXF的簡單實例

在開源ESB中最活躍的就是Mule和ServiceMix了。爲了便於大家更好的瞭解Mule和CXF,在此我通過一個實例來說明如何在Mule中使用CXF。
1.相關類
public class Product implements Serializable {
private String id;
private String description;
private int width;
private int height;

public Product() {
}

public Product(String id, String description, int width, int height) {
this.id = id;
this.description = description;
this.width = width;
this.height = height;
}
//相關的set,get方法。省略
}


@WebService
public interface ProductCatalogService {

@WebResult(name="item")
public List<String> listProducts();

@WebResult(name="product")
public Product getProductDetail(@WebParam(name="productId") String productId);

}


@WebService(endpointInterface = "com.honno.demo.product.ProductCatalogService",
serviceName = "ProductCatalogService")
public class ProductCatalogServiceImpl implements ProductCatalogService {
Map<String, Product> productMap = new HashMap<String, Product>();

public ProductCatalogServiceImpl() {
// Load some products
Product product = new Product("product1", "Square Widget", 10, 10);
productMap.put(product.getId(), product);
product = new Product("product2", "Round Widget", 5, 5);
productMap.put(product.getId(), product);
}

public List<String> listProducts() {
List<String> productListing = new ArrayList<String>();

Collection<Product> products = productMap.values();
for (Product p : products) {
productListing.add(p.getId() + " - " + p.getDescription());
}

return productListing;
}

public Product getProductDetail(String productId) {
Product product = null;
product = productMap.get(productId);
return product;
}


2.mule的配置文件
<spring:beans>
<spring:import resource="catalogContext.xml"/>
</spring:beans>

<model name="services">
<service name="ProductCatalogService">
<inbound>
<cxf:inbound-endpoint address="http://localhost:65082/services/ProductCatalogService" />
</inbound>
<component>
<spring-object bean="productCatalogService" />
</component>
</service>
</model>

3.生成wsdl文件
Mule成功啓動後,在ie的地址欄中通過http://localhost:65082/services/ProductCatalogService?wsdl即可查看wsdl文件。

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://product.demo.honno.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ProductCatalogService" targetNamespace="http://product.demo.honno.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://product.demo.honno.com/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://product.demo.honno.com/">
<xs:complexType name="product">
<xs:sequence>
<xs:element minOccurs="0" name="description" type="xs:string"/>
<xs:element name="height" type="xs:int"/>
<xs:element minOccurs="0" name="id" type="xs:string"/>
<xs:element name="width" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:element name="listProducts" type="listProducts"/>
<xs:complexType name="listProducts">
<xs:sequence/>
</xs:complexType>
<xs:element name="listProductsResponse" type="listProductsResponse"/>
<xs:complexType name="listProductsResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="item" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="getProductDetail" type="getProductDetail"/>
<xs:complexType name="getProductDetail">
<xs:sequence>
<xs:element minOccurs="0" name="productId" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="getProductDetailResponse" type="getProductDetailResponse"/>
<xs:complexType name="getProductDetailResponse">
<xs:sequence>
<xs:element minOccurs="0" name="product" type="product"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getProductDetailResponse">
<wsdl:part element="tns:getProductDetailResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="listProductsResponse">
<wsdl:part element="tns:listProductsResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="listProducts">
<wsdl:part element="tns:listProducts" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getProductDetail">
<wsdl:part element="tns:getProductDetail" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="ProductCatalogService">
<wsdl:operation name="listProducts">
<wsdl:input message="tns:listProducts" name="listProducts">
</wsdl:input>
<wsdl:output message="tns:listProductsResponse" name="listProductsResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getProductDetail">
<wsdl:input message="tns:getProductDetail" name="getProductDetail">
</wsdl:input>
<wsdl:output message="tns:getProductDetailResponse" name="getProductDetailResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ProductCatalogServiceSoapBinding" type="tns:ProductCatalogService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="listProducts">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="listProducts">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="listProductsResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getProductDetail">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getProductDetail">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getProductDetailResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ProductCatalogService">
<wsdl:port binding="tns:ProductCatalogServiceSoapBinding" name="ProductCatalogServiceImplPort">
<soap:address location="http://localhost:65082/services/ProductCatalogService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

4.生成客戶端
通過wsdl我們就可以生成客戶端代碼,對webservice進行調用了。

更多內容,包括更加詳細的源代碼,見[url] http://www.opensourceforce.org/?fromuid=217 [/url] (Mule的版塊中可以找到相關代碼)。
歡迎大家交流。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章