架构: Windows 客户端 -> Wildfly JAX-RS 服务 -> JBoss 5.1.0.GA 旧系统。
在 Wildfly JAX-RS 服务和 JBoss 5.1.0.GA 旧系统之间进行通信时,我收到 java.lang.ClassCastException:javax.naming.Reference 无法转换为 com.interfaces.GroupBookingManagerRemote。
当我从 Wildfly 与 JBoss AS 5.1.0.GA 进行通信时,我尝试使用 JNDI 进行连接。
在我的 Wildfly Server Maven pom 中,我包括:
<dependency>
<groupId>jboss</groupId>
<artifactId>jnp-client</artifactId>
<version>4.2.2.GA</version>
</dependency>
这使我能够访问所需的 org.jnp.* 类和接口(interface)。
我只需使用以下代码连接到我的远程计算机并检索 GroupBookingManager。但是,当我尝试将该类转换为 GroupBookingManagerRemote 接口(interface)时,就会出现问题。
Properties env = new Properties();
env.setProperty(Context.PROVIDER_URL, "jnp://myremoteserver:1099");
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
InitialContext initialContext = new InitialContext(env);
Object ref = initialContext.lookup("MyEARFile/GroupBookingManager/remote");
if (ref != null) {
bookingManager = (GroupBookingManagerRemote) ref; // java.lang.ClassCastException: javax.naming.Reference cannot be cast
}
我有一个 myclient.jar 文件,已将其添加到包含远程接口(interface) GroupBookingManagerRemote 的 Wildfly 应用程序中。
有人认为我所做的事情有任何问题吗?
谢谢
达伦
最佳答案
感谢您的帮助,金比,
经过一番折腾后,我自己找到了答案。
从 Wildfly 8.1.0(客户端)-> JBoss AS 5
您不需要任何 JBoss 5 jar
首先,您需要对要在客户端使用的接口(interface)的引用。这可以位于 your-project-client.jar 中。如果使用 Maven,您可以创建存储库并使用 mvn 构建 Maven 目录结构
mvn install:install-file -DlocalRepositoryPath=DirectoryName -DcreateChecksum=true -Dpackaging=jar -Dfile=Path-to-you-project-client.jar -DgroupId=YourGroupId -DartifactId=YourartifactId -Dversion=1.0
然后,为了连接到远程计算机并将接口(interface)转换回您的接口(interface),您可以使用:
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, org.jboss.naming.remote.client.InitialContextFactory.class.getName());
env.put(Context.PROVIDER_URL, "remote://remoteserver:4447");
InitialContext initialContext = new InitialContext(env);
这使用远程命名中的 Wildfly remote://和 wildfly-ejb-client-bom 中的 ejb
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>8.1.0.Final</version>
<scope>compile</scope>
<type>pom</type>
</dependency>
而且我还需要这种依赖关系来进行通信
<dependency>
<groupId>org.jboss.xnio</groupId>
<artifactId>xnio-nio</artifactId>
<version>3.2.2.Final</version>
<scope>compile</scope>
</dependency>
这个用于远程命名。
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-remote-naming</artifactId>
<version>2.0.1.Final</version>
</dependency>
另请注意,该端口不是 JBoss 5 JNDI:1099 的常用端口,这是默认远程处理端口:4447
Object ref = initialContext.lookup("ejb:Your-EAR/YourClass/remote!" + YouClass.class.getName());
然后,您可以将引用转换到您的界面并正常使用它。
希望这是有道理的。
关于java - @远程JNDI通信: Wildfly to JBoss AS 5. 1.0.GA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26143840/