`

Web Service Context示例

 
阅读更多

Web service context指Endpoint之间传输的消息。Context内容以键值对格式(Map)进行存储,包括incoming和outgoing

Message.

 

CXF提供了访问上下文数据的对象:javax.xml.ws.handler.MessageContext,而MessageContext与Message所在的

Scope有关:

Application Scope: 定义在该Scope中的 MessageContext可被Service Provider, Service Consumer, Handler实现访问。

任何Message上下文Property在Service Provider或Service Consumer中设置的默认Scope是Application.

Handle Scope:只能被Handler实现访问.任何Message上下文由Handler实现的,则默认Scope是Handler.

 

可通过MessageContext对象的setScope方法改变MessageContext的Scope.

 

完整代码参考http://springsfeng.iteye.com/blog/1634753附件。

 

实例:

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.apache.cxf.message.Message;

@WebService(serviceName = "OrderProcessService", portName = "OrderProcessPort")
public class OrderProcessImpl implements OrderProcess {

	@Resource
	WebServiceContext wsc;

	public String processOrder(Order order) {
		System.out.println("Processing order...");
		String orderID = validate(order);
		return orderID;
	}

	/**
	 * Validates the order and returns the order ID
	 **/
	private String validate(Order order) {
		System.out.println("Getting the operation info from the message context ");
		MessageContext ctx = wsc.getMessageContext();
		QName operation = (QName) ctx.get(Message.WSDL_OPERATION);

		System.out.println("The operation name is " + operation);

		String custID = order.getCustomerID();
		String itemID = order.getItemID();
		int qty = order.getQty();
		double price = order.getPrice();

		if (custID != null && itemID != null && !custID.equals("") && !itemID.equals("") && qty > 0 && price > 0.0) {
			return "ORD1234";
		}
		return null;
	}

}

     发布服务:

import javax.xml.ws.Endpoint;

public class Server {

    protected Server() throws Exception {
        System.out.println("Starting Server");
        OrderProcessImpl orderProcessImpl = new OrderProcessImpl();
        String address = "http://localhost:8080/OrderProcess";
        Endpoint.publish(address, orderProcessImpl);
    }

    public static void main(String args[]) throws Exception {
        new Server();
        System.out.println("Server ready...");

        Thread.sleep(50 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}

     Client:

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.pcdp.sample.context.Order;
import org.pcdp.sample.context.OrderProcess;

public class Client {

	private static final QName SERVICE_NAME = new QName("http://context.sample.pcdp.org/", "OrderProcessService");
	private static final QName PORT_NAME = new QName("http://context.sample.pcdp.org/", "OrderProcessPort");

	private static final String WSDL_LOCATION = "http://localhost:8080/OrderProcess?wsdl";

	public static void main(String args[]) throws Exception {
		URL wsdlURL = new URL(WSDL_LOCATION);
		Service service = Service.create(wsdlURL, SERVICE_NAME);
		OrderProcess port = service.getPort(PORT_NAME, OrderProcess.class);

		Order order = new Order();
		order.setCustomerID("C001");
		order.setItemID("I001");
		order.setPrice(100.00);
		order.setQty(20);

		String result = port.processOrder(order);
		System.out.println("The order ID is " + result);

	}

}

     Client输出:

2012-8-17 11:28:19 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://context.sample.pcdp.org/}OrderProcessService from WSDL: http://localhost:8080/OrderProcess?wsdl
The order ID is ORD1234 

     Server 输出:

Processing order...
Getting the operation info from the message context 
The operation name is {http://context.sample.pcdp.org/}processOrder
分享到:
评论

相关推荐

    CXF WebService整合Spring示例工程代码demo

    CXF WebService整合Spring示例工程代码demo可以直接导入eclipse。参照网页http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 完成的webService服务提供。 大致步骤: 1.引入cxf和其他需要的jar包,(本...

    spring加载restful(文档+程序源码)

     创建一个示例RESTful Web Service  本节中的例子将演示Spring 3环境的创建过程,并创建一个可以部署到Tomcat中的“Hello World”应用。然后我们再完成一个更复杂的应用来了解Spring 3 REST支持的重要概念,如...

    asp.net技术内幕(1)

    15.2 使用应用程序状态 15.2.1 理解应用程序状态和同步 15.2.2 使用Global.asax文件 15.2.3 理解Context和使用Global.asax文件 15.2.4 处理应用程序Start和Init事件 15.2.5 处理Application...

    asp.net技术内幕(2)

    15.2 使用应用程序状态 15.2.1 理解应用程序状态和同步 15.2.2 使用Global.asax文件 15.2.3 理解Context和使用Global.asax文件 15.2.4 处理应用程序Start和Init事件 15.2.5 处理Application...

    asp.net技术内幕(5)

    15.2 使用应用程序状态 15.2.1 理解应用程序状态和同步 15.2.2 使用Global.asax文件 15.2.3 理解Context和使用Global.asax文件 15.2.4 处理应用程序Start和Init事件 15.2.5 处理Application...

    asp.net技术内幕(4)

    15.2 使用应用程序状态 15.2.1 理解应用程序状态和同步 15.2.2 使用Global.asax文件 15.2.3 理解Context和使用Global.asax文件 15.2.4 处理应用程序Start和Init事件 15.2.5 处理Application...

    asp.net技术内幕(3)

    15.2 使用应用程序状态 15.2.1 理解应用程序状态和同步 15.2.2 使用Global.asax文件 15.2.3 理解Context和使用Global.asax文件 15.2.4 处理应用程序Start和Init事件 15.2.5 处理Application...

    ASP.net技术内幕

    本书是关于使用ASP.NET建立网站的完整参考书,书中包含数百个代码示例,读者可以使用这些示例开始建立自己的网站 前言 <br/>第一部分 使用ASP.NET Web表单 <br/>第1章 建立ASP.NET页面 <br/>1.1 ...

    Spring中文帮助文档

    8.3.8. PetClinic示例 8.4. 更多资源 II. 中间层数据访问 9. 事务管理 9.1. 简介 9.2. 动机 9.3. 关键抽象 9.4. 使用资源同步的事务 9.4.1. 高层次方案 9.4.2. 低层次方案 9.4.3. ...

    Spring API

    8.3.8. PetClinic示例 8.4. 更多资源 II. 中间层数据访问 9. 事务管理 9.1. 简介 9.2. 动机 9.3. 关键抽象 9.4. 使用资源同步的事务 9.4.1. 高层次方案 9.4.2. 低层次方案 9.4.3. ...

    How Tomcat Works: A Guide to Developing Your Own Java Servlet Container

    第1章 一个简单的Web服务器 3 1.1 The Hypertext Transfer Protocol (HTTP) 3 1.2 HTTP Request 3 1.3 HTTP Response 4 1.4 Socket类 4 1.5 ServerSocket类 5 1.6 应用举例 5 第2章 一个简单的servlet容器 7 2.1 ...

    how-tomcat-works

    第1章 一个简单的Web服务器 3 1.1 The Hypertext Transfer Protocol (HTTP) 3 1.2 HTTP Request 3 1.3 HTTP Response 4 1.4 Socket类 4 1.5 ServerSocket类 5 1.6 应用举例 5 第2章 一个简单的servlet容器 7 2.1 ...

    annosaurus:用于存储和检索视频图像注释的服务

    用于创建图像和视频注释的Web服务。 我们正在研究API文档; 。 您可以在上以它们的进行中状态查看它们 这是一个如何使用Docker启动它的示例: docker run -d \ -p 8100:8080 \ -e AUTHENTICATION_SERVICE= " ...

    节点错误对象Fumble.zip

    示例代码:var fumble = require('fumble'); var callAndProcess = require('./callAndProcess'); module.exports = require('api').base.service({  name: 'foo',   read: function (req, resource,...

    J2EE中文版指南 CHM格式 带全文检索

    访问Web Context 168 维持客户端状态 169 访问一个会话 169 将属性与会话绑定 169 与会话相关联的通知对象 170 会话管理 170 会话跟踪 170 结束servlet 171 跟踪服务请求 171 通知方法Shut Down 172 创建长时间运行...

    Android典型技术模块开发详解

    13.4.1 CXF框架的Web Service 13.4.2 Android端开发 13.5 Base64编解码 13.6 Http方式的SOAP调用 13.7 本章小结 第14章 AChartEngine 14.1 API介绍 14.1.1 渲染器 14.1.2 数据填充器 14.1.3 图表View 14.2 折线图 ...

    ZendFramework中文文档

    ContextSwitch and AjaxContext 7.8.4.3.1. 缺省可用的上下文 7.8.4.3.2. 创建定制的上下文 7.8.4.3.3. 为每个动作设置上下文 7.8.4.3.4. 初始化上下文开关 7.8.4.3.5. 另外的功能 7.8.4.3.6. AjaxContext ...

    ASP3《高级编程》(第一部分)

    12.2.1 Indexing Service 376 12.2.2 活动目录 383 12.2.3 Exchange Server 386 12.2.4 定制的提供者 386 12.3 半结构化的数据 389 12.3.1 Record对象 390 12.3.2 Internet 发布 390 12.3.3 WebDAV 401 12.4...

    ASP3《高级编程》(第二部分)

    12.2.1 Indexing Service 376 12.2.2 活动目录 383 12.2.3 Exchange Server 386 12.2.4 定制的提供者 386 12.3 半结构化的数据 389 12.3.1 Record对象 390 12.3.2 Internet 发布 390 12.3.3 WebDAV 401 12.4...

    cms后台管理

    /WEB-INF/config/application-context.xml /WEB-INF/config/cache-context.xml /WEB-INF/config/captcha-context.xml /WEB-INF/config/jeecore-context.xml /WEB-INF/config/jeecms-context.xml </param-value>...

Global site tag (gtag.js) - Google Analytics