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

你可能感兴趣的:(web Service)