webservice维持session会话(cxf框架)

package com.yh.webservice;

import org.apache.cxf.jaxws.handler.logical.LogicalMessageContextImpl;

import javax.xml.ws.handler.LogicalHandler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.handler.LogicalHandler;
import javax.xml.ws.handler.LogicalMessageContext;
import javax.xml.ws.handler.MessageContext;

import org.apache.cxf.jaxws.handler.logical.LogicalMessageContextImpl;

/**

  • @author wt

  • @version 1.0

  • @date 2020/5/9
    */
    public class SessionLogicalHandler implements
    LogicalHandler {

    public SessionLogicalHandler() {
    super();
    }

    // 静态 所有的 SessionLogicalHandler 共享 一个 cookie
    public static List http_headers_cookie = null;

    @Override
    public boolean handleMessage(LogicalMessageContext context) {
    // TODO Auto-generated method stub
    // javax.xml.ws.handler.MessageContext.Scope s=
    // context.getScope(MessageContext.HTTP_REQUEST_HEADERS);
    // Set-Cookie=[JSESSIONID=48B10A68BB05F69F8ED82A33F566C5D8; Path=/myapp;
    // HttpOnly]

     LogicalMessageContextImpl c = (LogicalMessageContextImpl) context;
     // response 时记录服务端返回的session信息
     if (c.get(MessageContext.HTTP_RESPONSE_HEADERS) != null) {
         Map<String, List> header = (Map<String, List>) c
                 .get(MessageContext.HTTP_RESPONSE_HEADERS);
         List<String> ls = header.get("Set-Cookie");// 获取header 中cookie的信息
         if (ls != null) {
             System.out.println(ls.get(0).split(";")[0]);
             http_headers_cookie = ls;// 保存cookie信息
         }
     } else if (c.get(MessageContext.HTTP_REQUEST_HEADERS) != null
             && http_headers_cookie != null) {// request 请求的时候 把cookie信息设置进去
         Map<String, List> header = (Map<String, List>) c
                 .get(MessageContext.HTTP_REQUEST_HEADERS);
         header.put("cookie", http_headers_cookie);
     } else if (c.get(MessageContext.HTTP_REQUEST_HEADERS) == null
             && http_headers_cookie != null) {// request 请求的时候 把cookie信息设置进去
         Map<String, List> header = new HashMap<String, List>();
         header.put("cookie", http_headers_cookie);
         c.put(MessageContext.HTTP_REQUEST_HEADERS, header);
     }
    
     return true;
    

    }

    @Override
    public boolean handleFault(LogicalMessageContext context) {
    // TODO Auto-generated method stub
    return false;
    }

    @Override
    public void close(MessageContext context) {
    // TODO Auto-generated method stub

    }

}



package com.yh.webservice;

/**
 * @author wt
 * @version 1.0
 * @date 2020/5/9
 */
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import com.hzjd.nbiot.entity.*;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;


public class WsClassLoader {


    // 本地
    private static String WS_HOST = "http://localhost:8080/NBIoTService/services/NBIoTService?wsdl";

    private JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

    private WsClassLoader() {

    }

    private static WsClassLoader instance;

    public static synchronized WsClassLoader getInstance() {
        if (instance == null) {
            instance = new WsClassLoader();
            // 设置 handler
            instance.factory.getHandlers().add(new SessionLogicalHandler());
        }
        return instance;
    }

    private Map<String, Object> wsCache = new HashMap<String, Object>();

    @SuppressWarnings("unchecked")
    public <T> T getWs(String address, Class<T> t) {
        if (wsCache.get(t.getName()) == null) {
            factory.setServiceClass(t);
            String addressFull = WS_HOST + address;
            factory.setAddress(addressFull);

            Object client = factory.create();
            // /开始超时设置
            Client proxy = ClientProxy.getClient(client);
            // proxy.get().add(new LogicalLoggingHandler());
            HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            policy.setConnectionTimeout(60000);
            policy.setReceiveTimeout(0);
            conduit.setClient(policy);
            // /结束超时设置
            wsCache.put(t.getName(), client);
        }
        return (T) wsCache.get(t.getName());
    }

    public static void main(String[] args) {
        NBIoTServicePortType client = (NBIoTServicePortType) WsClassLoader.getInstance()
                .getWs("/NBIoTServicePortType", NBIoTServicePortType.class);

//        login(client);
//        EnableMeter(client);












    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值