Mule 的第二个例子

这个例子 主要 演示了 web service 作为 inbound 的情况。

 

1、首先要创建web service。一个接口 一个类 如下:

 

MuleService.java

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface MuleService {
    public String testMule(@WebParam(name = "str")
    String str);
}

 

MuleServiceImp.java

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(serviceName = "eagleMuleService", endpointInterface = "expl.MuleService")
public class MuleServiceImp implements MuleService {

    public String testMule(String str) {
        System.out.println("----service---");
        return "hello--" + str;
    }
}

 

2、写 mule 的配置文件

 

mule-config.xml

<?xml version="1.0" encoding="UTF-8"?> 
  <mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:spring="http://www.springframework.org/schema/beans" 
        xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" 
        xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.2" 
        xmlns:axis="http://www.mulesource.org/schema/mule/axis/2.2" 
        xmlns:smtps="http://www.mulesource.org/schema/mule/smtps/2.2" 
        xmlns:http="http://www.mulesource.org/schema/mule/http/2.2" 
        xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" 
        xmlns:soap="http://www.mulesource.org/schema/mule/soap/2.2" 
        xsi:schemaLocation=" 
                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
                 http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd 
                 http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd 
                 http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd 
                 http://www.mulesource.org/schema/mule/cxf/2.2 http://www.mulesource.org/schema/mule/cxf/2.2/mule-cxf.xsd 
                 http://www.mulesource.org/schema/mule/axis/2.2 http://www.mulesource.org/schema/mule/axis/2.2/mule-axis.xsd 
                 http://www.mulesource.org/schema/mule/smtps/2.2 http://www.mulesource.org/schema/mule/smtps/2.2/mule-smtps.xsd 
                 http://www.mulesource.org/schema/mule/soap/2.2 http://www.mulesource.org/schema/mule/soap/2.2/mule-soap.xsd 
                 http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd 
                 "> 
       <description> 
          eagleMule demo which shows how to publish web services over CXF. 
      </description> 
      <model name="eagleMule"> 
           <service name="testMuleService"> 
              <inbound> 
                  <axis:inbound-endpoint address="http://localhost:8899/services/testMuleService"> 
                      <soap:http-to-soap-request-transformer /> 
                  </axis:inbound-endpoint> 
                  <cxf:inbound-endpoint address="http://localhost:8898/services/testMuleService"> 
                      <soap:http-to-soap-request-transformer /> 
                  </cxf:inbound-endpoint> 
              </inbound> 
              <component class="expl.MuleServiceImp">
              </component> 
          </service> 
      </model>
</mule>

 

PS: 这里的axis 的web service 没有发布成功。cxf 的成功。具体原因没有深入研究。

      发布web service 是否成功 就是访问 {url}?wsdl, 看能否访问到wsdl文件。

 

3、启动这个mule

 

EagleMuleMain.java

import org.mule.api.context.MuleContextFactory;
import org.mule.api.context.WorkManager;
import org.apache.cxf.configuration.ConfigurationException;
import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextFactory;

public class EagleMuleMain {
   
    public static void main(String[] args) throws ConfigurationException, InitialisationException {
        try {
            String configFile = "expl/mule_config.xml";
            String[] configFileArr = new String[] { configFile };
            MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
            MuleContext context = muleContextFactory.createMuleContext(
                    new SpringXmlConfigurationBuilder(configFileArr));
            context.start();
        } catch (MuleException t) {
            t.printStackTrace();
        }
    }
}

 

PS: 这里需要一堆零散的jar包,一个一个找全的

 

4、写客户端调用cxf发布的web service

 

(1) 这种方法成功

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class MyClient {

    public static void main(String[] args) {
       
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
       
        factory.setAddress("http://localhost:8898/services/testMuleService");
       
        factory.setServiceClass(MuleService.class);
       
        MuleService helloworld = (MuleService) factory.create();
       
        System.out.println(helloworld.testMule("CY"));
    }

}

 

(2)这种方法没有成功,说找不到方法

import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.module.client.MuleClient;

public class Client {
    public static void main(String[] args) {
        MuleClient client = null;
        try {
            client = new MuleClient();
            String url = "cxf:http://localhost:8898/services/testMuleService?wsdl&amp;method=testMule";

            MuleMessage message = client.send(url, "eagle", null);
            Object obj = message.getPayload();
            System.out.println("--------------obj---------"
                    + obj.getClass().getName());
            if (obj instanceof String) {
                System.out.println("---------str--------------" + obj);
            }
        } catch (MuleException e) {
            e.printStackTrace();
        } finally {
            client.dispose();
        }
    }
}

 

PS: mule 的东西研究了一段时间了,还是云山雾绕的。我的策略就是 一个例子一个例子的积累,慢慢会明白的。

你可能感兴趣的:(apache,spring,Web,webservice,SOAP)