0
votes

i'm triyng to develop a simple web service in Java that used an EJB, with EJBs in jar file and WebServices in war file, both packaged in one ear. We use EjB 3.0 on JBOSS-AS 7.1.

Here my EJB class with its interface :

@Stateless
public class TestEjb implements ITestEjb {

    public boolean authentifier(String login, String password) {
        if(login.equals("nico") && password.equals("nr"))
            return true;
        else 
            return false;
    }

}

@Local
public interface ITestEjb {
    public boolean authentifier(String login, String password);
}

And here is my web service which inject the previous EJB :

//@Stateless
@WebService
@SOAPBinding(style = Style.RPC)
public class AuthentificationWS {
    @EJB
    private ITestEjb testEjb;

   public boolean authentifier(String login,String password) {
       return testEjb.authentifier(login, password);
   }
}

The deployment works well but when i call my web service, the EJB is always null
(error : java.nullpointerException).

So i tried to add a @Stateless to my web Service to convert it to EJB but without success when i try to consume it by a web service client.

Then i tried to access Ejb by local jndi lookup like below, taking the local jndi url in JBOSS log :

final String jndi = "java:module/TestEjb!fr.test.ITestEjb";
testEjb = (ITestEjb) new InitialContext().lookup(jndi);

But it did not work.

Finally i tried with the global jndi url like below :

final String jndi = "ejb:Num-ear-1.0.0-SNAPSHOT/Num-ejb-1/TestEjb!fr.test.ITestEjb"
testEjb = (ITestEjb) new InitialContext().lookup(jndi);

And it works well !!

So, i don't understand why my webservice cannot access locally to its EJB by injection or local Jndi way?

3
Are TestEjb and AuthentificationWS in the same EAR? Are they in the same ejb-jar?Philippe Marschall
Which application server product and version are you using?Brett Kail
@Philippe Marschall Yes i tried but no effectsNikoas

3 Answers

3
votes

The reason is because in JBOSS-AS 7.1, ejb service is deployed and mapped in different way. If you do not specify the mapped name for stateless bean, you will have to do look up using its global name, like this: ejb:Num-ear-1.0.0-SNAPSHOT/Num-ejb-1/TestEjb!fr.test.ITestEjb

You can try to add the mapped name by following:

@Stateless(name = "ejb/ar/TestEjb", mappedName = "ejb/ar/TestEjb")
public class TestEjb implements ITestEjb {

And in your webservice:

@EJB(name = "ejb/ar/TestEjb", mappedName = "ejb/ar/TestEjb")
private ITestEjb testEjb;

And in you web.xml:

<ejb-local-ref>
    <ejb-ref-name>ejb/ar/TestEjb</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>yourpackage.TestEjb</local>
</ejb-local-ref>

In this case, I assume that you are deploying webservice as web module. For reference on how to deploy web service as web module, please take a look on this link: http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/

1
votes

I used before: new InitialContext().lookup(java:app/domain/TestService) Converted to ear and used:

@Local
@Remote
public interface Test {}

@Stateless(name="Test", mapped-name="Test")
public TestService implements Test {}

@Webservice
public MyService{
     @EJB(name="Test", mapped-name="Test")
     private Test test;
}

This seems to work, but need to set @Remote in Test interface ():). Webservice is in war, domain in jar and both are contained in ear and deployed in Glassfish (3.1.2). It is a pity I had to use annotation @Remote because it is all in the same ear. If I omit the @Remote, I get exception at deploy time in Glassfish: "Trying to set an ejb-ref on an EJB, while the EJB does not define remote interfaces". Is there a way to omit the @Remote ??

-1
votes

Try it without the interface, you don't need to define an interface in EJB3.x any longer

@Stateless
public class TestEjb {

    public boolean authentifier(String login, String password) {
        if(login.equals("nico") && password.equals("nr"))
            return true;
        else 
            return false;
    }

}

Then inject into web service as follows

@WebService
public class AuthentificationWS {
   @EJB
   private TestEjb testEjb;

   public boolean authentifier(String login,String password) {
       return testEjb.authentifier(login, password);
   }
}