Problem
假設我們已經在 Integration Server 中,在 Public Cache Managers 中建立一個名為 Test 的 Cache Manager,並於 Test 中建立一個名為 hello 的 cache
若我們希望在 Java Service 中,用 webMethods 提供的套件來:
1. 新增資料到 cache
2. 從 cache 中取出資料
3. 移除 cache 中的資料
該如何做?
How-to
以下是 sample code:
// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---
private static final String cacheManagerName = "Test";
private static final String cacheName = "hello";
public static void addValueToCacheByKey(IData pipeline, String key, Object value) throws ServiceException {
IDataCursor cursor = pipeline.getCursor();
IDataUtil.put(cursor, "cacheManagerName", cacheManagerName);
IDataUtil.put(cursor, "cacheName", cacheName);
IDataUtil.put(cursor, "key", key);
IDataUtil.put(cursor, "value", value);
try {
Service.doInvoke("pub.cache", "put", pipeline);
} catch (Exception e) {
throw new ServiceException(e);
}
cursor.destroy();
}
public static Object getValueFromCacheByKey(IData pipeline, String key) throws ServiceException {
IDataCursor cursor = pipeline.getCursor();
IDataUtil.put(cursor, "cacheManagerName", cacheManagerName);
IDataUtil.put(cursor, "cacheName", cacheName);
IDataUtil.put(cursor, "key", key);
IData data = null;
try {
data = Service.doInvoke("pub.cache", "get", pipeline);
} catch (Exception e) {
throw new ServiceException(e);
}
Object result = IDataUtil.get(data.getCursor(), "value");
cursor.destroy();
return result;
}
public static void removeCacheByKey(IData pipeline, String key) throws ServiceException {
IDataCursor cursor = pipeline.getCursor();
IDataUtil.put(cursor, "cacheManagerName", cacheManagerName);
IDataUtil.put(cursor, "cacheName", cacheName);
IDataUtil.put(cursor, "key", key);
try {
Service.doInvoke("pub.cache", "remove", pipeline);
} catch (Exception e) {
throw new ServiceException(e);
}
}
// --- <<IS-END-SHARED-SOURCE-AREA>> ---