加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 23:59:39 所属栏目:安全 来源:网络整理
导读:据了解Mule通过Flow发布的服务默认就是通过CXF发布的,首先创建服务和实现类 Service:HelloWorld package com.easyway.esb.mule;import javax.jws.WebService;@WebServicepublic interface HelloWorld{ String sayHi(String text);} 实现类:HelloWorldImpl

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

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例子

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读