天天看點

Mule學習之路_1.MuleClient調用Mule釋出的服務

Service:HelloWorld

package com.easyway.esb.mule;

import javax.jws.WebService;

@WebService
public 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.xsd
http://www.mulesoft.org/schema/mule/pattern http://www.mulesoft.org/schema/mule/pattern/current/mule-pattern.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://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例子

繼續閱讀