【发布时间】:2011-01-31 12:02:39
【问题描述】:
在 tomcat 上使用 RMI 的最佳方式是什么?
我可以为我的所有应用程序创建全局 RMI 池或一个 RMI 连接吗?
【问题讨论】:
-
。你为什么要这样做..?
-
我需要从我的 servlet 中的 stanbalone 应用程序获取数据
在 tomcat 上使用 RMI 的最佳方式是什么?
我可以为我的所有应用程序创建全局 RMI 池或一个 RMI 连接吗?
【问题讨论】:
我在封装了 RMI 接口的控制器中使用了一个静态类。 RMI 将在服务器端汇集您的连接,您无需在 Tomcat 中担心它。
【讨论】:
RMI 示例
public interface Account
extends java.rmi.Remote {
public double balance(int accountNumber)
throws java.rmi.RemoteException;
}
public class AccountImpl
extends
java.rmi.server.UnicastRemoteObject
implements Account {
public AccountImpl()
throws java.rmi.RemoteException {
super();
}
public double add(int accountNumber)
throws java.rmi.RemoteException {
return 1000.0;
}
}
示例 Servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Account service=request.getSession()!=null ?(Account)request.getSession().getAttribute(loginService.ACCOUNT_SERVICE.name()):null;
service=service!=null?service:getService();
if(service!=null)
{
request.getSession().setAttribute(loginService.ACCOUNT_SERVICE.name(), service);
}
}
}
}
【讨论】: