jacob 用法

(1)我从官网下载到的 压缩包是


jacob-1.18-M2.zip ,地址http://sourceforge.net/projects/jacob-project/ 

(2)将 jacob.dll  放入 windows-》  system32 目录下,同时再放一份到类路径下 比如 maven项目 放到 src/main/resources 下 。然后将jacob.jar 添加到 classpath下。

(3)现在已经可以用jacob api调用dll 组件 了,贴上一份测试过的代码。

package com.gushiyingxiong.stockagent.utils;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jawin.COMException;
import org.jawin.DispatchPtr;

import com.gushiyingxiong.stockagent.GlobalVar;
import com.gushiyingxiong.stockagent.exception.ConnectionInvidException;
import com.gushiyingxiong.stockagent.exception.ReadException;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Variant;

public final class JacobHsCommXClient implements IHsCommXClient {

	private static final Logger LOGGER = Logger
			.getLogger(JacobHsCommXClient.class);
	private static AtomicLong _ID=new AtomicLong(0);
	private long id;
	
	private ActiveXComponent client;
	private AtomicInteger senderId = new AtomicInteger(0);

	public JacobHsCommXClient() {
		try {
			id=_ID.incrementAndGet();
			client = new ActiveXComponent("HsCommX.Comm");
			client.invoke("create");
//			client.put("ReceiveTimeOut", 20);
//			int con = (int) client.invoke("Connect");
			int con = connect();
			if (con != 0) {
//				String errorMsg = ;
				LOGGER.error("证券客户端[id="+id+"]连接失败,异常信息:"+(String) client.getPropertyAsString("ErrorMsg"));
				// System.exit(-1);
			}else
				LOGGER.info("证券客户端[id="+id+"] 连接成功");
			// System.out.println(con);
		} catch (Exception e) {
			LOGGER.error("证券客户端[id="+id+"] 连接失败" + "异常信息:" + e.getMessage());
			// System.exit(-1);
		}
	}

	@Override
	public JSONObject callX(int iFunc, Map<String, Object> params)
			throws Exception {
		return callX(1, iFunc, params);
	}

	public int connect() {
		Variant con = new Variant(-1);
		try {
			// 经过测试 解密密钥 长度 应=16
			// 再说明 如果 不加密 keyLen 必须传0
			int keyciper = GlobalVar.STOCK_KEYCIPER;
			String key = GlobalVar.STOCK_KEY;
			int keyLen = key.getBytes("UTF-8").length;
			if (keyciper == 0)
				keyLen = 0;
			con =  client.invoke("Connectx", new Variant(GlobalVar.STOCK_PROTOCOL),new Variant(GlobalVar.STOCK_SERVER_IP),new Variant(GlobalVar.STOCK_SERVER_PORT),new Variant(keyciper),new Variant(key),new Variant(keyLen));
			
		} catch (Exception e) {
			LOGGER.error("东莞证券 service 连接失败" + "异常信息:" + e.getMessage());
		}
		return con.getInt();
	}

	/**
	 * 
	 * 
	 * @param FiBranchNo
	 *            开户营业部号
	 * @param iFunc
	 *            功能号
	 * @param params
	 * @return
	 */

	public JSONObject callX(int FiBranchNo, int iFunc,
			Map<String, Object> params) throws Exception {
		JSONObject result = new JSONObject();
		JSONArray arr = null;

		client.invoke("SetHead", FiBranchNo, iFunc);
		client.invoke("SetRange", params.size(), 1);
		if (params != null || params.size() != 0) {
			for (String key : params.keySet()) {
				client.invoke("AddField", key);
			}
			for (Object v : params.values()) {
				client.invoke("AddValue", new Variant(v));
			}
		}
		int _senderId = senderId.incrementAndGet();
		client.setProperty("SenderId", _senderId);
		Variant issend = client.invoke("Send");
		if (issend.getInt() == 0) {
			client.invoke("freepack");
			int isrecive =  client.invoke("Receive").getInt();
			if (isrecive == 0) {
				int ret_Id = (int) client.getPropertyAsInt("SenderId");
				int errorNo = (int) client.getPropertyAsInt("ErrorNo");

				if (ret_Id != _senderId) {
					LOGGER.warn((String) client.getPropertyAsString("ErrorMsg"));
					result.put("error_no", -1);
					result.put("error_info", "东莞证券api 请求失败");
					return result;
				}
				
				if (params.containsKey("request_num")
						&& params.containsKey("position_str")) {
					arr = new JSONArray();
					int eof = 1;
					while ((eof = (int) client.getPropertyAsInt("Eof")) == 0) {
						JSONObject temp = new JSONObject();
						int filedCount = (int) client.getPropertyAsInt("Fieldcount");

						// int filedCount=2;
						for (int i = 0; i < filedCount; i++) {
							String key =  client.invoke("GetFieldName",
									i).getString();
							temp.put(key, client.invoke("FieldByName", key));
						}
						arr.add(temp);
						client.invoke("MoveBy", 1);
					}
					String _er_no=null;
					if(arr.size()==1)
					  _er_no = (String) arr.getJSONObject(0).get(
							"error_no");
					if (_er_no != null
							&& !"0".equals(_er_no)) {
						result = arr.getJSONObject(0);
					} else {
						result.put("list", arr);
					}
				} else {
					int filedCount = (int) client.getPropertyAsInt("Fieldcount");
					LOGGER.info("filedCount"+filedCount);
					for (int i = 0; i < filedCount; i++) {
						LOGGER.info("i="+i);
						String key = client.invoke("GetFieldName", i).getString();
						String value=client.invoke("FieldByName", key).getString();
						LOGGER.info(key+"="+value);
						result.put(key, value);
					}
					result.put("entrust_no",  client.invoke("FieldByName", "entrust_no").getString());
				}

			} else {
				 int errorNo = (int) client.getPropertyAsInt("ErrorNo");
				String errorMsg = (String) client.getPropertyAsString("ErrorMsg");
				LOGGER.info(errorMsg);
				throw new ReadException(errorMsg);
			}
		} else {
			 int errorNo = (int) client.getPropertyAsInt("ErrorNo");
			String errorMsg = (String) client.getPropertyAsString("ErrorMsg");
			LOGGER.info(errorMsg);
			throw new ConnectionInvidException(errorMsg);
		}
		return result;
	}

	@Override
	public void close() {
		try {
			if (client != null) {
				client.invoke("DisConnect");
				client.invoke("Free");
			}
		} catch (Exception e) {
			LOGGER.info(e.getMessage());
		}

	}

	public static void main(String[] args) throws COMException {
		DispatchPtr client = new DispatchPtr("HsCommX.Comm");
		client.invoke("create");
		client.invoke("SetConnect");
		int con = (int) client.invoke("Connect");
		if (con != 0) {
			LOGGER.error("证券客户端 创建失败");
			// System.exit(-1);
		}
		client.invoke("Disconnect");
		client.invoke("Free");

	}
}

 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值