[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);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值