7.4. 迁移应用程序客户端


7.4.1. 将 Naming Client 配置迁移到 Elytron

本节介绍如何使用 org.jboss.naming.remote.client.InitialContext 类执行远程 JNDI 查找的客户端应用程序,该类由 org.jboss.naming.remote.client.InitialContext 类来支持。

以下示例假设通过为用户凭证指定属性和它连接到的命名供应商的 URL 创建 InitialContextFactory 类。

示例:之前版本中使用的 InitialContext 代码

Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put(Context.PROVIDER_URL,"http-remoting://127.0.0.1:8080");
properties.put(Context.SECURITY_PRINCIPAL, "bob");
properties.put(Context.SECURITY_CREDENTIALS, "secret");
InitialContext context = new InitialContext(properties);
Bar bar = (Bar) context.lookup("foo/bar");
...
Copy to Clipboard Toggle word wrap

您可以从以下迁移方法之一中选择:

7.4.1.1. 使用配置文件方法迁移 Naming 客户端

按照以下步骤,使用配置方法将命名客户端迁移到 Elytron。

  1. 在客户端应用程序 META-INF/ 目录中创建 wildfly-config.xml 文件。该文件应包含要在建立与命名提供程序的连接时使用的用户凭据。

    示例: wildfly-config.xml 文件

    <configuration>
      <authentication-client xmlns="urn:elytron:client:1.2">
        <authentication-rules>
          <rule use-configuration="namingConfig">
            <match-host name="127.0.0.1"/>
          </rule>
        </authentication-rules>
        <authentication-configurations>
          <configuration name="namingConfig">
            <set-user-name name="bob"/>
            <credentials>
              <clear-password password="secret"/>
            </credentials>
          </configuration>
        </authentication-configurations>
      </authentication-client>
    </configuration>
    Copy to Clipboard Toggle word wrap

  2. 如下例所示,创建一个 InitialContext。请注意,InitialContextorg.wildfly.naming.client.WildFlyInitialContextFactory 类支持。

    示例: InitialContext 代码

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.wildfly.naming.client.WildFlyInitialContextFactory");
    properties.put(Context.PROVIDER_URL,"remote+http://127.0.0.1:8080");
    InitialContext context = new InitialContext(properties);
    Bar bar = (Bar) context.lookup("foo/bar");
    ...
    Copy to Clipboard Toggle word wrap

7.4.1.2. 使用编程方法迁移 Naming 客户端

使用此方法,您可以提供用于在应用程序代码中直接与命名提供程序建立连接的用户凭证。

示例:使用编程方法进行代码

// Create the authentication configuration
AuthenticationConfiguration namingConfig = AuthenticationConfiguration.empty().useName("bob").usePassword("secret");

// Create the authentication context
AuthenticationContext context = AuthenticationContext.empty().with(MatchRule.ALL.matchHost("127.0.0.1"), namingConfig);

// Create a callable that creates and uses an InitialContext
Callable<Void> callable = () -> {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.wildfly.naming.client.WildFlyInitialContextFactory");
    properties.put(Context.PROVIDER_URL,"remote+http://127.0.0.1:8080");
    InitialContext context = new InitialContext(properties);
    Bar bar = (Bar) context.lookup("foo/bar");
    ...
    return null;
};

// Use the authentication context to run the callable
context.runCallable(callable);
Copy to Clipboard Toggle word wrap

此迁移示例假定客户端应用已配置为通过 jboss-ejb-client.properties 文件调用部署到远程服务器的 Jakarta Enterprise Beans。此文件位于客户端应用 META-INF/ 目录中,包含连接到远程服务器所需的以下信息。

示例: jboss-ejb-client.properties 文件

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=127.0.0.1
remote.connection.default.port = 8080
remote.connection.default.username=bob
remote.connection.default.password=secret
Copy to Clipboard Toggle word wrap

客户端使用类似以下示例的代码查找 Jakarta Enterprise Beans 并调用其方法之一。

示例:调用远程 Jakarta Enterprise Bean 的客户端代码

// Create an InitialContext
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext context = new InitialContext(properties);

// Look up the {JEB} and invoke one of its methods
RemoteCalculator statelessRemoteCalculator = (RemoteCalculator) context.lookup(
    "ejb:/ejb-remote-server-side//CalculatorBean!" + RemoteCalculator.class.getName());
int sum = statelessRemoteCalculator.add(101, 202);
Copy to Clipboard Toggle word wrap

您可以从以下迁移方法之一中选择:

按照以下步骤,使用配置方法将命名客户端迁移到 Elytron。

  1. 在客户端应用程序 META-INF/ 目录中配置 wildfly-config.xml 文件。该文件应包含要在建立与命名提供程序的连接时使用的用户凭据。

    示例: wildfly-config.xml 文件

    <configuration>
      <authentication-client xmlns="urn:elytron:client:1.2">
        <authentication-rules>
          <rule use-configuration="ejbConfig">
            <match-host name="127.0.0.1"/>
          </rule>
        </authentication-rules>
        <authentication-configurations>
          <configuration name="ejbConfig">
            <set-user-name name="bob"/>
            <credentials>
              <clear-password password="secret"/>
            </credentials>
          </configuration>
        </authentication-configurations>
      </authentication-client>
      <jboss-ejb-client xmlns="urn:jboss:wildfly-client-ejb:3.0">
        <connections>
          <connection uri="remote+http://127.0.0.1:8080" />
        </connections>
      </jboss-ejb-client>
    </configuration>
    Copy to Clipboard Toggle word wrap

  2. 如下例所示,创建一个 InitialContext。请注意,InitialContextorg.wildfly.naming.client.WildFlyInitialContextFactory 类支持。

    示例: InitialContext 代码

    // Create an InitialContext
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.wildfly.naming.client.WildFlyInitialContextFactory");
    InitialContext context = new InitialContext(properties);
    
    // Look up an {JEB} and invoke one of its methods
    // Note that this code is the same as before
    RemoteCalculator statelessRemoteCalculator = (RemoteCalculator) context.lookup(
        "ejb:/ejb-remote-server-side//CalculatorBean!" + RemoteCalculator.class.getName());
    int sum = statelessRemoteCalculator.add(101, 202);----
    Copy to Clipboard Toggle word wrap

  3. 现在,您可以删除过时的 jboss-ejb-client.properties 文件,因为不再需要该文件。

使用此方法,您可以提供在应用程序代码中直接连接远程服务器所需的信息。

示例:使用编程方法进行代码

// Create the authentication configuration
AuthenticationConfiguration ejbConfig = AuthenticationConfiguration.empty().useName("bob").usePassword("secret");

// Create the authentication context
AuthenticationContext context = AuthenticationContext.empty().with(MatchRule.ALL.matchHost("127.0.0.1"), ejbConfig);

// Create a callable that invokes the {JEB}
Callable<Void> callable = () -> {

    // Create an InitialContext
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "remote+http://127.0.0.1:8080");
    InitialContext context = new InitialContext(properties);

    // Look up the {JEB} and invoke one of its methods
    // Note that this code is the same as before
    RemoteCalculator statelessRemoteCalculator = (RemoteCalculator) context.lookup(
        "ejb:/ejb-remote-server-side//CalculatorBean!" + RemoteCalculator.class.getName());
    int sum = statelessRemoteCalculator.add(101, 202);
    ...
    return null;
};

// Use the authentication context to run the callable
context.runCallable(callable);
Copy to Clipboard Toggle word wrap

现在,您可以删除过时的 jboss-ejb-client.properties 文件,因为不再需要该文件。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat