1、需求:
利用Axis2的接口动态调用EOS6.0 的WS服务(不需要生成Axis2的客户端Stub代码)。这里的WS服务是用EOS的逻辑构件实现的,并在Composite中装配成逻辑构件,并promote为WS服务。
2、方法
1)开发服务
-- 开发一个逻辑构件SvcDemoLC,逻辑构件中只有一个方法 getName(String param1),为了简单起见,返回值等于输入参数。
- 在构件装配节点中 创建一个复合构件
系统会字体打开复合构件的装配图,将前面创建的逻辑构件SvcDemoLC拖拉到装配图中,并将构件的名称命名为 DemoSvc
-- 暴露WS服务:在pallete中选择"promote"连接线,将DemoSvc接口promote为WS服务(即添加WS绑定)
2)利用Axis2调用服务
-- 构造SOAP调用的头,注意每个参数的类型需要用__type来标识,如,调用方法getFF(String param1)的XML节点如下:
<getName xmlns="http://www.primeton.com/com.primeton.svcprc.demo/com/primeton/svcprc/demo/SvcDemoLC">
<param1 __type="java:java.lang.String">你好</param1>
</getFF>
-- 通过ServiceClient.sendReceive() 发送调用请求
3、示例代码:
public class AxisCallEOSWS {
//目标端点
private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/eos-default/CalculatorSvc?wsdl");
public static void main(String[] args) {
try {
OMElement payload = getMyServiceOMElement();
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setTo(targetEPR);
options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
//构造客户端
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
System.out.println("payload:"+payload);
//调用
OMElement result = sender.sendReceive(payload);
System.out.println("Result:"+result);
//打印XML格式的输出
StringWriter writer = new StringWriter();
result.serialize(XMLOutputFactory.newInstance().createXMLStreamWriter(writer));
writer.flush();
System.out.println(writer.toString());
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
public static OMElement getMyServiceOMElement() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://www.primeton.com/com.primeton.svcprc.demo/com/primeton/svcprc/demo/CalculatorComponent", "");
//接口名称
OMElement method = fac.createOMElement("getFF", omNs);
//参数
OMElement n1 = fac.createOMElement("param1", omNs);
//参数的值
n1.setText("10");
//参数的类型,注意一点要传” __type”
OMAttribute _type = fac.createOMAttribute("__type", null, "java:java.lang.String") ;
n1.addAttribute(_type) ;
method.addChild(n1);
return method;