@Remote JNDI通信:Wildfly到JBoss AS 5.1.0.GA

时间:2014-10-01 14:33:17

标签: java maven jboss jndi wildfly-8

结构: Windows客户端 - > Wildfly JAX-RS服务 - > JBoss 5.1.0.GA遗留系统。

我得到一个java.lang.ClassCastException:当在Wildfly JAX-RS服务和JBoss 5.1.0.GA遗留系统之间进行通信时,无法将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。*类和接口。

我只需使用以下代码连接到我的远程计算机并检索GroupBookingManager。但是,当我尝试将类强制转换为接口GroupBookingManagerRemote时,会出现问题。

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文件,我已经添加到Wildfly应用程序中,该文件包含远程接口GroupBookingManagerRemote。

有没有人看到我所做的任何问题?

谢谢,

的Darren

1 个答案:

答案 0 :(得分:2)

感谢您的帮助Gimby,

我在经历了一些混乱之后找到了答案。

来自Wildfly 8.1.0(客户端) - &gt; JBoss AS 5

您不需要任何JBoss 5罐

首先,您需要引用您希望在客户端使用的接口。这可以在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

然后,为了连接到远程计算机并将界面强制转换回您的界面,您可以使用:

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://在remotefly-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());

然后,您可以将引用转换为您的界面并正常使用它。

希望这是有道理的。

相关问题