0
votes

Although I have used EJB earlier, I want to re-assure myself that I understand how it really works.

So, I created a Simple Session Bean EJB (3.1), and packaged it as .ear (which has client jar as well). The below is the snippet:

Session Bean Implementation:

package com.example;
import javax.ejb.Stateless;

    @Stateless
    public class FirstSessionEJB implements FirstSessionEJBRemote {

    public FirstSessionEJB() {

    }

    @Override
    public String print() {

        return "Hello";
    }
}

Remote interface:

package com.example;

import javax.ejb.Remote;

@Remote
public interface FirstSessionEJBRemote {
    public String print();
}

I deployed this EJB as .ear and it was successfully deployed in Wildfly 10.x.

Now, I want to access this using a standalone Java client, running in a separate JVM.

Here is the client code (It might not be completed as I am not clear on how to invoke mainly due to JNDI).

package com.example.main;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.example.FirstSessionEJBRemote;

public class Main {

    public static void main(String[] args) throws NamingException {

        String GLOBAL_JNDI_NAME="java:global/FirstEJBProjEAR/FirstEJBProj/FirstSessionEJB!com.example.FirstSessionEJBRemote";

        Hashtable<String,String> jndiProperties = new Hashtable<>();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

        InitialContext ic = new InitialContext(jndiProperties);

        FirstSessionEJBRemote ejbRemote = null;
        ejbRemote = (FirstSessionEJBRemote)ic.lookup(GLOBAL_JNDI_NAME);
        ejbRemote.print();
    }
}

I referred to this link on how to do the JNDI lookup (and what all parameters to use in, however it is not working.)

In the link it is mentioned that it has Wildfly specific jar which works without JNDI lookup.

Can anyone help me understand:

1) What all properties I need to set up for JNDI look up?

2) Is there any specific jar that needs to be present in client side application?

I don't want to use any specific Wildfly jar, that is, I want to go with traditional JNDI lookup, so can anyone please guide me on this?

It is very frustrating to struggle just to write a simple "Hello world" kind of EJB. I referred to some books are well, but all what they have provided is just the "lookup" code without actually telling what all properties needs to be included for JNDI and any jar to be included.

1

1 Answers

2
votes

As the article you link to states albeit a bit hidden in that mountain of text, you do need the jboss-client.jar that you will find in the Wildfly server installation (bin/client/jboss-client.jar); it needs to be on the client's runtime classpath. It contains to begin with that org.jboss.ejb.client.naming package referenced in your code.

The jar contains the extra bit of magic for a client to be able to setup and maintain EJB remote invocations with the Wildfly server, just using JNDI isn't going to cut it. And there is no one jar to rule them all, each container (Wildfly, Glassfish, Weblogic, etc.) has its own implementation for a client library.


Do note that invoking EJBs from a client application is very old school (read: you don't want to do that). A more realistic and modern day view of EJB technology is to use it within an enterprise container itself, such as from a web application / war - say as part of a RESTful service. You likely don't even need the extra layer of the EAR file then, you can just package everything neatly into the one war application.

And in that scenario if you do have a client application, that client can talk to the RESTful service - a much simpler and cross-server, cross-platform communications interface.