Mule学习之路_1.MuleClient调用Mule发布的服务

来源:互联网 发布:开票软件主程序打不开 编辑:程序博客网 时间:2024/05/18 05:02

据了解Mule通过Flow发布的服务默认就是通过CXF发布的,首先创建服务和实现类

Service:HelloWorld


package com.easyway.esb.mule;import javax.jws.WebService;@WebServicepublic interface HelloWorld{    String sayHi(String text);}

实现类:HelloWorldImpl

package com.easyway.esb.mule;import javax.jws.WebService;@WebService(endpointInterface = "com.easyway.esb.mule.HelloWorld", serviceName = "HelloWorld")public class HelloWorldImpl implements HelloWorld{    public String sayHi(String text)    {        return "Hello, " + text;    }}
Flow文件 --JAXWS.xml
<?xml version="1.0" encoding="UTF-8"?><mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:rmi="http://www.mulesoft.org/schema/mule/rmi"xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:pattern="http://www.mulesoft.org/schema/mule/pattern"xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf"xmlns:vm="http://www.mulesoft.org/schema/mule/vm"xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/rmi http://www.mulesoft.org/schema/mule/rmi/current/mule-rmi.xsdhttp://www.mulesoft.org/schema/mule/pattern http://www.mulesoft.org/schema/mule/pattern/current/mule-pattern.xsdhttp://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsdhttp://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsdhttp://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsdhttp://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsdhttp://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<flow name="helloService" doc:name="helloService">        <http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:9090/hello" doc:name="HTTP"/>          <cxf:jaxws-service serviceClass="com.easyway.esb.mule.HelloWorldImpl" doc:name="SOAP"/>          <component class="com.easyway.esb.mule.HelloWorldImpl" doc:name="Java"/>    </flow></mule>

客户端代码
import org.mule.api.FutureMessageResult;import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.MuleMessage;import org.mule.api.config.ConfigurationBuilder;import org.mule.api.config.ConfigurationException;import org.mule.api.context.MuleContextBuilder;import org.mule.api.context.MuleContextFactory;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextBuilder;import org.mule.context.DefaultMuleContextFactory;import org.mule.module.client.MuleClient;public class MuleClientMain {    public static void main(String[] args) throws Exception{       // Create a MuleContextFactory      MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();             //create the configuration builder and optionally pass in one or more of these      ConfigurationBuilder builder =           new SpringXmlConfigurationBuilder("JAXWS.xml");      //The actual context builder to use      MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();             //Create the context      MuleContext context = muleContextFactory.createMuleContext(builder, contextBuilder);             //Start the context      context.start();             MuleClient client = new MuleClient(context);            String url = "cxf:http://localhost:9090/hello?method=sayHi";      MuleMessage message = client.send(url, "eagle", null);        String s = message.getPayloadAsString();      System.out.println(s);                }          }  
启动项目ruan as Mule Application
可以通过http://localhost:9090/hello可以看到SOAP信息
在输入方法名,参数,就可以得到返回值
http://localhost:9090/hello/sayHi/arg0/WuDi --syaHi为方法名,arg0为第一个参数,因为这个方法只有一个参数
如果有多个参数可以通过这个方式传 sayHi/arg0/bbb/arg1/aaa/arg3/...
WuDi为实参,得到
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="http://mule.esb.easyway.com/"><return>Hello, WuDi</return></ns2:sayHiResponse></soap:Body></soap:Envelope>

到此,实现了HelloWorld例子