-1

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.

  • Your question's subject `How to call Ejb bean in same Jvm` contradicts your second last sentence `As like we uses public static void main method in java application` because you're either asking about a client that is executing in your server in the same JVM as your EJB, or a standalone EJB client (which runs in a different JVM), so your question is not clear at all. – Steve C Nov 28 '16 at 08:03
  • Steve actually i donot know proper relationship how Ejb in same JVM. Can you explain me or else give me link to read it out. i only want to call my EJB bean class to get output. So can you tell me the way? –  Nov 28 '16 at 09:07
  • From where do you want to call your EJB? – Steve C Nov 28 '16 at 11:04
  • i want to call my Ejb from any class like Demo. Which can be in same folder as where Ejb is situated. –  Nov 28 '16 at 12:13
  • I think you are asking how to call `doSomething()` without using JAX-RS. There are lots of ways, but we don't know what you expect to be able to do. If you just want `doSomething()` to be called once at startup, you could annotate with `@PostConstruct`. If you want it triggered automatically with a timer, you could annotate with `@Schedule`. How do you want to use this method? – Mike Nov 28 '16 at 13:59

1 Answers1

0

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

Community
  • 1
  • 1
Alexander Rühl
  • 6,769
  • 9
  • 53
  • 96