论坛首页 Java版 SOA

SOA我的实现[不用客户端接口]

浏览 2141 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2007-10-28 关键字: 原创
以前实现过mq与业务系统的连接,在第二个版本实现了一个light weight的esb框架,省去众多客户端的接口,现在要支持web service,我的想法如下:
系统启动时读取wsdl文件,初始化为系统原有的统一的消息结构(arraylist), developer向hashmap中插入parameter,然后从hashmap中获取response, esb根据消息结构和消息数据动态生成soap message然后用rpc调用web service, 得到response解析
现在要实现三步:
1. 解析wsdl, 可以用wsdl4j
2. 生成/解析soap message, 使用saaj
3。动态调用web service(忽略接口,传递服务和方法字符串)

有没有做过的,第三步我现在有点困难。。。
   
时间:2007-10-28
不用再重复发明轮子了,建议你可以使用Camel 以及 CXF实现 MQ Message到WebService端的调用。
   
0 请登录后投票
时间:2007-10-28
hehe, mq部分已经实现,现在要实现一个符合自己框架的轻量级的“cxf”
我看看jax-ws,希望dx们给点帮助,因为时间比较紧
   
0 请登录后投票
时间:2007-10-28
简单来说,你只需用使用CXF 的Client 就可以实现一个通用的Client的调用,具体代码可以参考Camel中的CXF component。
至于你说的第三点,这个可以参考CXF的Dynamic Client。

http://cwiki.apache.org/CXF20DOC/dynamic-clients.html

BTW, 目前发布的Groovy 的soap stack 实现也是基于CXF的Dynamic Client 来实现的。
   
0 请登录后投票
时间:2007-10-28
谢谢,明天研究一下,有问题再提问

搞完我放些我详细的想法及框架
   
0 请登录后投票
时间:2007-10-29
jax-ws是不是只能用在jdk1.5上, 如果用jdk1.4怎么办?
   
0 请登录后投票
时间:2007-10-29
harvey 写道
jax-ws是不是只能用在jdk1.5上, 如果用jdk1.4怎么办?


是,JAXWS只能在JDK 1.5 上使用。
   
0 请登录后投票
时间:2007-10-29
harvey 写道
jax-ws是不是只能用在jdk1.5上, 如果用jdk1.4怎么办?


是,JAXWS只能在JDK 1.5 上使用。
   
0 请登录后投票
时间:2007-10-29
我看到几种实现:
<1>DynamicProxy
<2>DynamicInvocation
<3>Stubs
都不是我想要的.

<4>SAAJ/JAXM:
public static void main(String[] args) {
try {
//First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();

//Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();

//Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();

// Populate the body
//Create the main element and namespace
SOAPElement bodyElement = body.addChildElement(envelope.createName("hello", "", "http://localhost:9082/WebServiceWeb/services/HelloWorld"));
//Add content
SOAPElement nameElement = bodyElement.addChildElement("name");
nameElement.addTextNode("Harvey");

//Save the message
message.saveChanges();

//Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();

//Send the message and get a reply

//Set the destination
String endpoint = "http://localhost:9082/WebServiceWeb/services/HelloWorld";
//Send the message
SOAPMessage reply = connection.call(message, endpoint);

//Check the output
System.out.println("\nRESPONSE:\n");
//Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();

//Close the connection
connection.close();

} catch (Exception e) {
System.out.println(e.getMessage());
}

}
I Got below error:
========================
REQUEST:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header/><soapenv:Body><hello xmlns="http://localhost:9082/WebServiceWeb/services/HelloWorld"><name xmlns="">Harvey</name></hello></soapenv:Body></soapenv:Envelope>

RESPONSE:

[Fatal Error] :-1:-1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.



<5>另一种方式:
public static void main(String args[]) {

Service service = new Service();
Call call = null;
try {
call = (Call) service.createCall();
} catch (ServiceException e) {
System.out.println("Service Exception ...");
e.printStackTrace();
}
try {
call.setTargetEndpointAddress(new URL("http://localhost:9082/WebServiceWeb/services/HelloWorld"));
} catch (MalformedURLException e1) {
System.out.println("Malformed URL ");
e1.printStackTrace();
}

String xml = "<name>IBM</name>";
System.out.println("the xml :::::::::::::::::::::" + xml);
Document doc = null;
try {
doc = XMLUtils.newDocument(new ByteArrayInputStream(xml.getBytes()));
} catch (Exception e4) {
System.out.println("IO Exception ");
e4.printStackTrace();
}
Element cdataElem;
cdataElem = doc.createElementNS("http://localhost:9082/WebServiceWeb/services/HelloWorld", "hello");
Element reqElem;
reqElem = doc.getDocumentElement();
cdataElem.appendChild(reqElem);
SOAPBodyElement[] input = new SOAPBodyElement[1];

input[0] = new SOAPBodyElement(cdataElem);

Vector elems = null;
try {
elems = (Vector) call.invoke(input);
} catch (RemoteException e5) {
System.out.println("Remote Exception");
e5.printStackTrace();
}
if (elems != null) {
for (Iterator i = elems.iterator(); i.hasNext();){
System.out.println("The Element:" + i.next());
}
}else{
System.out.println("Elements are null");
}
}
成功:
[color=darkred]the xml :::::::::::::::::::::<name>IBM</name>
The Element:<ns1:helloResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://localhost:9082/WebServiceWeb/services/HelloWorld" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<helloReturn xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Hello IBM</helloReturn>
</ns1:helloResponse>
[/color]

谁知道为什么吗?
第4和第5种方式那个比较好呢?


wsdl 文件:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://service.test.harvey.com" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://service.test.harvey.com" xmlns:intf="http://service.test.harvey.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types/>

<wsdl:message name="helloResponse">

<wsdl:part name="helloReturn" type="xsd:string"/>

</wsdl:message>

<wsdl:message name="helloRequest">

<wsdl:part name="name" type="xsd:string"/>

</wsdl:message>

<wsdl:portType name="HelloWorld">

<wsdl:operation name="hello" parameterOrder="name">

<wsdl:input message="impl:helloRequest" name="helloRequest"/>

<wsdl:output message="impl:helloResponse" name="helloResponse"/>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld">

<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation name="hello">

<wsdlsoap:operation soapAction=""/>

<wsdl:input name="helloRequest">

<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://service.test.harvey.com" use="encoded"/>

</wsdl:input>

<wsdl:output name="helloResponse">

<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://service.test.harvey.com" use="encoded"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="HelloWorldService">

<wsdl:port binding="impl:HelloWorldSoapBinding" name="HelloWorld">

<wsdlsoap:address location="http://localhost:9082/WebServiceWeb/services/HelloWorld"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>
   
0 请登录后投票
时间:2007-10-30
从你的WSDL来看,服务端的是采用rpc-encoded方式来进行编码的。
同时由于这种编码方式不具备互操作性,CXF不支持这种编码方式。

还有你的4,5两种code都需要自己负责Message marshal 和unmahsal工作,要想写成一个通用的Client我觉得工作量很大。
   
0 请登录后投票
论坛首页 Java版 SOA

跳转论坛:
JavaEye推荐