How to create an EJB using annotation and NetBeans/GlassFish/JBoss and access it remotely in under five minutes:

Requirements: NetBeans 8.1 running on JDK1.8 and JavaEE 7

For GlassFish 4.1.1

  • Create a new Jave EE -> EJB Module project, set GlassFIsh as target for deployment.
  • Create a new Jave EJB Client Application as a regular Java application.
  • Check GlassFish Admin Console to see that bean has been deployed under Applications.
  • Right click on Enterprise Beans, and select New Session Bean.
  • Select Stateless and Local and Remote Interface.
  • Have Remote Interface created in another project, an EJB Client.
  • Copy Remote Interface classes back to this project.
  • Deploy project to GlassFish.
  • In the EJB Client Application Libraries setting, add appserv-rt.jar from GlassFish installation for run-time naming context.
  • Add Java 6 API Library for javax.ejb package for compile. This must come second after appserv-rt.jar.
  • Create Initial Context in program: InitialContext ic = new InitialContext(), there is no other setting needed if appserv-rt.jar is properly included.
  • Simply do a look up and cast the result to the remote interface.
  • The look up name should be package.classnameRemote.

For JBoss 7.1

  • Create EJB Module as for GlassFish, set JBoss as target for deployment. J2EE version should not matter.
  • Create EJB Client Application as a Maven Applicaiton.
  • Create interface files and remote interface files in Client Application.
  • Deploy EJB Module to JBoss.
  • Add Maven dependencies to Client Application

org.jboss/jboss-remote-naming/2.0.4.Final
javax.javaee/javaee/6.0-alpha-1
org.jboss.xnio/xnio-nio/3.3.6.Final

  •  Create Initial Context using these properties:

Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"
Context.PROVIDER_URL, "remote://127.0.0.1:4447"
"jboss.naming.client.ejb.context", true

  •  The look up name is "EARName/JARName/BeanName!package_name.BeanNameRemote"
  •  If EJB Module has any external JAR dependency, it is best that it is deployed as part of a an Enterprise Application (EAR) deployment.