1

Such simple question but I can't find the answer in google. I have two osgi bundles on glassfish4:

  1. Bundle1 - Servlet
  2. Bundle2 - MyEJB1, MyEJB2. Both EJB are local, stateless, implements the same interface (MyLocalInterface) and every has its own name.

In servlet I inject EJB like this:

@Inject @OSGiService (dynamic = true)
private MyLocalInterface ejb;

How can I distinguish via names these two EJB for injection? As I think I should use serviceCriteria in @OSGiService but how?

EDIT:
I have a lot of osgi services (not ejb, simple osgi services) with the same interface. To distinguish them I use

@Inject @OSGiService(dynamic=true, serviceCriteria = "(component.name=com.bla.bla)"). 

The same I need for ejb.

EDIT2:
Up to now the only way I've found is to use jndi-name as both cdi beans are EJB:

@Inject @OSGiService(dynamic=true, serviceCriteria = "(jndi-name=java:global/....)"). 

Be careful! It won't work without "()"! But this solution could be much better if we solve this question: Glassfish 4: how to set application or module name for JNDI

Community
  • 1
  • 1
  • I am not sure how glassfish exposes the OSGi services. Can you try to check on the OSGi level which service properties are set? You can then query for these properties in the serviceCriteria. – Christian Schneider Oct 03 '18 at 06:47

1 Answers1

1

Simply use the @Named("myname") annotation. It is already defined in the standard.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • Do you mean Inject OSGiService Named("myname")? Can you provide example of code for my servlet? –  Jun 13 '14 at 15:42
  • It works at least in the same CDI module simply with @Inject @Named("myname"). Not sure if this also works between bundles. If other qualifier annotations worked for you in this case then this one should also work (Like you showed in your comment). – Christian Schneider Jun 13 '14 at 15:47
  • I don't think that Inject Named can work between osgi bundles. In one bundle I think it will work, but not between bundles. –  Jun 13 '14 at 15:51
  • 1
    I have found that you need to use "Export-EJB: ALL" in your Manifest to export your CDI beans as OSGi services. I have not yet found how to disambiguate them. Can you just try to use some qualifiers and @Named and check what properties the OSGi services get? For pax cdi there are plans to provide a mapper between qualifier annotations and OSGi service properties. Not sure if glassfish also has that. – Christian Schneider Jun 13 '14 at 16:12