7.4.2. 将 EJB 客户端迁移到 Elytron
此迁移示例假定客户端应用已配置为使用 jboss-ejb-client.properties 文件调用部署到远程服务器的 EJB。此文件位于客户端应用 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
客户端查找 EJB 并使用类似以下示例的代码调用其中一个方法。
示例:调用远程 EJB 的客户端代码
// 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 EJB 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);
您可以从以下迁移方法之一进行选择:
7.4.2.1. 使用配置文件方法迁移 EJB 客户端 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
按照以下步骤,使用配置方法将您的命名客户端迁移到 Elytron。
在客户端应用
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>如以下示例中所示,创建一个
InitialContext。请注意,InitialContext由org.wildfly.naming.client.WildFlyInitialContextFactory类支持。示例:
InitialContextCode// 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 EJB 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);-----
现在,您可以删除过时的
jboss-ejb-client.properties文件,因为不再需要该文件。