【问题标题】:Java single threaded RMI or alternativeJava 单线程 RMI 或替代方案
【发布时间】:2012-12-06 01:07:32
【问题描述】:

Lotus Notes Java 库只在 32 位 JVM 中运行,我需要从我的 64 位 JVM 应用程序中调用它,所以我写了一个 RMI 桥:64 位应用程序运行 32 位 RMI 服务器,并与 32 位服务器对话以进行 Lotus Notes 调用。

Lotus Notes 要求每个线程(将调用任何 Lotus Notes 函数)调用 lotus.domino.NotesThread.sinitThread();在调用任何其他 Lotus Notes 函数之前,并在最后通过调用 un-init 函数进行清理,这些调用可能很昂贵。

由于 RMI 不保证单线程执行,如何将所有请求通过管道传输到已为 Lotus Notes 初始化的单个线程?我也对其他 RPC/“桥”方法持开放态度(更喜欢使用 Java)。目前,我必须确保我定义的每个 RMI 函数调用都确保其线程已初始化。

【问题讨论】:

    标签: java rmi rpc


    【解决方案1】:

    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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用single thread executor service,每次要调用lotus notes方法时,向executor提交任务,获取返回的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();
      

      【讨论】:

      • 我在下面发布我使用的最终代码(这里不能轻易发布代码)
      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多