`
liugang594
  • 浏览: 978201 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[CXF] Server与Client实现方式六:Local

阅读更多

【参考:http://cxf.apache.org/docs/local-transport.html 】

除了remote的交互方式,cxf还提供了一种local的交互方式,它允许在同一个JVM内进行service的调用。

 

一、服务接口的定义

和之前几篇文章一样,定义很简单:

@WebService
public interface OrderProcess {
	public String processOrder(Order order);
}

 

二、服务端的实现

除了address使用的protocol是local以外,和http的方式几乎没有区别:

Endpoint.publish("local://orderProcess", new OrderProcessImpl());

 

或者是Spring的方式:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
	   http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<jaxws:endpoint id="orderProcess"
		implementorClass="com.liulutu.liugang.local.OrderProcess"
		implementor="com.liulutu.liugang.local.OrderProcessImpl" address="local://orderProcess" />
</beans>

 

三、Client端实现

		ClientProxyFactoryBean clientBean = new ClientProxyFactoryBean();
		clientBean.setAddress("local://orderProcess");
		clientBean.setServiceClass(OrderProcess.class);
		OrderProcess orderProcess = clientBean.create(OrderProcess.class);
		Order order = new Order();
		order.setId("hello");
		System.out.println(orderProcess.processOrder(order));

 

或者是Spring的方式:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
	   http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<jaxws:client id="orderProcessClient"
		serviceClass="com.liulutu.liugang.local.OrderProcess" address="local://orderProcess" />
</beans>

然后在java代码里:

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"/com/liulutu/liugang/local/client.xml");
		
		OrderProcess orderProcess = context.getBean("orderProcessClient", OrderProcess.class);
		Order order = new Order();
		order.setId("hello");
		String processOrder = orderProcess.processOrder(order);
		System.out.println(processOrder); 

四、同一个JVM的问题

注意:使用local协议,我们不能像远程调用那种分别启动Server端和Client端,然后双方通信,server和Client必须在同一个JVM实例下,并且我发现,它们之间甚至不能分别创建org.apache.cxf.transport.local.LocalTransportFactory实例。因此可能的方式如下:

 

Java的方式 -- 合并Server和Client两段java代码:

		//start server
		Endpoint.publish("local://orderProcess", new OrderProcessImpl());
		
		//start client
		ClientProxyFactoryBean clientBean = new ClientProxyFactoryBean();
		clientBean.setAddress("local://orderProcess");
		clientBean.setServiceClass(OrderProcess.class);
		OrderProcess orderProcess = clientBean.create(OrderProcess.class);
		Order order = new Order();
		order.setId("hello");
		System.out.println(orderProcess.processOrder(order));

 

Spring方式一 -- 使用一个ApplicationContext同时加载Server和Client的Spring配置:

 

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
		"/com/liulutu/liugang/local/client.xml", "/com/liulutu/liugang/local/server.xml");
		
		OrderProcess orderProcess = context.getBean("orderProcessClient", OrderProcess.class);
		Order order = new Order();
		order.setId("hello");
		String processOrder = orderProcess.processOrder(order);
		System.out.println(processOrder);

 

Spring方式二 -- 合并Server和Client的Spring配置内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
	   http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<jaxws:client id="orderProcessClient"
		serviceClass="com.liulutu.liugang.local.OrderProcess" address="local://orderProcess" />

	<jaxws:endpoint id="orderProcess"
		implementorClass="com.liulutu.liugang.local.OrderProcess"
		implementor="com.liulutu.liugang.local.OrderProcessImpl" address="local://orderProcess" />
</beans>

 然后在代码里:

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"/com/liulutu/liugang/local/client.xml");
		
		OrderProcess orderProcess = context.getBean("orderProcessClient", OrderProcess.class);
		Order order = new Order();
		order.setId("hello");
		String processOrder = orderProcess.processOrder(order);
		System.out.println(processOrder);

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics