Mule 例子:以axis做为inbound,传递复杂类型,MuleClient UMOMessage

这个例子出自:http://raymondhekk.iteye.com/blog/300604

 

重点演示了:

1、用 mule 的 inbound 来发布 axis 的 web services。

2、用 mule 的 MuleClient,UMOMessage 来调用 web services。

3、调用 web services 时候,传递复杂类型。

 

类清单:
  Person: 数据类,测试复杂数据类型的传递
  PersonService: 服务接口类
  PersonServiceImpl: 服务实现类。
  PersonServiceLaucher: Mule 服务器启动类
  PersonClient: 客户端测试类。
  person-config.xml, Mule 配置,其中将 PersonServiceImpl 暴露为一个axis1.4的服务。

 

Person.java

import java.util.Date;

public class Person implements java.io.Serializable {
    private String name;
    private String surname;
    private Date registerDate = new Date();
    private int age = 20;   
    public Person() {
        this.name = "defaultName";
        this.surname = "defaultSurname";
        this.registerDate = new Date();
        this.age = 39;
    }   
    public Person(String name, String surname) {
        this.name = name;
        this.surname = surname;
        this.registerDate = new Date();
        this.age = 39;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public Date getRegisterDate() {
        return registerDate;
    }
    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

 

PersonService.java

public interface PersonService {   
    int countPerson(int i);   
    String printPerson(Person person);
    Person createPerson(String name, String surname, int age);
    Person[] batchCreatePerson(String namePattern);
}

PersonServiceImpl.java

public class PersonServiceImpl implements PersonService {
   
    public int countPerson(int i) {
        System.out.println("Mule Axis Function countPerson is Called!!");
        return 31;
    }
   
    public String printPerson(Person person) {
        System.out.println("Mule Axis Function printPerson is Called!!");
        return "person:name=" + person.getName() + ",surname="
                + person.getSurname();
    }

    public Person createPerson(String name, String surname, int age) {
        System.out.println("Mule Axis Function createPerson is Called!!");
        Person p = new Person(name, surname);
        p.setAge(age);
        System.out.println("a person is created:" + p.getName());
        return p;
    }

    public Person[] batchCreatePerson(String namePattern) {
        System.out.println("Mule Axis Function batchCreatePerson is Called!!");
        Person[] persons = new Person[3];
        for (int i = 0; i < persons.length; i++) {
            Person p = new Person(namePattern + i, namePattern + i);
            p.setAge(20 + i);
            persons[i] = p;
        }
        return persons;
    }
}

 

PersonServiceLaucher.java

import org.mule.config.ConfigurationException;
import org.mule.config.builders.MuleXmlConfigurationBuilder;
import org.mule.umo.manager.UMOManager;

 public class PersonServiceLaucher { 
     public static void main(String[] args) throws ConfigurationException { 
         MuleXmlConfigurationBuilder muleXmlConfigurationBuilder =
                     new MuleXmlConfigurationBuilder(); 
         UMOManager manager = muleXmlConfigurationBuilder
                     .configure("demo/mule/person/person-config.xml"); 
         System.out.println("Mule Server has started!");
     }
 }

 

PersonClient.java

import org.mule.extras.client.MuleClient;
import org.mule.impl.MuleMessage;
import org.mule.umo.UMOException;
import org.mule.umo.UMOMessage;

public class PersonClient {
    private MuleClient muleClient;

    public PersonClient() {
        try {
            muleClient = new MuleClient();
        } catch (UMOException e) {
            e.printStackTrace();
        }
    }
   
    public void testCountPerson() throws Exception {
        Object[] args = new Object[] { 1 };
        UMOMessage msg = new MuleMessage(args);
        UMOMessage message = muleClient
                .send(
                        "axis:http://localhost:8082/services/personService?method=countPerson", msg);

        System.out.println("message:" + message.getPayloadAsString());
    }

    public void testPrintPerson() throws Exception {

        Person person = new Person("David", "Jones");
        UMOMessage msg = new MuleMessage(person);
        UMOMessage message = muleClient
                .send(
                        "axis:http://localhost:8082/services/personService?method=printPerson",
                        msg);

        System.out.println("message:" + message.getPayloadAsString());
    }

    public void testCreatePerson() throws Exception {
        Object[] args = new Object[] { "xiaolong", "li", new Integer(23) };
        UMOMessage msg = new MuleMessage(args);
        UMOMessage message = muleClient
                .send(
                        "axis:http://localhost:8082/services/personService?method=createPerson",
                        msg);

        Person p = (Person) message.getPayload();
        System.out.println("message:" + message.getPayloadAsString());
        System.out.println("payload:" + p.getName() + "," + p.getSurname()
                + "," + p.getAge());
    }

    public void testBatchCreatePerson() throws UMOException {
        Object[] args = new Object[] { "Admin" };
        UMOMessage msg = new MuleMessage(args);
        UMOMessage message = muleClient
                .send(
                        "axis:http://localhost:8082/services/personService?method=batchCreatePerson",
                        msg);

        Person[] ps = (Person[]) message.getPayload();
        System.out.println("persons:" + ps.length);
        for (int i = 0; i < ps.length; i++) {
            Person p = ps[i];
            System.out.println("person:" + p.getName() + "," + p.getSurname()
                    + "," + p.getAge());
        }
    }

    public static void main(String[] args) throws Exception {
        PersonClient personClient = new PersonClient();
        //personClient.testCountPerson();
        personClient.testPrintPerson();
        //personClient.testCreatePerson();
        //personClient.testBatchCreatePerson();
    }
}

 

person-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mule-configuration PUBLIC "-//MuleSource //DTD mule-configuration XML V1.0//EN" "http://mule.mulesource.org/dtds/mule-configuration.dtd">

<mule-configuration id="mule-test" version="1.0">


    <model name="personService">
        <!-- test: http://localhost:8082/services/personService?method=createPerson -->
        <mule-descriptor name="personService"
            implementation="demo.mule.person.PersonServiceImpl">
            <inbound-router>
                <endpoint
                    address="axis:http://localhost:8082/services">
                </endpoint>
            </inbound-router>
           
                <!-- not nessesary
                <properties> 
                <property name="style" value="wrapped"/> 
                <property name="use" value="literal"/> 
                </properties>  -->
           
        </mule-descriptor>
    </model>
</mule-configuration>

 

 

测试过程:


1) 启动Mule 服务容器。访问 http://localhost:8082/services/personService?wsdl ,确定 web services 发布成功。
2) 执行PersonClient

 

 

 

 

 

你可能感兴趣的:(Web,xml,Blog)