Stephan
Stephan

Reputation: 736

Unable to inject remote bean on JBoss EAP 7: ClassCastException

I have two maven Java applications running on two difference Jboss EAP 7 instances, lets name them Client-Application , Server-Application.

I want to do a remote injection on the Client-Application, injecting a bean from the Server-Application.

My structure is following:

Client-Application

I have added the following maven dependency:

<dependency>
    <groupId>org.jboss.eap</groupId>
    <artifactId>wildfly-ejb-client-bom</artifactId>
    <type>pom</type>
</dependency>

My main class and interface on this application looks like following:

public void remoteInjectionTest() {
    try {


        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        jndiProperties.put("java.naming.factory.initial", "org.jboss.as.naming.InitialContextFactory");
        jndiProperties.put("java.naming.provider.url", "localhost");
        jndiProperties.put("jboss.naming.client.ejb.context", "true");


        Context context = new InitialContext(jndiProperties);

        String jndi = "java:global/Server-Application/ServiceBean!test.package.ServiceBeanInterface";
        serviceInterface = (ServiceBeanInterface) context.lookup(jndi);

        return serviceInterface.getHelloString();
    } catch (NamingException e) {

        e.printStackTrace();
    }

}

and the Interface:

public interface ServiceBeanInterface {

    public String getHelloString();

}

Server-Application This section shows how my server-application looks like:

@Stateless
@Remote(ServiceBeanInterface.class)
public class ServiceBean implements ServiceBeanInterface {

    @Override
    public String getHelloString() {
        return "Hello";
    }

}

I tried also to add a @Remote on the server-applications Interface but again i had no success.

@Remote
 public interface ServiceBeanInterface {
    public String getHelloString();
 }

Any suggestions?

The error i am getting is:

Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy118 cannot be cast to test.package.ServiceBeanInterface

Upvotes: 0

Views: 923

Answers (2)

Steve C
Steve C

Reputation: 19435

Assuming that your Client-Application is also deployed on your JBoss EAP 7 instance, then the solution is far simpler than you think.

I set up a test project that creates four artifacts:

  • serverapi - contains the ServiceBeanInterface
  • serverejb - contains the ServiceBean
  • serverapp - EAR containing serverejb and serverapi
  • clientapp - WAR containing a servlet (shown below) and serverapi in it's WEB-INF/lib directory

The test servlet looks like:

@WebServlet(urlPatterns = "/")
public class Client extends HttpServlet {

    // Inject remote stub
    @EJB(lookup="java:global/serverapp-1.0-SNAPSHOT/serverejb-1.0-SNAPSHOT/ServiceBean!com.stackoverflow.p42618757.api.ServiceBeanInterface")
    private ServiceBeanInterface serviceBeanInterface;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write(serviceBeanInterface.getHelloString());
    }

}

The serverapp and clientapp are deployed to the server.

Hitting http://localhost:8080/clientapp-1.0-SNAPSHOT/ yields the expected Hello.

No old fashioned JNDI lookups needed.

However, if you really want to do a JNDI lookup, then you just need:

 Context initialContext = new InitialContext(); // no props needed in the same server instance
 ServiceBeanInterface serviceBeanInterface
     = (ServiceBeanInterface)initialContext.lookup("java:global/serverapp-1.0-SNAPSHOT/serverejb-1.0-SNAPSHOT/ServiceBean!com.stackoverflow.p42618757.api.ServiceBeanInterface");

A demo example can be found on GitHub.

Upvotes: 2

Steve C
Steve C

Reputation: 19435

There is extensive documentation for doing instance-to-instance remote EJB calls on the JBoss documentation site at EJB invocations from a remote server instance.

The configuration may look a little complicated but that is primarily for security reasons. The code side is still relatively simple.

I recommend that you refer to this and then ask a new question if you still have problems.

Upvotes: 1

Related Questions