users@jersey.java.net

Re: [Jersey] which ManagedBean can inject EJB ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 21 Oct 2009 18:46:41 +0200

On Oct 21, 2009, at 6:35 PM, Felipe Gaścho wrote:

> you are injecting integers ???

It is just an example of using @Resource for injection. See the web.xml.

You can add the following class:

package com.sun.jersey.samples.managedbeans.resources;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
  *
  * @author paulsandoz
  */
@Path("foo")
@Stateless
public class Foo {
     @GET
     public String get() {
         return "foo";
     }
}

Then modify ManagedBeanPerRequestResource to be:

@Path("/managedbean/per-request")
@ManagedBean
public class ManagedBeanPerRequestResource {

     @Resource(name="injectedResource") int injectedResource = 0;

     @Context UriInfo ui;

     @EJB Foo foo;

     public ManagedBeanPerRequestResource() {
          
Logger
.getLogger
(ManagedBeanPerRequestResource.class.getName()).log(Level.INFO, "In
constructor " + this);
     }

     @PostConstruct
     public void postConstruct() {
          
Logger
.getLogger
(ManagedBeanPerRequestResource.class.getName()).log(Level.INFO, "In
post construct " + this + "; UriInfo: " + ui);
     }

     @PreDestroy
     public void preDestroy() {
          
Logger
.getLogger
(ManagedBeanPerRequestResource.class.getName()).log(Level.INFO, "In
pre destroy");
     }

     @GET
     @Produces("text/plain")
     public String getMessage() {
          
Logger
.getLogger
(ManagedBeanPerRequestResource.class.getName()).log(Level.INFO, "In
getMessage " + this + "; UriInfo: " + ui);

         return Integer.toString(injectedResource++) + " " + foo.get();
     }
}


I think your code may have some more fundamental issues with EJBs, i
am not expert enough to help you on that unfortunately.

Paul.

> I am trying to inject @Stateless objects....
>
> is it something like that:
>
> before: @EJB private PujCompetitionFacade facade;
>
> after: @Resource(name="injectedResource") private
> PujCompetitionFacade facade;
>
> ??
>
> anyway I follow with the container mess
> "com.sun.enterprise.admin.cli.CommandException: remote failure:
> Exception while loading the app : java.lang.RuntimeException: EJB
> Container initialization error"
>
> in the log:
>
> java.lang.RuntimeException: EJB Container initialization error
> at
> org
> .glassfish
> .ejb.startup.EjbApplication.loadContainers(EjbApplication.java:180)
> at com.sun.grizzly.util.FixedThreadPool
> $BasicWorker.run(FixedThreadPool.java:410)
> at java.lang.Thread.run(Thread.java:619)
> Caused by: java.lang.RuntimeException: Error while binding JNDI name
> com
> .kenai
> .puj
> .arena
> .model
> .entity
> .facade
> .PujCompetitionFacade
> #com.kenai.puj.arena.model.entity.facade.PujCompetitionFacade
> for EJB : PujCompetitionFacadeImpl
> at
> org
> .glassfish
> .ejb.startup.EjbApplication.loadContainers(EjbApplication.java:168)
> ... 32 more
> Caused by: javax.naming.NameAlreadyBoundException: Use rebind to
> override
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>