Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestEasy @EJB returning null

I have a jax-rs service implemented with RestEasy, I am using Jboss as7 and have implemented a signleton ejb with the @Signleton annotation. The signleton is started when the server starts (@startup) and I would like to inject it into the jax-rs usign the @EJB. The problem is that the class is always null and I am started to get lost because in every tutorial I have looked this is the way they are injecting the ejb. Should I use any special xml file? what am I doing wrong?

@Path("/")
public class Service extends Application{
@EJB
private GlobalStore store;
public Service(){
fromejbstore = store.getSentiment();//null pointer is thrown
}
}

the Signleton ejb is:

@Singleton
@Startup
public class GlobalStore {
 Sentiment sentiment;
 @PostConstruct
public void initialize() {
 //do something
}
public Sentiment getSentiment(){return sentiment;}
}
like image 472
maxsap Avatar asked Oct 16 '25 15:10

maxsap


2 Answers

I have changed my code to look like this and avoid the injection:

    `try{
        InitialContext ctx=new InitialContext();
        localRef = (myEjb) ctx.lookup("java:global/appName/EjbName");
       }catch(NamingException ne){
            System.out.println("\n[MyRestService]NamingException: "+ne);
            ne.printStackTrace();
       }`

Could not find the reason the @EJB annotation is not working, probably this has something to do with the RestEasy.

like image 76
maxsap Avatar answered Oct 18 '25 19:10

maxsap


AFAIK, the @EJB annotation injection doesn't work for POJOs - try turning your Service class into a stateless session bean (by adding @Stateless annotation on the class level) and the GlobalStore injection should work like a charm

like image 39
Tomasz Knyziak Avatar answered Oct 18 '25 20:10

Tomasz Knyziak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!