如何在Mule中调用java组件

时间:2013-02-08 09:12:27

标签: 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将return-element中的值以上面的客户端中的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>

因此我正在使用骡子。正如您在我的客户端代码中看到的,我添加了一些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后,“mapping”在我的客户端的createPayLoad()方法中正确完成。但这不是我想要做的。我的问题:如何使用AnnotatedEntryPointResolvers为此示例调用整个java组件(包括main方法)?是否有如上所述的替代或更好的解决方案?

1 个答案:

答案 0 :(得分:1)

调用main()很奇怪但是......你可以用:

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