⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agentcrbtwshandler.java

📁 同步接收web services请求
💻 JAVA
字号:
/** 
 * AgentCrbtWSHandler.java
 * @版权: Copyright (C) 2007
 * @公司:北京汉铭信通科技有限公司
 * @url: www.aceway.com.cn
 */

package com.aceway.vas.xjcrgw.ws.handler;

import java.util.HashMap;

import com.aceway.vas.commons.tcp.TcpClient;
import com.aceway.vas.commons.tcp.Util;
import com.aceway.vas.commons.util.logger.AcewayLogger;
import com.aceway.vas.sjcraw.cbgp201.Msg;
import com.aceway.vas.sjcraw.cbgp201.MsgHead;
import com.aceway.vas.xjcrgw.ws.factory.AgentCrbtWSRespFactory;

/**
 * 此类是 CRBT_AGENT 模块的WEB SERVICES请求处理类,业务操作请求应答消息处理类
 * 负责同步并发的WEB SERVICES请求,调用底层NIO通信包发送消息
 * 接收服务端的业务操作请求应答消息,响应WEB SERVICES应答
 * 
 * @author zhou tao
 */
public class AgentCrbtWSHandler extends MsgReceiveAdapter implements
		AgentWSHandler {

	private AcewayLogger acewayLogger = (AcewayLogger) AcewayLogger.getLogger("CrbtAgentLogger");

	/**
	 * 唯一实例
	 */
	private static AgentCrbtWSHandler agentCrbtWSHandler = new AgentCrbtWSHandler();

	/**
	 * 交易标识,用于唯一标识一次完整的业务。 该域为字符形式表示的十进制数字,由消息发起者设定,应答者对应给回此序列号。 每发起一次业务,该序列号加1。
	 */
	private static long linkId;

	/**
	 * 字符形式表示的十进制数字,表示一个序列号,由消息发起者设定,应答者对应给回此序列号。 每发起一次请求,该序列号加1。
	 * 序列号范围:0000000000-9999999999,循环使用。
	 */
	private static long seqNo;

	/**
	 * seqNo标识一次webservice请求,map存放这些等待响应的请求 key:seqNo value:obj
	 */
	private static HashMap waitRespMap = new HashMap();	

	/**
	 * 消息是否加密
	 */
	private boolean isMsgNeedEncrypt;

	private AgentCrbtWSHandler() {
		
	}

	public static AgentCrbtWSHandler getInstance() {
		return agentCrbtWSHandler;
	}

	public synchronized long genSeqNo() {
		return seqNo < 9999999999L ? seqNo++ : 1;
	}

	public synchronized long genLinkId() {
		return linkId++;
	}

	public int sendMsg(Msg msg) {
		return 0;
	}

	/**
	 * 发送CBGP消息包,阻塞web services请求
	 * 
	 * @param msg
	 *            字节数组形式的CBGP消息
	 * @param seqNo
	 *            唯一标识一次web services请求
	 * @return 发送是否成功
	 */
	public int sendMsg(byte msg[], String seqNo) {
		// 得到TCP客户端实例
		TcpClient tcpClient = Util.getClient();
		if (isMsgNeedEncrypt) {// 对消息加密
			// TODO:
		}
		int result = tcpClient.send(msg);
		if (result == 0) {// 如果发送成功

			RespObj obj = new RespObj();
			obj.setSeqNo(seqNo);
			synchronized (waitRespMap) {
				waitRespMap.put(seqNo, obj);
			}

			synchronized (obj) {
				try {
					obj.wait();// 发送完毕,等待请求的响应
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * 请求响应消息处理
	 * 
	 * @param msg
	 *            字节数组形式的CBGP消息
	 * @param seqNo
	 *            唯一标识一次web services请求
	 * @return 发送是否成功
	 */
	public void receiveOperRespMsg(byte[] bytes) {
		MsgHead msgHead = new MsgHead(bytes);
		String seqNo = msgHead.getSeqNo();
		RespObj obj = null;
		synchronized (waitRespMap) {
			// 得到服务端响应结果数据,放入webservices请求线程等待的响应结果对象中
			obj = (RespObj) waitRespMap.get(seqNo);
		}
		if (obj != null) {
			obj.setB(bytes);
			synchronized (obj) {
				obj.notify();// 通知相应webservices请求线程取结果
			}
		}
	}

	/**
	 * 被唤醒的webservices请求线程凭seqno过来取结果 取出结果并删除
	 * 
	 * @param seqNo
	 *            唯一标识一次请求
	 * @return object Response响应对象
	 */
	public Object getWSResponse(String seqNo) {
		RespObj respObj = null;
		Object result = null;
		synchronized (waitRespMap) {
			respObj = (RespObj) waitRespMap.remove(seqNo);
		}
		if (respObj != null && respObj.getSeqNo().equals(seqNo)) {
			byte[] b = respObj.getB();
			result = AgentCrbtWSRespFactory.createAgentCrbtWSResp(b);
		}
		return result;
	}

	/**
	 * 响应对象,每个实例对应一个web wervices请求线程
	 */
	private class RespObj {
		String seqNo;

		byte[] b;

		public byte[] getB() {
			return b;
		}

		public void setB(byte[] b) {
			this.b = b;
		}

		public String getSeqNo() {
			return seqNo;
		}

		public void setSeqNo(String seqNo) {
			this.seqNo = seqNo;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -