如何在 Mule 中调用 Java 组件 [英] How to invoke a java component in Mule
问题描述
我正在使用以下 SOAP 客户端在 Apache OFBiz 中调用 Web 服务:
I'm using the following SOAP client to inovke a web service in Apache OFBiz:
public class CreatePerson {
private static OMFactory fac;
private static OMNamespace omNs;
public static String fname;
public static String lname;
static {
fac = OMAbstractFactory.getOMFactory();
omNs = fac.createOMNamespace("http://my-ip-adress/service/", "ns1");
}
public static void main(String[] args) throws AxisFault {
ServiceClient sc = new ServiceClient();
Options opts = new Options();
opts.setTo(new EndpointReference("http://my-ip-adress:port/webtools/control/SOAPService"));
opts.setAction("createPerson");
sc.setOptions(opts);
OMElement res = sc.sendReceive(createPayLoad(fname, lname));
System.out.println(res);
}
public static OMElement createPayLoad(@XPath("//person/return[1]")String firstName, @XPath("//person/return[2]")String lastName) {
CreatePerson.fname = firstName;
CreatePerson.lname = lastName;
OMElement createPerson = fac.createOMElement("createPerson", omNs);
OMElement mapMap = fac.createOMElement("map-Map", omNs);
createPerson.addChild(mapMap);
mapMap.addChild(createMapEntry("login.username", "admin"));
mapMap.addChild(createMapEntry("login.password", "ofbiz"));
// do the mapping here!
mapMap.addChild(createMapEntry("firstName", firstName));
mapMap.addChild(createMapEntry("lastName", lastName));
return createPerson;
}
public static OMElement createMapEntry(String key, String val) {
OMElement mapEntry = fac.createOMElement("map-Entry", omNs);
// create the key
OMElement mapKey = fac.createOMElement("map-Key", omNs);
OMElement keyElement = fac.createOMElement("std-String", omNs);
OMAttribute keyAttribute = fac.createOMAttribute("value", null, key);
mapKey.addChild(keyElement);
keyElement.addAttribute(keyAttribute);
// create the value
OMElement mapValue = fac.createOMElement("map-Value", omNs);
OMElement valElement = fac.createOMElement("std-String", omNs);
OMAttribute valAttribute = fac.createOMAttribute("value", null, val);
mapValue.addChild(valElement);
valElement.addAttribute(valAttribute);
// attach to map-Entry
mapEntry.addChild(mapKey);
mapEntry.addChild(mapValue);
return mapEntry;
}
}
接下来,我想使用以下 xml 将返回元素中的值获取到映射"(AnnotatedEntryPointResolver),并在上面的客户端中使用 firstName 和 lastName:
Next I want to use the following xml to get the values in return-element to "map" (AnnotatedEntryPointResolver) with firstName and lastName in my client above:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getAllResponse xmlns:ns2="http://service.ofbiz.org/">
<person>
<return>Testname</return>
<return>Test</return>
</person>
</ns2:getAllResponse>
</soap:Body>
</soap:Envelope>
因此我使用 Mule.正如您在我的客户端代码中看到的,我添加了一些 XPath 注释来引用返回元素中的 xml 值.出于测试目的,我的 Mule 配置很简单:
Herefore I'm using Mule. As you can see in my client code I added some XPath annotations to reference the xml values in return-element. For testing purposes my Mule config is simple:
<flow name="test_flow" doc:name="test_flow">
<file:inbound-endpoint path="[mypath]\xml\in" responseTimeout="10000" doc:name="File"/>
<component class="org.ofbiz.service.CreatePerson" doc:name="Java"/>
</flow>
我只是在使用文件入站端点和上面引用我的客户端的 java 组件.在我的客户端的 createPayLoad() 方法中运行 Mule 后,映射"已正确完成.但这不是我想做的.我的问题:对于这个例子,如何使用 AnnotatedEntryPointResolvers 调用整个 java 组件(包括主方法)?是否有如上所述的替代或更好的解决方案?
I'm just using a file inbound endpoint and the java component referencing my client above. After running Mule "mapping" is done correctly within the createPayLoad()-method in my client. But it's not what I want to do. My question: How can I invoke the whole java component (including the main-method) using the AnnotatedEntryPointResolvers for this example? Is there an alternative or better solution as described above?
推荐答案
调用 main()
很奇怪,但是...你可以这样做:
Invoking the main()
is odd but... you can do it with:
<scripting:component>
<scripting:script engine="groovy">CreatePerson.main([] as String[])</scripting:script>
</scripting:component>
这篇关于如何在 Mule 中调用 Java 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!