3 Replies Latest reply on Sep 19, 2018 4:21 AM by Falco Thiel

    [JBoss MSC 1.4.x] How to get the org.jboss.msc.Service from a ServiceController?

    Falco Thiel Newbie

      Hi,

      I'm migrating my project from WildFly 11 to WildFly 14. The JBoss MSC library was updated and that's why I have some deprecation warnings.

      I figured out how to use the new JBoss MSC Api to install my Services and set the dependencies between them in my ServiceActivator.

      But I can't find a not deprecated way to obtain a org.jboss.msc.Service from a ServiceContainer as in the following example:

       

      ServiceName serviceName = ...;

      ServiceContainer serviceContainer = ...;

      ServiceController<?> serviceController = serviceContainer.getService(serviceName);

      serviceController.getService();

       

      The method org.jboss.msc.service.ServiceController.getService() is deprecated and it returns org.jboss.msc.service.Service which is deprecated too.

      My Problem here is that there is no method to get org.jboss.msc.Service from a ServiceController.

       

      How is it intended to do this with the new JBoss MSC Api?

       

      Any help would be nice, thanks in advance.

        • 1. Re: [JBoss MSC 1.4.x] How to get the org.jboss.msc.Service from a ServiceController?
          Falco Thiel Newbie

          Ok, I've managed to work my way through some of the source code and this WildFly 14 High Availability Guide  also helped. I was aiming to only use non-deprecated code but in the end I couldn't find a way to do this. So I use a mix of new and deprecated classes.

          My services still implementing the deprecated interface org.jboss.msc.service.Service. In my ServiceActivator I use the new Api, these test classes jboss-msc/src/test/java/org/jboss/msc/multi_value_services at 1.4.3.Final · jboss-msc/jboss-msc · GitHub were helpful.

           

          To get a service or better the value of a service from the ServiceContainer I found this way:

           

          Supplier<Object> serviceSupplier = new ActiveServiceSupplier<>(serviceContainer, serviceName);

          serviceSupplier.get();

           

          This gets me the value of the specified service, but the service must implement the deprecated interface org.jboss.msc.service.Service.

          • 2. Re: [JBoss MSC 1.4.x] How to get the org.jboss.msc.Service from a ServiceController?
            Richard Opalka Master

            Hi Falco,

             

            new MSC services API is based just on requires and provides clauses.

            Obtaining services from controllers was deprecated and there is no alternative.

            In case your application needs service instances then such services must allow it explicitly.

            Here's simple example:

             

            ...

             

            ServiceName HTTP_SERVER = ServiceName.of("http", "server");
            ServiceName APPLICATION = ServiceName.of("application");

            ServiceBuilder<?> sb1 = serviceContainer.addService(HTTP_SERVER);
            sb1.setInstance(new HttpServer(sb1.provides(HTTP_SERVER)));
            ServiceController serverController = sb1.install();

            ServiceBuilder<?> sb2 = serviceContainer.addService(APPLICATION);
            sb2.setInstance(new Application(sb2.requires(HTTP_SERVER)));
            ServiceController appController = sb2.install();


            ...

             

            private static final class HttpServer implements org.jboss.msc.Service {

               private final Consumer<HttpServer> service;
               private HttpServer(Consumer<HttpServer> service) { this.service = service; }

               public void start(StartContext context) { service.accept(this); }

               public void stop(StopContext context) {}

               public String getHost() { return "localhost"; }

               public int getPort() { return 80; }

            }

             

            private static final class Application implements org.jboss.msc.Service {

               private final Supplier<HttpServer> server;
               private Application(Supplier<HttpServer> server) { this.server = server; }

               // the following method is obtaining Service instance in a new way (replacement for ServiceController.getService())

               public void start(StartContext context) { System.out.println(server.get().getHost() + ":" + server.get().getPort()); }

               public void stop(StopContext context) {}

            }

            • 3. Re: [JBoss MSC 1.4.x] How to get the org.jboss.msc.Service from a ServiceController?
              Falco Thiel Newbie

              Hi Richard,

               

              thank you for your reply and I'm sincerely sorry for not seeing your answer earlier.

               

              Your explanation and your example helped me to understand the last things that were still unclear to me.