1

I've written an EJB3 business logic and deployed under Glassfish. I'm now going to test it through a client in same container, injecting interface with @EJB.

The problem is that this interface is null

THE CLIENT

public class EjbTest {

@EJB
private static RepAlertManager rep;


    public static void main(String[] args) throws IOException, SQLException {

    try 
    {
        DBConnection.setTypeConnetionLocal(true);

        List<Long> result = rep.doSomething("ENTRATE");

        for(Long temp:result){
            System.out.println(temp);
        }

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    }
}

THE LOCAL INTERFACE

public interface RepAlertManager {

     public List<Long> doSomething(String message) throws SQLException; }

THE STATELESS bean extending data source

@Stateless public class RepAlertManagerImpl extends DataSource implements RepAlertManager {

public RepAlertManagerImpl() throws IOException {
    super();
}


public List<Long> doSomething(String message) throws SQLException {

        // some code
} }

DATA SOURCE bean is a stateless bean setting connection in the constructor and releasing it through a get() method

Noomak
  • 371
  • 5
  • 19
  • I don't see any annotations like `@Stateless` in your code – Petr Mensik Apr 07 '15 at 12:07
  • `@EJB private static RepAlertManager rep` => Why _static_? – Seelenvirtuose Apr 07 '15 at 12:12
  • because I'm testing it from main method – Noomak Apr 07 '15 at 12:21
  • 1
    If your code is not container managed but has access to the EJB container you need to manually set your EJB field with a jndi lookup. Here's an example on how to do that https://blogs.oracle.com/roumen/entry/accessing_ejb_3_session_beans – DSF Apr 07 '15 at 12:42
  • 1
    In CDI you cannot inject a static variable. Plus glass fish will not normally run an application class like this. Glass fish is the application and components get deployed to it (servers, beans etc.) – redge Apr 07 '15 at 13:10
  • @DSF I'm receiving "javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial" - as I tried with ic.lookup("ejbModule.RepAlertManager"); and ic.lookup("java:comp/env/ejb/RepAlertManager") - how can I be sure my ejb is pooled and ready for request calling ? – Noomak Apr 07 '15 at 13:19
  • @redge but how can I try my ejb from a java client ? – Noomak Apr 07 '15 at 13:20
  • I haven't done this for a while but basically you need a Jndi client in that you can instantiate in your application and then you configure it with the host and port of your app servers Jndi implementation, Including any necessary security credentials. Have a look at some other questions like this http://stackoverflow.com/questions/18895090/jndi-object-creation-in-standalone-application – redge Apr 07 '15 at 13:31
  • Actually I may have been wrong on that previous comment it may be as simple as using the `InitialContext(Hashtable,?> environment)` constructor where the `environment` has the relevant JNDI urI and credentials for your server I'm sure there are good examples of this in the Glass fish doco. You should find a client.jar associated with your server and including this on your class path should give you the implementation I was referring to in the previous comment. – redge Apr 07 '15 at 13:39
  • `environment` will also need the INITIAL_CONTEXT_FACTORY class. – redge Apr 07 '15 at 13:48
  • A javaee application-client runs in what is called a enterprise application container. Glassfish has a set of library dependencies (including the eacc container library) form which when you download the app.jnlp, from the glassfish domain management, will take care of loading the necessary libraries, and boostraps your standalone application providing the necessary jndi context (including the server url) so that as a developer, you need not set this manually. The tutorial for this must be somewhere in jee4 ot jee5. – maress Apr 07 '15 at 13:50
  • Try here https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB – redge Apr 07 '15 at 22:00

1 Answers1

1

This one might help, provided you're using Java EE 6
Testing an EJB with JUnit

Also don't use static when injecting. CDI can't inject a static var.

Community
  • 1
  • 1
Karuro
  • 96
  • 7