kukuh utama
kukuh utama

Reputation: 11

Invoke EJB Remote Bean in Web App

I have just begun reading up on EJBs.

Is it possible invoke remote bean in seperate web app (war)? if its possible how to achieve it. I tried it, web app can invoke remote bean if both in same EAR but i want invoke it in separate EJB + WAR.

ps. I use glassfish 4.0

Upvotes: 0

Views: 305

Answers (1)

Alex A
Alex A

Reputation: 138

For glassfish you can try this: https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB

Below my example for weblogic.

1) in your EAR you should have @remote annotated interface and it's implementation

@Remote
public interface Calculator {

    public int add(int a, int b);

}

@Stateless(mappedName = "myCalculator")
public class CalculatorImpl implements Calculator {
    @Override
    public int add(int a, int b) {
      return a + b;
    }
}

2) you should have a Client which will call your remote calculator

private static Calculator getRemoteCalculator() {
    Hashtable<String, String> props = new Hashtable<String, String>();

    props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    props.put(Context.PROVIDER_URL, "t3://localhost:7001");    


    try {
        InitialContext ctx = new InitialContext(props);
        return (Calculator) ctx.lookup("myCalculator#com.javaee.Calculator");
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}

3) for Client you should add your remote Calculator EJB module to build pass

Upvotes: 1

Related Questions