2012-12-18 30 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); 
    } 
}