1

I've a Payara5 application server running. I've deployed a JAR which contains one stateless EJB.

I've a second Java EE project which is a JAX-RS. One of the resource classes needs to access this EJB.

I'm using maven. So I have put a depency to my pom.xml for the JAX-RS application which refers to the EJB project.

<dependency>
    <groupId>diagro.be</groupId>
    <artifactId>HelloWorld</artifactId> <!-- EJB project -->
    <version>1.0</version>
    <scope>provided</scope>
</dependency>

In my resource class I inject the EJB.

@Inject HelloWorld hw;

The JAX-RS compiles and the war package is build. On deploy I get the error that the HelloWorld class is not found.

Is what I do possible? Or do I need to do something else to link?

Stijn Leenknegt
  • 1,317
  • 4
  • 12
  • 22
  • Why `HelloWorld` is configured with a scope of `provided`? There is a reason to do this instead of `compile` ?? Do you want to EJB JAR to run beside of your WAR app? – Carlitos Way Jan 23 '19 at 21:00
  • yes the ejb needs to run beside the war app. Because I want it to made it possible to run the ejb in an other war also. I configured it with "provided" because I thought it was the right choice??? – Stijn Leenknegt Jan 23 '19 at 21:21
  • Why can't each WAR have its own copy of the EJB jar? – Steve C Jan 24 '19 at 13:36

1 Answers1

0

Is what I do possible? Or do I need to do something else to link?

It is possible, but you need some things first:

  1. Your HelloWorld class should implement an interface; let's called IHelloWorld. Such interface will define the EJB methods that you want to expose to your API clients ...

  2. Also, such interface must be annotated with @javax.ejb.Local or @javax.ejb.Remote (I believe that @Local should do the right one because you will be running in the same JVM, but in my experience, when you plan to invoke a EJB in another application, @Remote should be used ... you can read more of this in here)

  3. Then, you should create such interface in a common project for your WAR application and EJB application. Let's call the project: my-proj-interfaces ...

  4. Later, in your EJB and WAR projects, you create a dependency to that project with compile scope.

  5. In the EJB project will be the HelloWorld class that implements the IHelloWorld interface.

  6. In your WAR project will be the IHelloWorld consumer; let´s call it HelloWorldClient ... that class will be something like this:

    public class HelloWorldClient {
        @EJB(lookup=xxx)
        private IHelloWorld ejbService;
    }
    

The key point is the use of the @EJB annotation and the lookup attribute, that indicates how to locate your EJB in your server ... normally, the value of the lookup attribute will be the JNDI entry used by your server to publish the EJB (check the server docs to find the right value for this attribute)...

  1. That should be all (note: this is what I do in wildfly ... I would hope that in your server the same procedure applies because all I said is standard)...
Carlitos Way
  • 3,279
  • 20
  • 30