论坛首页 Java企业应用论坛

Mule web service调用中的复杂类型传递

浏览 5921 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-24   最后修改:2008-12-24
SOA
Mule版本: 1.4.4

  官网上很辛苦在一个旮旯里找到如何向 Mule暴露的axis web service 传递复杂数据类型 ,http://www.mulesource.org/pages/viewpage.action?pageId=4678, 官网上只提供了原理,自己补充了一下客户端测试代码,部分服务类做了修改。
   
  类清单:
  Person: 数据类,测试复杂数据类型的传递
  PersonService: 服务接口类
  PersonServiceImpl: 服务实现类。
  PersonServiceLaucher: Mule服务器启动类
  PersonClient: 客户端测试类。
  person-config.xml, Mule 配置,其中将 PersonServiceImpl 暴露为一个axis1.4的服务。

1. Person数据类
  public class Person 
   {
	private String name;
	private String surname;
	private Date registerDate = new Date();
	private int age = 20;
    ...


2.  服务接口类
  public interface PersonService {	
	String printPerson(Person person) ;	
	Person createPerson(String name,String surname,int age);	
 	Person[] batchCreatePerson(String namePattern);
  }


3. 实现类:
package demo.mule.person;

public class PersonServiceImpl implements PersonService {
	public String printPerson(Person person) {		
		return "person:name=" + person.getName() +",surname=" + person.getSurname();
	}
	
	/**
	 * test return a complex type by web service
	 */
	public Person createPerson(String name, String surname,int age) {
		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) {
		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;
	}
}


4. 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>



5. server启动
   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");
	}
}



6. 测试类
 public class PersonClient {	
	private MuleClient muleClient ;	
	public PersonClient() {
		try {
			muleClient = new MuleClient();
		} catch (UMOException e) {		
			e.printStackTrace();
		}
	}

	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[]{"小龙","李", 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.testPrintPerson();
		personClient.testCreatePerson();		
		personClient.testBatchCreatePerson();
	}
}



测试过程:
1) 启动Mule服务容器
2) 执行PersonClient


总结:
  1) Mule暴露POJO为一个服务还是很简单的, 配置非常少
  2) 传递和返回Java复杂数据类型,自定义数据类型都非常方便。不过,客户端需要明确知道每个方法传递的参数个数和类型,以及返回的参数类型,并且需要自己type cast。 在具体项目中,可以自己再做一层封装或用AOP, 以本地调用的方式调用 mule 的web service。
   发表时间:2009-03-05  
好例子啊 正在找这样的例子 网上这样的例子实在是太少了
0 请登录后投票
   发表时间:2009-03-12  
lz有没有Mule版本是2.1.2的啊 公司项目要用
0 请登录后投票
   发表时间:2009-03-15  
portrait 写道
lz有没有Mule版本是2.1.2的啊 公司项目要用



官网上都有的。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics