T2Util.java
3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.cjs.cms.util.t2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.cjs.cms.biz.ServiceException;
import com.cjs.cms.util.lang.StringUtil;
import com.hundsun.t2sdk.common.core.context.ContextUtil;
import com.hundsun.t2sdk.common.share.dataset.DatasetService;
import com.hundsun.t2sdk.impl.client.T2Services;
import com.hundsun.t2sdk.interfaces.IClient;
import com.hundsun.t2sdk.interfaces.T2SDKException;
import com.hundsun.t2sdk.interfaces.share.dataset.IDataset;
import com.hundsun.t2sdk.interfaces.share.dataset.IDatasets;
import com.hundsun.t2sdk.interfaces.share.event.EventTagdef;
import com.hundsun.t2sdk.interfaces.share.event.EventType;
import com.hundsun.t2sdk.interfaces.share.event.IEvent;
/**
* 恒生T2接口工具类
*
* @author tongyufu
*
*/
public final class T2Util {
private static T2Services server;
private static IClient client;
static {
try {
server = T2Services.getInstance();
String configString = T2Util.class.getResource("/t2sdk-config.xml").getPath();
server.setT2sdkConfigString(configString);
server.init();
server.start();
client = server.getClient("myserver");
} catch (T2SDKException e) {
e.printStackTrace();
}
}
/**
* 发送请求
*
* @param params 请求参数
* @param funccode 接口编号
* @throws T2SDKException
*/
public static T2Result request(Map<String, Object> params,
String funccode) throws ServiceException {
try {
IEvent event = ContextUtil.getServiceContext().getEventFactory()
.getEventByAlias(funccode, EventType.ET_REQUEST);
//请求参数
IDataset requestParam = DatasetService.getDefaultInstance().getDataset();
for (String key : params.keySet()) {
requestParam.addColumn(key);
}
requestParam.appendRow();
for (String key : params.keySet()) {
requestParam.updateString(key, StringUtil.castString(params.get(key)));
}
event.putEventData(requestParam);
if ("651004".equals(funccode)) {
event.setStringAttributeValue(EventTagdef.TAG_SYSTEM_NO, "1");
}
//发送请求
IEvent rsp = client.sendReceive(event, 10000);
T2Result result = new T2Result();
result.setCode(rsp.getReturnCode());
//响应状态
result.setData(T2Util.getData(rsp.getEventDatas()));
return result;
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("请求T2接口出错", e);
}
}
private static List<Map<String, Object>> getData(IDatasets datasets) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < datasets.getDatasetCount(); i++) {
Map<String, Object> row = new HashMap<String, Object>();
// 开始读取单个结果集的信息
IDataset ds = datasets.getDataset(i);
// 遍历单个结果集记录,遍历前首先将指针置到开始
ds.beforeFirst();
while (ds.hasNext()) {
ds.next();
row = new HashMap<String, Object>();
for (int j = 1; j <= ds.getColumnCount(); j++) {
row.put(ds.getColumnName(j), ds.getValue(j));
}
list.add(row);
}
}
return list;
}
public static void main(String[] args) throws T2SDKException {
Map<String, Object> params = new HashMap<String, Object>();
//params.put("bank_no", "99");
params.put("mobile_tel", "13817944654");
T2Result result = T2Util.request(params, "619837");
System.out.println(result);
}
}