-->

@Remote JNDI Communication: Wildfly to JBoss AS 5.

2020-07-23 08:55发布

问题:

Architecture: Windows Client -> Wildfly JAX-RS Services -> JBoss 5.1.0.GA legacy system.

I am getting a java.lang.ClassCastException: javax.naming.Reference cannot be cast to com.interfaces.GroupBookingManagerRemote when communicating here between Wildfly JAX-RS Services and JBoss 5.1.0.GA legacy system.

As I am communicating from Wildfly to JBoss AS 5.1.0.GA I am attempting to connect using JNDI.

In my Wildfly Server Maven pom I include:

<dependency>
    <groupId>jboss</groupId>
    <artifactId>jnp-client</artifactId>
    <version>4.2.2.GA</version>
</dependency>

This gives me access to the required org.jnp.* classes and interfaces.

I simply use the following code to connect to my remote machine and retrieve back a GroupBookingManager. However the issue appears when I attempt to cast the class to the interface 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
}

I have a myclient.jar file which I have added to my Wildfly application that contains the remote interface GroupBookingManagerRemote.

Does anyone see any issue with what I have done?

Thanks,

Darren

回答1:

Thanks for your help Gimby,

I found the answer myself after a bit more messing about.

From Wildfly 8.1.0 (client) -> JBoss AS 5

You do not require any JBoss 5 jars

Firstly you need a reference to the interface that you wish to use on the client side. This can be in a your-project-client.jar. If using Maven you can create a repository and build the Maven directory structure using mvn

mvn install:install-file -DlocalRepositoryPath=DirectoryName -DcreateChecksum=true -Dpackaging=jar -Dfile=Path-to-you-project-client.jar -DgroupId=YourGroupId -DartifactId=YourartifactId -Dversion=1.0

Then in order to connect to the remote machine and cast the interface back to your-interface, you use:

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);

This uses Wildfly remote:// which is in remote naming and ejb in wildfly-ejb-client-bom

<dependency>
    <groupId>org.wildfly</groupId>
    <artifactId>wildfly-ejb-client-bom</artifactId>
    <version>8.1.0.Final</version>
    <scope>compile</scope>
    <type>pom</type>
</dependency> 

And I also required this dependency for communication

<dependency>
    <groupId>org.jboss.xnio</groupId>
    <artifactId>xnio-nio</artifactId>
    <version>3.2.2.Final</version>
    <scope>compile</scope>
</dependency>

and this one for the remote naming.

<dependency>
    <groupId>org.jboss</groupId>
    <artifactId>jboss-remote-naming</artifactId>
    <version>2.0.1.Final</version>
</dependency>                   

Also note the port is not the ususal port for JBoss 5 JNDI:1099 this is the Default Remoting Port : 4447

Object ref = initialContext.lookup("ejb:Your-EAR/YourClass/remote!" + YouClass.class.getName());

You can then cast your reference to your interface and use it as normal.

Hope this makes sense.