【问题标题】:Remotely access an EJB across a network or networks from client从客户端通过一个或多个网络远程访问 EJB
【发布时间】:2023-04-03 06:51:01
【问题描述】:

假设一个 CLI 或 Swing 接口客户端,如何通过 Glassfish 访问远程 Bean?由obtaining a reference toBeanManager?

示例代码:

Hashtable contextArgs = new Hashtable();

// First you must specify the context factory.
// This is how you choose between jboss implementation
// vs. an implementation from Sun or other vendors.
contextArgs.put( Context.INITIAL_CONTEXT_FACTORY, "com.jndiprovider.TheirContextFactory" );

// The next argument is the URL specifying where the data store is:
contextArgs.put( Context.PROVIDER_URL, "jndiprovider-database" );

// (You may also have to provide security credentials)

// Next you create the initial context
Context myCurrentContext = new InitialContext(contextArgs);

http://en.wikipedia.org/wiki/Java_Naming_and_Directory_Interface#Basic_lookup

因此,要让 CLI 应用程序访问正在运行的 glassfish 应用程序服务器,它会使用以下内容:

    Context context = null;
    try {
        context = new InitialContext();
        hello = (Hello) context.lookup("java:global/SalutationApp/SalutationApp-ejb/Hello");
        hello.myRemoteMethod();
    } catch (Exception e) {
        e.printStackTrace();
    }

仅使用指定的 URL 连接到特定的 glassfish 实例?如何建立连接?

【问题讨论】:

  • 您是否考虑过用于 Swing 项目的应用程序客户端?这将允许您使用正常的@EJB 注入机制,尽管在您的主类静态变量中。
  • 意志。那允许多个客户?
  • 这就是 ejb-application-client 的目的。多个客户端,一分钟安装。自动球体配置等
  • 从未见过选项。使用。 NetBeans。

标签: java jakarta-ee glassfish ejb cdi


【解决方案1】:

如果您正在执行真正的远程查找,则无法通过 CDI 的BeanManager 执行此操作,因为 Swing GUI 不是本地的。

首先,您需要配置应用程序服务器以允许远程调用 - 这很可能包括一些身份验证设置,但我从未在 Glassfish 上这样做过。

下面是我们如何在我们自己的 Swing 应用程序之一中查找 EJB bean 的远程接口,该应用程序访问部署在 JBoss 4 上的 EJB。

首先,我们在 UI 的 ClassPath 中添加了一个jndi.properties,其中包含了哪个服务器负责命名查找的信息(请注意,端口号是特定于应用程序服务器的)。这是一个在我们自己的 Swing GUI 中使用的jndi.properties 文件:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=server.local\:1199

接下来,鉴于您的命名上下文现在知道如何查找远程 JNDI 名称,您可以使用这样的东西(同样,这在我们的 Swing UI 中使用):

InitialContext ctx = new InitialContext();
MyRemote mr = (MyRemote) ctx.lookup("global/jndi/name/of/remote/interface");

除了使用属性文件,您还可以通过在代码中传递配置值来配置 InitialContext,如下所示:

    Properties env = new Properties();
    env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory"); // depends on your server
    env.setProperty(Context.PROVIDER_URL, "jnp://server.local:1099/");
    env.setProperty(Context.SECURITY_PRINCIPAL, "user"); 
    env.setProperty(Context.SECURITY_CREDENTIALS, "password");
    InitialContext ctx = new InitialContext(env);  

此答案并不完整,但我希望这有助于您入门。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 2014-05-14
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多