Make shared Java EE "Module" usable by multiple processes (via CDI)

Hello,
I would like to create a Java EE Module that would be accessible by multiple processes via CDI. The shared module would contain some beans that would handle things like mailing, integration with other systems pdf generation etc. Please see the diagram for hopefully better explanation.

I would like to be able to access this shared module from any process which has the dependency included. I would like to access it preferably the same way as any other bean by the cdi both by the expression eg. ${MyBen.method()} or using @Inject from the Java part of the app.

Would something like this be possible?

Thank you

Hey Adam,

I think an easy solution is to use cdi producer methods.
You can write a producer method for every service in the shared module. (I’m guessing the shared services module, process 1 and process 2 are different ear files within the application server)

Then you can bundle all producer methods in a jar which is referenced from process 1 and process 2. (Don’t forget the beans.xml in that jar)

Pseudocode:
@Produces
private IMySharedService produceMySharedService() {
return (IMySharedService) new InitialContext().lookup(“shared-service-jndi-name”);
}

1 Like

Hello Patst,
I tried to go the way you described.I created two things.

First is a shared interface jar.
Which has an interface ITestBean with annotation @Local
and an Imports class which has the producess method:

public class Imports {

  @Produces
  @EJB(lookup = "java:global/Process-utils-ejb/TestBean")
  private ITestBean testBean;

  private Imports() {
  }
}

And second is “Module” itself which contains the bean:

@Named(value = "TestBean")
@Singleton
public class TestBean implements ITestBean{
  public void test(){
       System.out.println("Test bean method");
  }        
}

But the problem is the produces annotaition throws following exception:

Can not set cz.klajmajk.process.utils.shared.interfaces.ITestBean 
field cz.klajmajk.process.utils.shared.Imports.testBean 
to cz.klajmajk.process.utils.shared.interfaces.ITestBean$$$view3

Heureka,
I finally got it working after quite a bit of hassle but the final solution looks better than I expected.

The exception in post above was caused by the @Local annotation @Remote solved that one.

That made the @Inject working. But I was not able to use it from the EL expresstion in .bpmn. The following code actually got things working:

public class Imports {

  private Imports() {
  }

  @Produces
  @Named("testBean")
  private ITestBean getTestBean() throws NamingException {
      return (ITestBean) new InitialContext().lookup("java:global/Process-utils-ejb/TestBean");
  }

}

Thank you, and I hope it helps someone not to burn so much time finding the solution :wink:

Adam