property file 檔名為 opc_config.properties ,內容如下:
opc.url=opc.tcp://192.168.1.1:49320 opc.retry.interval=5 opc.max.retries=5 opc.sleep.ms=5000
我目前的開發的目錄是在 Acme 的 package之下:
property file 就要放在 [IntegrationServer 安裝目錄]\instances\default\packages\Acme\resources 之下
值得注意的是,當我們在 load 此 property file 時,路徑規範是 packages\\[你的 package name]\\resources\\[你的 property file name],如 packages\\Acme\\resources\\opc_config.properties ,若沒按照此規範會出現 FileNotFoundException
Sample code 如下:
/**
* The primary method for the Java service
*
* @param pipeline
* The IData pipeline
* @throws ServiceException
*/
public static final void PropertyFileUtils(IData pipeline) throws ServiceException {
String config_file = "packages\\Acme\\resources\\opc_config.properties";
try {
Properties properties = loadProperties(config_file);
String opcUrl = properties.getProperty("opc.url");
String retryInterval = properties.getProperty("opc.retry.interval");
String maxRetry = properties.getProperty("opc.max.retries");
String sleepMs = properties.getProperty("opc.sleep.ms");
logger("opcUrl = " + opcUrl);
logger("retryInterval = " + retryInterval);
logger("maxRetry = " + maxRetry);
logger("sleepMs = " + sleepMs);
} catch (IOException e) {
throw new ServiceException(e);
}
}
// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---
public static Properties loadProperties(String config_file) throws ServiceException, IOException{
Properties properties = new Properties();
InputStream inputStream = null;
try {
inputStream = new FileInputStream(config_file);
properties.load(inputStream);
} catch (IOException e) {
throw new ServiceException(e);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
return properties;
}
public static void logger(String message) throws ServiceException {
IData input = IDataFactory.create();
IDataCursor inputCursor = input.getCursor();
IDataUtil.put(inputCursor, "message", message);
IDataUtil.put(inputCursor, "function", "customLogger");
IDataUtil.put(inputCursor, "level", "INFO");
inputCursor.destroy();
try {
Service.doInvoke("pub.flow", "debugLog", input);
} catch (Exception e) {
throw new ServiceException(e);
}
}
// --- <<IS-END-SHARED-SOURCE-AREA>> ---
相同的,如果你有一些設定用的檔案如 csv 等,也可以放在 resources 目錄下,透過相同的方式皆可讀取得到
Reference
[1] https://goo.gl/EFbLkg