Search code examples
javajakarta-eeejbjax-rs

How to call Ejb bean in same Jvm?


i want to know from how many ways i can call my EJb bean without Jax-rs web service.I setup an EJB 3 interface/implementation looking like this...

UserService (interface)

package business;

public interface UserService {

    public String doSomething();

}

UserServiceBean (implementation)

@Stateless
@Local
public class UserServiceBean implements UserService{

    public UserServiceBean() {
    }

    @Override
    public String doSomething() {
        return "Work done!";
    }

}

What i know: I know by calling my web service i can get output : "Work done!" like this.

RestService (Web Service)

package webservices;

@Path("/service")
public class RestService {

    @Inject
    private UserService userService;

    public RestService() {
        // TODO Auto-generated constructor stub
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    @Path("/userService")
    public String getUserServiceResponse(String json){
        String response = userService.doSomething();
        return response;
    }

}

What I Want: I want a simple method/approach/shortcut etc you can say it. To call my EJB bean without any web service to get my expected output : "Work done!" .

As like we uses public static void main method in java application.It is very clear question which i have asked.Hope you all got it.


Solution

  • If I understand your request properly you look for a way to call your EJB from outside the container from a java main class?

    In that case you need to

    • get an JNDI context with the correct properties for your container
    • lookup a reference to the EJB within the container
    • execute the business method on it

    Here's an example for the WildFly:

    public static void main(String[] args) throws NamingException {
            Properties jndiProps = new Properties();
            jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
            jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
            jndiProps.put("jboss.naming.client.ejb.context", true);
            jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
            jndiProps.put(Context.SECURITY_PRINCIPAL, "user");
            jndiProps.put(Context.SECURITY_CREDENTIALS, "xxx");
            Context ctx = new InitialContext(jndiProps);
            MyBeanRemote myBean = (MyBeanRemote) ctx.lookup("/<appname>/<beanname>!<fullqualifiedremoteinterface>");
            myBean.doSomething();
        }
    

    Note the following:

    • You need to have a remote interface for your EJB
    • You may need to add an application user via script in WildFly's bin folder
    • You need to adapt the lookup string to your needs, you'll see it in WildFly logs during deployment (just leave out namespace prefix)

    I included a full answer here for completeness, credits belong to user theisuru for my own question