来自《ICE权威指南》一书中的Ice的一个工具类。

IceClientUtil.java:

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.lucky.interf.util;
import Ice.Communicator;
import Ice.ObjectPrx;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
/**
* @Author j.tommy
* @Date 2016/7/3 0003 下午 9:50
*/
public class IceClientUtil {
private static final Map<Class, ObjectPrx> cls2PrxMap = new HashMap<Class, ObjectPrx>();
private static volatile Ice.Communicator ic = null;
private static volatile long lastAccessTime = 0L;
private static volatile MonitorThread monitorThread;
private static int idleTimeoutSeconds = 300;
private static final String locatorKey = "--Ice.Default.Locator";
private static String iceLocator = null;
// private static final String clientConfigFile = "iceconfig/iceclient.conf";
/**
* 获取代理对象
* @param clazz
* @return
*/
public static ObjectPrx getServicePrx(Class clazz) {
ObjectPrx prx = cls2PrxMap.get(clazz);
if (prx != null) {
lastAccessTime = System.currentTimeMillis();
return prx;
}
prx = createServicePrx(getIceCommunicator(),clazz);
cls2PrxMap.put(clazz,prx);
lastAccessTime = System.currentTimeMillis();
return prx;
}
// 创建守护线程,关闭长时间不使用的连接
private static void createMonitorThread() {
monitorThread = new MonitorThread();
monitorThread.setDaemon(true);
monitorThread.start();
}
private static Ice.Communicator getIceCommunicator() {
if (ic == null) {
synchronized (IceClientUtil.class) {
if (ic == null) {
if (iceLocator == null) {
ResourceBundle rb = ResourceBundle.getBundle("iceclient", Locale.ENGLISH);
iceLocator = rb.getString(locatorKey);
idleTimeoutSeconds = Integer.parseInt(rb.getString("idleTimeoutSeconds"));
String[] initParams = new String[]{locatorKey + "=" + iceLocator};
ic = Ice.Util.initialize(initParams);
createMonitorThread();
}
// 另外一种创建Ice.Communicator的方式
/*Ice.InitializationData initData = new InitializationData();
initData.properties = Ice.Util.createProperties();
initData.properties.load(clientConfigFile);
ic = Ice.Util.initialize(initData);*/
}
}
}
return ic;
}
/**
* 使用反射方式创建ice代理对象
* @param ic
* @param clazz
* @return
*/
private static ObjectPrx createServicePrx(Communicator ic, Class clazz) {
ObjectPrx prx = null;
String className = clazz.getName();
String serviceName = clazz.getSimpleName();
int pos = serviceName.lastIndexOf("Prx");
if (pos <= 0) {
throw new IllegalArgumentException("Invalid ObjectPrx class,class name must end with Prx.");
}
String realServiceName = serviceName.substring(0, pos);
try {
Ice.ObjectPrx base = ic.stringToProxy(realServiceName);
prx = (ObjectPrx) Class.forName(className + "Helper").newInstance();
Method method = prx.getClass().getDeclaredMethod("uncheckedCast",Ice.ObjectPrx.class);
prx = (ObjectPrx) method.invoke(prx,base);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return prx;
}
static class MonitorThread extends Thread {
public void run() {
while (!monitorThread.isInterrupted()) {
try {
TimeUnit.SECONDS.sleep(5);
if (lastAccessTime + idleTimeoutSeconds < System.currentTimeMillis()) {
closeCommunicator(true);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void closeCommunicator(boolean removeServicesCache) {
synchronized (IceClientUtil.class) {
if (ic != null) {
safeShutdown();
monitorThread.interrupt();
if (removeServicesCache && !cls2PrxMap.isEmpty()) {
cls2PrxMap.clear();
}
}
}
}
private void safeShutdown() {
try {
ic.shutdown();
} catch (Exception e) {
e.printStackTrace();
} finally {
ic.destroy();
ic = null;
}
}
}

iceclient.properties文件:

1
2
--Ice.Default.Locator=DemoGrid/Locator:tcp -h 127.0.0.1 -p 4061:tcp -h 127.0.0.1 -p 4062
idleTimeoutSeconds = 300

调用:

import com.lucky.interf.generated.CalcServiceIcePrx;
import com.lucky.interf.generated.MessageServiceIcePrx;
import com.lucky.interf.util.IceClientUtil;
/**
 * @Author j.tommy
 * @Date 2016/7/2 0002 下午 12:34
 */
public class Test {
    public static void main(String[] args) {
         MessageServiceIcePrx msip = (MessageServiceIcePrx) IceClientUtil.getServicePrx(MessageServiceIcePrx.class);
         String result = msip.sendMessage("IceBox msg");
         System.out.println("result->" + result);
         CalcServiceIcePrx csip = (CalcServiceIcePrx) IceClientUtil.getServicePrx(CalcServiceIcePrx.class);
         double sum = csip.calc(0.999, 0.001);
         System.out.println("sum->" + sum);
     }
 }