1

I have a EAP created with Eclipse containing two modules: a Dynamic Web Application, and a EJB module.

The application.xml of the EAP:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" id="Application_ID" version="7">
  <display-name>JExecuterEAP</display-name>
  <module>
    <ejb>JExecuterEJB.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>JExecuter.war</web-uri>
      <context-root>JExecuter</context-root>
    </web>
  </module>
</application>  

The EJB module:

package myp;

import javax.ejb.Stateless;

@Stateless
public class ExecutionEngineBean implements IExecutionEngine {

    public ExecutionEngineBean() {

    }

    @Override
    public void test() {
        System.out.println("Test EJB");
    }

}


package myp;

import javax.ejb.Remote;

@Remote
public interface IExecutionEngine {
    public void test();
}

The Web Application:

import myp.IExecutionEngine;


@ServerEndpoint(value = "/executerendpoint")
public class ExecuterEndpoint {

    @EJB
    IExecutionEngine bean;

    private static final Logger logger =     Logger.getLogger("ExecuterEndpoint");


    @OnOpen
    public void openConnection(Session sesion) {
        logger.log(Level.INFO, "open conection");

        //Here the EJB is NULL

    }

    @OnMessage
    public void onMessage(Session session, String msg) {        
    }

}

I'm doing the deployment in GlassFish 4.1.

Rafael Angarita
  • 777
  • 10
  • 27

2 Answers2

1

It looks like the problem is related to the EAR.

If you put the same code into a WAR file it should work, but I'm not sure about the reason.

It may be related to Tyrus, the WebSocket implementation which comes with Glassfish. I guess something is missing which is required to make it work in an EAR.

Have a look at this question:

The answer suggests to use @Inject and to check out the Tyrus CDI samples & tests, they contain examples what is possible with the current version of Tyrus. There is test named InjectToStatefulEndpoint.java which injects an EJB via @Inject, but it looks like the test is also running inside a WAR structure.

I have also seen another working example (source can be found here on GitHub) which uses an EAR and annotates the bean with @LocalBean, but I couldn't get this to work in my own project.

See also:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66
0

Actually, after trying a lot things, it worked when I annotated the class ExecuterEndpoint with @Stateless.

I really appreciate if someone has a good explanation.

Rafael Angarita
  • 777
  • 10
  • 27