繁体   English   中英

如何在Mule中调用Java组件

[英]How to invoke a java component in Mule

我正在使用以下SOAP客户端在Apache OFBiz中启动Web服务:

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在上面的客户端中使用firstName和lastName将返回元素中的值获取到“ map”(AnnotatedEntryPointResolver)中:

<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注释来引用return-element中的xml值。 出于测试目的,我的Mule配置很简单:

<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组件。 运行Mule之后,在我的客户端的createPayLoad()方法中正确完成了“映射”。 但这不是我想要的。 我的问题:在此示例中,如何使用AnnotatedEntryPointResolvers调用整个Java组件(包括主方法)? 是否有上述替代或更好的解决方案?

调用main()很奇怪,但是...您可以执行以下操作:

<scripting:component>
    <scripting:script engine="groovy">CreatePerson.main([] as String[])</scripting:script>
</scripting:component>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM