How to call EJB from NetBeans module

Apache NetBeans Wiki Index

Note: These pages are being reviewed.

This How-To is based on GlassFish EJB Faq

How to call EJB from Java EE Application Client built on top of NetBeans Platform

Important: Application Client must be created as it is described in Java EE Application Client on top of the NetBeans Platform Tutorial otherwise this will not work

  • create lookup method in some class in your module

  • add entry to application-client.xml in application client module

Example

  • for following lookup method in some class from your module:

protected mypkg.MySessionBeanRemote lookupMySessionBean() {
    try {
        javax.naming.Context c = new javax.naming.InitialContext();
        return (mypkg.MySessionBeanRemote) c.lookup("java:comp/env/ejb/MySessionBean");
    } catch(javax.naming.NamingException ne) {
        java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
        throw new RuntimeException(ne);
    }
}
  • there must be following entry in application-client.xml:

<ejb-ref>
    <ejb-ref-name>ejb/MySessionBean</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <remote>mypkg.MySessionBeanRemote</remote>
</ejb-ref>

How to call EJB from standalone module/NB platform based application

Call EJB on GlassFish

  • ensure that $GLASSFISH_HOME/lib/appserv-rt.jar, $GLASSFISH_HOME/lib/appserv-ext.jar, $GLASSFISH_HOME/lib/appserv-deployment-client.jar, $GLASSFISH_HOME/lib/javaee.jar, $GLASSFISH_HOME/lib/jmxremote_optional.jar are on NB platform based application’s classpath (startup classpath is not enough)

  • ensure that the same applies to jar with EJB interfaces and its helper classes

  • when using jars from GlassFish v1 or v1u1 - disable assertions (due to bug in GlassFish which should be fixed in GlassFish v2)

  • add org.omg.CORBA.ORBInitialHost and org.omg.CORBA.ORBInitialPort JVM options to application’s startup JVM options

  • use lookup

Example

  • add:

run.args.extra=-J-da -J-Dorg.omg.CORBA.ORBInitialHost=localhost -J-Dorg.omg.CORBA.ORBInitialPort=3700 \
               -cp:a $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/appserv-ext.jar:\
                     $GLASSFISH_HOME/lib/appserv-deployment-client.jar:$GLASSFISH_HOME/lib/javaee.jar:\
                     $GLASSFISH_HOME/lib/jmxremote_optional.jar:someejb.jar

to module suite project.properties

  • add javaee.jar and jar with ejb interfaces to compile time dependencies for your module

  • create lookup method for your bean in some class in your module:

// for EJB 3.0 bean
protected mypkg.MyBeanRemote lookupMyBeanRemote30 throws NamingException {
    javax.naming.Context ic = new javax.naming.InitialContext();
    return (mypkg.MyBeanRemote) ic.lookup("mypkg.MyBeanRemote");
}

// for EJB 2.1 and/or earlier
protected mypkg.MyBeanRemote lookupMyBeanRemote21 throws NamingException {
    javax.naming.Context ic = new javax.naming.InitialContext();
    Object remote = c.lookup("java:comp/env/ejb/MyBean");
    mypkg.MyBeanRemoteHome rv = (mypkg.MyBeanRemoteHome) PortableRemoteObject.narrow(remote, mypkg.MyBeanRemoteHome.class);
    return rv.create();
}

Applies to: NetBeans 5.5, 6.0, 6.1

Platforms: all