9 Replies Latest reply on May 28, 2013 12:18 PM by Mauricio Salatino

    Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?

    Guillermo L Newbie

      I'm starting to learn and use jBPM 5.4 and I'm wondering whether it is actually possible to get app beans references (CDI beans) and use them within script/service tasks? If so, is there any good reference documentation/tutorial to which you can point me in order to do this integration in the correct way? Any pattern to do this integration?

       

      Thanks for your help!

        • 1. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
          Mauricio Salatino Master

          Hi Guillermo L,

          I usually don't recommend using script tasks at all unless you are doing a proof of concept or something really simple. It's is usually a bad practice to embed java code inside an xml file, but the bpmn2 specification allows it.

          In jBPM6 we are making a huge effort to support CDI in all the framework, but in jBPM5.4 you can use it as well, you will need to do more work for sure. About service tasks, it shoudn't be so hard you can just implement a WorkItemHandler that access the CDI container to get the bean that you want.

           

          Cheers

          1 of 1 people found this helpful
          • 2. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
            vchmakov Newbie

            Hi Mauricio,

             

            Then what do you recommend instead of script tasks? What is the best practice in your opinion?

             

            Thanks,

             

            Vladimir

            • 3. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
              Guillermo L Newbie

              Hi Mauricio,

              I do not personally like programming within XMLs and that's why I was looking forward another way to get my code out of these scripts by getting CDI components from them where my java code could be located, since they appear to be the way to implementa automatic tasks :-(. 

               

              I'm a BPM and jBPM beginner so as Vladimir I'd like to get your recommendation for automatic tasks implementation. Service tasks appear to have been specified for other purposes but should we use them instead of script tasks? Other alternatives?

               

              I'd appreciate some more insight you may provide regarding this subject.

               

              Thanks,

              Guillermo

              • 4. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
                vchmakov Newbie

                Hi Mauricio,

                 

                I found answer to my question in “jBPM5 Developer Guide” book on page 143:

                “Business users do not commonly use script tasks, but they are really helpful for technical people. By adding script tasks to a process  we can easily modify the behavior of our processes without modifying any Java class. We can use this type of task to add logs, messages, or to perform data transformation tasks in our processes. As a rule of thumb, script tasks shouldn’t contain business logic inside them. Abstract tasks, human tasks, and service tasks are better place to implement this kind of logic.”

                 

                In our company we usually implement the business logic in plain classes and call methods from the script tasks.

                 

                Thanks,

                 

                Vladimir

                1 of 1 people found this helpful
                • 5. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
                  Guillermo L Newbie

                  Thanks for your response Vladimir!

                   

                  I've also found a thread from Esteban here and he does not recommend using script tasks.

                   

                  I understand that under some scenarios a script task might be an option, however I'm integrating jBPM within JEE 6 and my beans are CDI beans so I need to get references to them through the container. As per what I have read so far, it appears that Task or Service Task might be my alternatives since I can inject and bind the work item handlers into the session however I'd like to know the difference between both (Task vs ServiceTask).

                   

                  Thanks again for sharing your experience!

                  • 6. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
                    vchmakov Newbie

                    Hi Guillermo L,

                     

                    We do as well integrate jBPM with JEE6 and run it under JBoss AS 7. We have a set of services which we instantiate at startup and then pass into jBPM engine as a global variable. It is not strictly speaking necessary, though, as the service set container is a singleton and can be accessed from any participating class. One of the services is a persistence service. We receive commands via MDB. There is an injection point in the MDB for the persistence context. Every time a command is invoked we pass the injected instance to our persistence service and to the Drools/jBPM engine to make them participate in the container’s transaction. I guess that the similar approach can be used in your case as well with your CDI beans.

                     

                    Thanks,

                     

                    Vladimir

                    • 7. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
                      Guillermo L Newbie

                      I can now understand your approach and appears to be good for me. I'm running jBPM within a similar environment (JEE 6/JBoss 7). I guess my CDI beans can be injected as you did with your components as global variables and then invoke their functions from a script task avoiding writing java code within XMLs.

                       

                      Going back to tasks; do you know what is the actual difference between Tasks and Service Tasks?

                       

                      Thanks again for sharing your valuable experience!

                      • 8. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
                        vchmakov Newbie

                        Based on what I understood from reading of the “jBPM5 Developer Guide” book Service Task is designed for integrating when you run within SOA implementation. The main parameters are Interface and Operation. Operation is a method on the Interface to be invoked. The Abstract Task is an extension point where you define what object you wish to call and bind it via Task Name. The Task Name is used when you register a handler for your task.

                         

                        Hope this helps. Someone may want to correct me if I’m wrong.

                         

                        Thanks,

                         

                        Vladimir

                        • 9. Re: Is it possible to integrate CDI beans into script and service tasks within jBPM 5.4?
                          Mauricio Salatino Master

                          Vladimir you are correct. I'm happy to see that the book is helping.

                          I usually recommend using "Abstract Tasks" or just Task and a work item handler implementation associated to it to implement custom logic. Even using the script task for calling your own java classes is a bad and error prone practice in my experience. Services tasks are more SOA oriented as Vladimir mention but as far as I remember they can be also used to call a method in a bean as well.. so you can try that if you don't want to write your WorkItemHandler implementation..

                          Cheers