Injection of an EJB into a web java class under JBoss 7.1.1

Posted by Dobbo on Stack Overflow See other posts from Stack Overflow or by Dobbo
Published on 2012-11-21T21:34:15Z Indexed on 2012/11/22 11:00 UTC
Read the original article Hit count: 325

Filed under:
|
|

I am trying to build a website using JBoss 7.1.1 and RESTeasy. I have managed to constructed and deploy and EAR with a both a WAR and an EJB-JAR contained within:

voyager-app.ear 
  META-INF/MANIFEST.MF
  META-INF/application.xml
  META-INF/jboss-app.xml
  lib/voyager-lib.jar
  voyager-adm.war
  voyager-ejb.jar
  voyager-web.war

So far things are very simple. voyager-adm.war & voyager-lib.jar are empty (just the manifest file) but I know that I'm going to have code for them shortly. There is just one Stateful EJB - HarbourMasterBean (with just a local interface) and a few Database Entity Beans in the EJB jar file:

voyager-ejb.jar 
  META-INF/MANIFEST.MF
  META-INF/persistence.xml
  com/nutrastat/voyager/db/HarbourMasterBean.class
  com/nutrastat/voyager/db/HarbourMasterLocal.class
  com/nutrastat/voyager/db/PortEntity.class
  com/nutrastat/voyager/db/ShipEntity.class

As far as I can tell the EJBs deploy correctly because the database units are created and the log shows that the publication of some HarbourMaster references:

java:global/voyager-app/voyager-ejb/harbour-master!com.nutrastat.voyager.db.HarbourMasterLocal
java:app/voyager-ejb/harbour-master!com.nutrastat.voyager.db.HarbourMasterLocal
java:module/harbour-master!com.nutrastat.voyager.db.HarbourMasterLocal
java:global/voyager-app/voyager-ejb/harbour-master
java:app/voyager-ejb/harbour-master
java:module/harbour-master

The problem lies in getting the HarbourMaster EJB injected into my web bean. The reference to it is alway NULL no matter what I try.

voyager-web.war 
  META-INF/MANIFEST.MF
  WEB-INF/web.xml
  WEB-INF/classes/com/nutrastat/voyager/web/
  WEB-INF/classes/com/nutrastat/voyager/web/Ships.class
  WEB-INF/classes/com/nutrastat/voyager/web/VoyagerApplication.class

Ships.java:

@Path("fleet")
public class Ships {

  protected transient final Logger log;

  @EJB
  private HarbourMasterLocal harbourMaster;

  public Ships() {
log = LoggerFactory.getLogger(getClass());
  }

  @GET
  @Path("ships")
  @Produces({"text/plain"})
  public String listShips() {
    if (log.isDebugEnabled())
        log.debug("Harbour master value: " + harbourMaster);
    return "Harbour Master: " + harbourMaster;
  }
}

<web-app
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0" >

  <display-name>Voyager Web Application</display-name>

  <listener>
    <listener-class>
      org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
  </listener>

  <servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>
      org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
    <init-param>
      <param-name>
    javax.ws.rs.Application
      </param-name>
      <param-value>
    com.nutrastat.voyager.web.VoyagerApplication
      </param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

 &lt;/web-app&gt;

I have been searching the web for an answer and read a number of places, both on StackOverflow and elsewhere that suggests is can be done, and that the problems lies with configuration. But they post only snippets and I'm never sure if I'm doing things correctly.

Many thanks for any help you can provide.

Dobbo

© Stack Overflow or respective owner

Related posts about jboss

Related posts about ejb