1 2 3 Previous Next 35 Replies Latest reply on Oct 24, 2011 3:51 PM by lmcdasi

    can I avoid a service to block as7 startup ?

    lmcdasi

      I am trying to understand if I can avoid AS7 blocking for a Module Extension (or service). What I did as a test is to have the module extension somehow blocked in a while(true).

       

      Any idea how to define a module extension as a async task ? in order not to block the AS server.

        • 1. Re: can I avoid a service to block as7 startup ?
          jason.greene

          Since MSC is a concurrent state machine it's thread pool is a critical resource that is intended to run only non-blocking (busy) tasks. Since there can be thousands of services, and MSC does not know the usage pattern of each service, it's not possible to have a single large generic thread pool for everything. For this reason services that need to execute blocking tasks must use async startup and pass those tasks to another thread pool.

           

          See the Service javadoc for more info:

          http://docs.jboss.org/jbossmsc/1.0.0.GA/api/org/jboss/msc/service/Service.html

          • 2. Re: can I avoid a service to block as7 startup ?
            lmcdasi

            Ok. I did add :

             

            context.asynchronous();

            mystart_method();

            context.complete();

             

            But that did not make it. I guess I have to do:

             

            context.asynchronous();

             

            context.execute(mystart_method());

            context.complete();

             

            Is that right ?

             

            • 3. Re: can I avoid a service to block as7 startup ?
              dmlloyd

              You have to call context.complete() from within your asynchronous task.  No, you cannot use context.execute() for that; that uses a service thread.  You need a separate thread pool for blocking tasks.

               

              I think though that overall you might want to think about avoiding hooking into the AS at this level.

              • 4. Re: can I avoid a service to block as7 startup ?
                lmcdasi

                Sorry - getting confused. Based on the MSC reading it looks like the Lifecycle allows async tasks. The question is how one can do that ...... if it can

                • 5. Re: can I avoid a service to block as7 startup ?
                  jason.greene

                  Lifecycle.execute() reuses the same MSC thread pool, so blocking tasks are off limits there as well. It's facility is for uncommon use cases (arguably it shouldn't exist at all).

                   

                  If you want to do an async start task do the following steps:

                   

                  1. In your start() method call LifecycleContext.asynchronous. This tells MSC not to assume a method return indicates completion
                  2. In your start() method create a Thread (or a pool depending on your use case). For example, Executors.newSingleThreadExecutor.
                  3. Construct a new instance of your blocking task and pass it a copy of the StartContext (LifeCyclecontext)
                  4. The task's run method should be written to perform the blocking task and call complete() on the Lifecycle context when done
                  5. Pass and execute the task instance to the pool/thread you created in step 2. This will cause your blocking operation to start in another thread
                  6. Return from your start method.

                   

                  What will happen after that point is that the service will remain in STARTING state until the task calls complete(), once that happens it will become available to other dependencies.

                  1 of 1 people found this helpful
                  • 6. Re: can I avoid a service to block as7 startup ?
                    lmcdasi

                    I can certainly give it a try. Thanks!

                    • 7. Re: can I avoid a service to block as7 startup ?
                      lmcdasi

                      One extra question though - let's say I have multiple tasks that can be blocking, then based on what you mentioned earlier I can use a thread pool to kickstart each task. So far so good. The question is : when to call "complete" ? Should I call complete on every thread that had a blocking operation ?

                      Or I will have to make it one time after each thread finishes ?

                       

                      Just to complete such a case scenario and understand the full complexity. Thanks!

                      • 8. Re: can I avoid a service to block as7 startup ?
                        jason.greene

                        complete() needs to be called once when ALL work to indicate the service should be started as done. One way you can achieve what you describe though is to use an AtomicInteger shared by all tasks that is initialized to the number of tasks that must be ran, every task decrements and when the count hits 0 calls, that task calls context.complete().

                        1 of 1 people found this helpful
                        • 9. Re: can I avoid a service to block as7 startup ?
                          lmcdasi

                          Great ! I already started to think for a lot more complex stuff for the complete ....

                           

                          I'll ask one more question - hopefully the last one. Let's assume that I have a WAR file that needs that service to be up. I know that I can programatically add "dependecies" towards WAR depolyment. So far so good.

                           

                          My question is: does the AS postpone the WAR file deployment until the service completes OR it will deploy it and then the WAR file will miss-behave (meaning throwing some kind of exceptions due to lack of access to the "late" service) ?

                           

                          And I think I am going over the full exercice of understanding how AS works on that level. Thanks!

                          • 10. Re: can I avoid a service to block as7 startup ?
                            jason.greene

                            I'll ask one more question - hopefully the last one. Let's assume that I have a WAR file that needs that service to be up. I know that I can programatically add "dependecies" towards WAR depolyment. So far so good.

                             

                            My question is: does the AS postpone the WAR file deployment until the service completes OR it will deploy it and then the WAR file will miss-behave (meaning throwing some kind of exceptions due to lack of access to the "late" service) ?

                             

                            And I think I am going over the full exercice of understanding how AS works on that level. Thanks!

                             

                            It depends on what service you attach the dependency to. If you stick it on one of the deployment phase services, you will prevent deployment from completing. If you attach it to one of the services that executes the deployment (like for example a service that represntes some EE component) then only that service and anything depending on the service will not start.

                             

                            In all cases, if a service that was created during deployment was not started, the container will abort and rollback the deployment. That is unless the deployment operation uses a special option in the management request that tells it to allow incomplete deployments to survive.

                            • 11. Re: can I avoid a service to block as7 startup ?
                              lmcdasi

                              OK - I guess I understand. So for example if I do :

                               

                               

                               

                              context.addStep( new AbstractDeploymentChainStep() {

                                   protected void execute(DeploymentProcessorTarget processorTarget) {

                                        processorTarget.addDeploymentProcessor(Phase.DEPENDENCIES, Phase.DEPENDENCIES_MODULE, new MyDependencyProcessor());

                                   }

                              RUNTIME);

                              }, OperationContext.Stage.

                               

                              And in MyDependencyProcessor I do: 

                                  

                                   ModuleSpecification moduleSpecification = deploymentUnit.getAttachment(Attachments.

                              MODULE_SPECIFICATION);

                               

                               

                                   ModuleDependency dep = new ModuleDependency(Module.getBootModuleLoader(), myModule.getIdentifier(), false, false, false);

                               

                               

                                   dep.addExportFilter(PathFilters.getMetaInfFilter(),

                              true);

                               

                                   dep.addImportFilter(PathFilters.getMetaInfFilter(),

                              true);

                                   moduleSpecification.addSystemDependency(dep);

                               

                               

                               

                               

                               

                               

                              where myModule is the blocking service. Then if service still blocks at deployment, then all deployments would fail until the service is up, right ?

                               

                               

                               

                               

                              • 12. Re: can I avoid a service to block as7 startup ?
                                lmcdasi

                                I am getting a java.lang.IllegalStateException: Illegal controller state when the async call is executed. Let's say I have 2 modules (M1,M2) that I start from the same ME. M1 does not have any blocking op, but M2 does. The M1 module completes it's start ok.

                                But the M2 fails when calling async. The question is, can I do something like that ?

                                 

                                11:00:23,193 INFO  [<mypkg>.TestModuleImpl] (Controller Boot Thread) MEModule service started.
                                11:00:23,194 ERROR [stderr] (Controller Boot Thread) java.lang.IllegalStateException: Illegal controller state
                                11:00:23,196 ERROR [stderr] (Controller Boot Thread)    at org.jboss.msc.service.ServiceControllerImpl$StartContextImpl.asynchronous(ServiceControllerImpl.java:2158)
                                11:00:23,197 ERROR [stderr] (Controller Boot Thread)    at <mypkg>.AbstractModule.startAsync(AbstractModule.java:29) <-- context.asynchronous()
                                11:00:23,197 ERROR [stderr] (Controller Boot Thread)    at <mypkg>l.AsyncTestModuleImpl.start(AsyncTestModuleImpl.java:58)
                                11:00:23,197 ERROR [stderr] (Controller Boot Thread)    at <mypkg>.FrameworkBootstrapService.startModule(FrameworkBootstrapService.java:261)
                                11:00:23,197 ERROR [stderr] (Controller Boot Thread)    at <mypkg>.FrameworkBootstrapService.addModule(FrameworkBootstrapService.java:192)
                                11:00:23,198 ERROR [stderr] (Controller Boot Thread)    at <mypkg>.MEModuleAdd$1.execute(MEModuleAdd.java:62)
                                11:00:23,198 ERROR [stderr] (Controller Boot Thread)    at org.jboss.as.controller.OperationContextImpl.executeStep(OperationContextImpl.java:353)
                                11:00:23,198 ERROR [stderr] (Controller Boot Thread)    at org.jboss.as.controller.OperationContextImpl.doCompleteStep(OperationContextImpl.java:298)
                                11:00:23,198 ERROR [stderr] (Controller Boot Thread)    at org.jboss.as.controller.OperationContextImpl.completeStep(OperationContextImpl.java:223)
                                11:00:23,198 ERROR [stderr] (Controller Boot Thread)    at <mypkg>.MEModuleAdd$1.execute(MEModuleAdd.java:65)

                                • 13. Re: can I avoid a service to block as7 startup ?
                                  jason.greene

                                  Dan Sirbu wrote:

                                   

                                   

                                       moduleSpecification.addSystemDependency(dep);

                                   

                                   

                                   

                                   

                                   

                                  where myModule is the blocking service. Then if service still blocks at deployment, then all deployments would fail until the service is up, right ?

                                   

                                  No. A module dependency is NOT a service dependency. A module dependency has to do with linking of of classloading, and assumes that the dependency exists in some location. A service dependency controls start order, lifecycle control, and injection.

                                  • 14. Re: can I avoid a service to block as7 startup ?
                                    jason.greene

                                    Dan Sirbu wrote:

                                     

                                    I am getting a java.lang.IllegalStateException: Illegal controller state when the async call is executed. Let's say I have 2 modules (M1,M2) that I start from the same ME. M1 does not have any blocking op, but M2 does. The M1 module completes it's start ok.

                                    But the M2 fails when calling async. The question is, can I do something like that ?

                                    Only the service itself can call asynchronous in it's start or stop method. As mentioned above, it must be called before the service method returns.

                                    1 2 3 Previous Next