5 Replies Latest reply on Sep 22, 2014 8:42 PM by James Livingston

    How to share a @Singleton EJB between differents applications?

    Juan Martin Runge Newbie

      Hi,

      What Im trying to accomplish is to deploy an EJB and to use it from different other applications that may reside in the same server.  Perhaps the way to go is to write a module, but I dont know how...

       

      Let me give an example of what Im trying to do:

       

      An EJB (or plain JAR) Project with this class inside only:

      @Singleton

      @LocalBean

      public class SmsService {

           @PostConstruct

           public void init() {

                System.out.println("Hello, I'm an EJB");

           }

       

           public void greetMe(String name) {

                System.out.println("Hi " + name);

           }

      }

       

      I want to deploy this project on its own to wildfly.

       

      Then I want to have another project, lets say a WAR, for example, with this class:

      @Stateless

      @LocalBean

      public class SmsClient {

           @Resource

           private SmsService service;

       

           @PostConstruct

           public void init() {

                service.greetMe("pepe");

           }

      }

       

      And I want to have my first project referenced this way in my pom.xml (I forgot to say I was using Maven and Netbeans 8.0):

      <dependency>

           <groupId>sms</groupId>

           <artifactId>SmsService</artifactId>

           <version>0.0.1</version>

           <scope>provided</scope>

      </dependency>

       

      I did accomplish something similar, but using JNDI and I didnt manage to make wildfly to respect the @Singleton annotation (it created another instance when referencing)

       

      I think the way to go is writing a module, but havent been able to do it and couldnt find clear documentation on how to do it (everything I found referred to how to wrap a JDBC driver as a module, for example).

       

      If someone did something like this in the past, or know how to do it, please HELPPPPPP!!!! I've been two days with this now... and am quite desperate.  Perhaps arungupta?

       

      Kind regards!

       

      Juan Martin

        • 1. Re: How to share a @Singleton EJB between differents applications?
          Dirk Buchhorn Newbie

          I think you have to use a JBoss MSC Service instead of a singleton EJB. You can find a good start point on the wildfly-quickstart examples(cluster-ha-singleton). This example implements a clustered singleton. Based on this I try myself a bit and implement three maven modules (look at the attachment).

           

          wildfly-service-test: Contains the service

           

          wildfly-service-test-app-1: Contains a webservice to interact with the service

           

          wildfly-service-test-app-2: Contains a webservice to interact with the service (a copy of app-1, only the name was changed)

          • 2. Re: How to share a @Singleton EJB between differents applications?
            Juan Martin Runge Newbie

            Dirk, you're a genius!  This is was I was hoping to do!  Can I ask you for some explanation on the classes? I see MyServiceEnviroment is a simple POJO.  Is there any reason to use it or was just for the sake of the example?  And in MySingletonService you use ServiceName.JBOSS.append for creating the name.  What does this parameters mean? The only thing left is that app wont work if I deploy it from inside Netbeans.  It only works if I deploy it via Administration Console.  But sure this is an NB plugin bug.

             

            Thanks again!!!

            • 3. Re: Re: How to share a @Singleton EJB between differents applications?
              Dirk Buchhorn Newbie

              This are two good questions and I had this questions too. I read the org.jboss.msc javadoc documentation (can be downloaded from maven.org).

              In the Service Interface documentation you can read: "The value type specified by this service is used by default by consumers of this service, and should represent the public interface of this service, which may or may not be the same as the implementing type of this service"

               

              A ServiceName is structured like a java package name. The root name is JBOSS and all names are based on this name. All registered Services can be listed with CurrentServiceContainer.getServiceContainer().getServiceNames()

               

              Thats all what I currently know about this. I work on this now for less than 2 weeks.

              1 of 1 people found this helpful
              • 4. Re: How to share a @Singleton EJB between differents applications?
                Juan Martin Runge Newbie

                I started downloaded the docs (via IDE) and dont know why didnt finish...

                But now I understood why another test I made based on yours failed... As for the naming convention... its quite unclear still.  Will try to dig deeper in order to see if I can really find out what Im doing!

                But, despite this, you solved my problem, so Thanks again!

                 

                Kind Regards,

                • 5. Re: How to share a @Singleton EJB between differents applications?
                  James Livingston Apprentice

                  You should also be able to do it using @Singleton EJBs. What you'd need to do is make it implement an interface, and put that interface in a jar which SmsClient has access to. The problem with your initial attempt would be that SmsClient needs access to the SmsService class, and by doing that it effectively has it's own @Singleton EJB.