2012-12-18 29 views
1

Lotus Notes Java庫僅在32位JVM中運行,並且需要從我的64位JVM應用程序調用它,因此我編寫了一個RMI橋:64位應用程序運行32位RMI服務器,並與32位服務器通話以進行Lotus Notes調用。Java單線程RMI或替代

Lotus Notes要求每個線程(將調用任何Lotus Notes函數)調用lotus.domino.NotesThread.sinitThread();在調用任何其他Lotus Notes函數之前,並通過調用un-init函數在最後清理,並且這些調用可能很昂貴。

由於RMI不能保證單線程執行,我怎樣才能將所有請求都管理到已經初始化爲Lotus Notes的單個線程?我也對其他RPC /「橋」方法開放(更喜歡使用Java)。目前,我必須確保EVERY RMI函數調用已經定義,確保其線程已初始化。

回答

1

使用single thread executor service,並且每次您想調用蓮花筆記方法時,向執行程序提交任務,獲取返回的Future,並從Future獲取方法調用的結果。

例如,要調用的方法Bar getFoo(),你可以使用下面的代碼:

Callable<Bar> getFoo = new Callable<Bar>() { 
    @Override 
    public Bar call() { 
     return lotuNotes.getFoo(); 
    } 
}; 
Future<Bar> future = executor.submit(getFoo); 
return future.get(); 
+0

我發佈了我在下面使用的最終代碼(不能在此處輕鬆發佈代碼) – Mary

0

的getName()是一個簡單的例子,所以每個代碼得到這樣的待遇(這極大地醃的代碼,但它的工作原理!)

@Override 
    public String getName() throws RemoteException, NotesException { 
     java.util.concurrent.Callable<String> callableRoutine = 
       new java.util.concurrent.Callable<String>() { 

        @Override 
        public String call() throws java.rmi.RemoteException, NotesException { 
         return lnView.getName(); 
        } 
       }; 
     try { 
      return executor.submit(callableRoutine).get(); 
     } catch (Exception ex) { 
      handleExceptions(ex); 
      return null; // not used 
     } 
    } 


/** 
* Handle exceptions from serializing to a thread. 
* 
* This routine always throws an exception, does not return normally. 
* 
* @param ex 
* @throws java.rmi.RemoteException 
* @throws NotesException 
*/ 
private void handleExceptions(Throwable ex) throws java.rmi.RemoteException, NotesException { 
    if (ex instanceof ExecutionException) { 
     Throwable t = ex.getCause(); 
     if (t instanceof java.rmi.RemoteException) { 
      throw (java.rmi.RemoteException) ex.getCause(); 
     } else if (t instanceof NotesException) { 
      throw (NotesException) ex.getCause(); 
     } else { 
      throw new NotesException(LnRemote.lnErrorRmi, utMisc.getExceptionMessageClean(t), t); 
     } 
    } else { 
     throw new NotesException(LnRemote.lnErrorRmi, utMisc.getExceptionMessageClean(ex), ex); 
    } 
}