3 Replies Latest reply on Sep 3, 2013 7:03 AM by Maciej Swiderski

    Taskservice in Spring: NullPointerException on JtaTransactionManager

    mikemike Newbie

      Hi there,

      I started a couple of days with Jbpm5 and run directly in the following problem:


      I have found the example below and could run it as a Junit test. All tables with users and groups have been created successfully. After that I created a Spring Service and posted that snippet into a @PostConstruct method. But now following Exception is always occurring. I have to say that my SpringContext is using the org.springframework.orm.hibernate4.HibernateTransactionManager as the PlatformTxMngr. But this should not be a Problem, if I preparing all resources programatically, right?


      I posted in Stackoverflow as well: http://stackoverflow.com/questions/18569038/taskservice-in-spring-nullpointerexception-on-jtatransactionmanager


      Thank you a lot for any help!!!

       

      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workflowServiceWrapper': Invocation of init method failed; nested exception is java.lang.NullPointerException

              at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:135)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1448)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

              at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

              at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)

              at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)

              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)

              at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)

              at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)

              at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)

              at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:388)

              at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)

              at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)

              at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)

              at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)

              at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

              at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

              at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

              at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)

              at java.util.concurrent.FutureTask.run(FutureTask.java:166)

              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)

              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)

              at java.lang.Thread.run(Thread.java:722)

      Caused by: java.lang.NullPointerException

              at org.drools.persistence.jta.JtaTransactionManager.getStatus(JtaTransactionManager.java:205)

              at org.jbpm.task.service.persistence.TaskJTATransactionManager.getStatus(TaskJTATransactionManager.java:72)

              at org.jbpm.task.service.persistence.TaskJTATransactionManager.begin(TaskJTATransactionManager.java:45)

              at org.jbpm.task.service.persistence.TaskPersistenceManager.beginTransaction(TaskPersistenceManager.java:100)

              at org.jbpm.task.service.persistence.TaskPersistenceManager.getUnescalatedDeadlines(TaskPersistenceManager.java:177)

              at org.jbpm.task.service.TaskServiceSession.scheduleUnescalatedDeadlines(TaskServiceSession.java:242)

              at org.jbpm.task.service.TaskService.initialize(TaskService.java:134)

              at org.jbpm.task.service.TaskService.initialize(TaskService.java:119)

              at org.jbpm.task.service.TaskService.<init>(TaskService.java:97)

       

      The Snippet:

      @Test
        public void startDemo() {
        
          Properties driverProperties = new Properties();
          driverProperties.put("driverClassName", "driverName");
          driverProperties.put("user", "username");
          driverProperties.put("password", "password");
          driverProperties.put("url", "url");  
        
          //JNDI Support to make DataSources for TransactionManager available
          System.getProperties().put("java.naming.factory.initial", "bitronix.tm.jndi.BitronixInitialContextFactory");
      
          //JPA/JTA/JNDI compatible DataSource
          PoolingDataSource ds = new PoolingDataSource();
          ds.setUniqueName("jdbc/BitronixJTADataSource"); // => referenced by persistence.xml
          ds.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource");
          ds.setMaxPoolSize(3);
          ds.setAllowLocalTransactions(true);
          ds.setDriverProperties(driverProperties);
          ds.init();
      
          EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.task");
          TaskService taskService = new TaskService(emf, SystemEventListenerFactory.getSystemEventListener());
          TaskServiceSession taskSession = taskService.createSession();
        
          // Add demo users
          Map vars = new HashMap();
          InputStream usersin = DemoTaskService.class.getResourceAsStream("LoadUsers.mvel");
          if(usersin != null) {
            Reader reader = new InputStreamReader(usersin);
            @SuppressWarnings("unchecked")
            Map<String, User> users = (Map<String, User>) TaskService.eval(reader, vars);
            for(User user : users.values()) {
              taskSession.addUser(user);
            }
          }
          //add demo groups
          InputStream groupsin = DemoTaskService.class.getResourceAsStream("LoadGroups.mvel");
          if(groupsin != null) {
            Reader reader = new InputStreamReader(groupsin);
            @SuppressWarnings("unchecked")
            Map<String, Group> groups = (Map<String, Group>) TaskService.eval(reader, vars);
            for(Group group : groups.values()) {
              taskSession.addGroup(group);
            }
          }
      
          // start mina server
          MinaTaskServer server = new MinaTaskServer(taskService, 5153);
          Thread thread = new Thread(server);
          thread.start();
          taskSession.dispose();
          System.out.println("Task service started correctly!");
          System.out.println("Task service running ...");
      
          KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
          kbuilder.add(ResourceFactory.newClassPathResource("sample.bpmn"), ResourceType.BPMN2);
          KnowledgeBase kbase = kbuilder.newKnowledgeBase();
          StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
          MinaHTWorkItemHandler humanTaskHandler = new MinaHTWorkItemHandler(ksession);
          humanTaskHandler.setIpAddress("127.0.0.1");
          humanTaskHandler.setPort(5153);
          ksession.getWorkItemManager().registerWorkItemHandler("Human Task", humanTaskHandler);
          Map<String, Object> params = new HashMap<String, Object>();
      
          ksession.startProcess("com.sample.bpmn.hello", params);
          ksession.dispose();
      
          taskSession = taskService.createSession();
          List<TaskSummary> list = taskSession.getTasksOwned("krisv", "en-UK");
      
          System.out.println("Taskinstance: " + list.size());
          taskSession.dispose();
      
        }
      
      
      



      persistence.xml

       

      <persistence>
        </persistence-unit>
        <persistence-unit name="org.jbpm.task">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <jta-data-source>jdbc/BitronixJTADataSource</jta-data-source>
      
          <mapping-file>META-INF/Taskorm-JPA2.xml</mapping-file>
      
          <class>org.jbpm.task.Attachment</class>
          <class>org.jbpm.task.BooleanExpression</class>
          <class>org.jbpm.task.Comment</class>
          <class>org.jbpm.task.Content</class>
          <class>org.jbpm.task.Deadline</class>
          <class>org.jbpm.task.Delegation</class>
          <class>org.jbpm.task.EmailNotification</class>
          <class>org.jbpm.task.EmailNotificationHeader</class>
          <class>org.jbpm.task.Escalation</class>
          <class>org.jbpm.task.Group</class>
          <class>org.jbpm.task.I18NText</class>
          <class>org.jbpm.task.Notification</class>
          <class>org.jbpm.task.OnAllSubTasksEndParentEndStrategy</class>
          <class>org.jbpm.task.OnParentAbortAllSubTasksEndStrategy</class>
          <class>org.jbpm.task.PeopleAssignments</class>
          <class>org.jbpm.task.Reassignment</class>
          <class>org.jbpm.task.Status</class>
          <class>org.jbpm.task.SubTasksStrategy</class>
          <class>org.jbpm.task.Task</class>
          <class>org.jbpm.task.TaskData</class>
          <class>org.jbpm.task.User</class>
      
          <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="hibernate.max_fetch_depth" value="3" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup" />
            <!-- BZ 841786: AS7/EAP 6/Hib 4 uses new (sequence) generators which seem to cause problems -->
            <property name="hibernate.id.new_generator_mappings" value="false" />
         
          </properties>
        </persistence-unit>
      </persistence>
      
      
      
        • 1. Re: Taskservice in Spring: NullPointerException on JtaTransactionManager
          Maciej Swiderski Master

          the NPE is due to UserTransaction was not found so please make sure that bitronix is properly initialized and binds the UserTransaction into JNDI. Moreover since you would like to use JTA please set transaction type to JTA in your persistence.xml otherwise it defaults to resource local.

          Not sure that this configures the property properly (could be it returns copy of the actual properties):

          System.getProperties().put("java.naming.factory.initial", "bitronix.tm.jndi.BitronixInitialContextFactory"); 
          
          

          you could try to use this instead:

          System.setProperty("java.naming.factory.initial", "bitronix.tm.jndi.BitronixInitialContextFactory"); 
          
          

          or use jndi.properties on classpath (in case of maven in src/test/resources)

           

          HTH

          • 2. Re: Taskservice in Spring: NullPointerException on JtaTransactionManager
            mikemike Newbie

            It seems that you can only use transaction-type="JTA", if yor JTA TxMgr is configured inside the Application Server. If you would like to use the JTATxMgr embedded, there is no way around  transaction-type="RESOURCE_LOCAL" . But don’t forget to explain hibernate the situation: <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.NoJtaPlatform" />

            Here you can find the other possible strategies: http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch07.html#services-JtaPlatform

             

            My new persistence unit:

            <persistence-unit name="org.jbpm.task" transaction-type="RESOURCE_LOCAL">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>jdbc/BitronixJTADataSource</jta-data-source>
            
                <mapping-file>META-INF/Taskorm-JPA2.xml</mapping-file>
            
                <class>org.jbpm.task.Attachment</class>
                <class>org.jbpm.task.BooleanExpression</class>
                <class>org.jbpm.task.Comment</class>
                <class>org.jbpm.task.Content</class>
                <class>org.jbpm.task.Deadline</class>
                <class>org.jbpm.task.Delegation</class>
                <class>org.jbpm.task.EmailNotification</class>
                <class>org.jbpm.task.EmailNotificationHeader</class>
                <class>org.jbpm.task.Escalation</class>
                <class>org.jbpm.task.Group</class>
                <class>org.jbpm.task.I18NText</class>
                <class>org.jbpm.task.Notification</class>
                <class>org.jbpm.task.OnAllSubTasksEndParentEndStrategy</class>
                <class>org.jbpm.task.OnParentAbortAllSubTasksEndStrategy</class>
                <class>org.jbpm.task.PeopleAssignments</class>
                <class>org.jbpm.task.Reassignment</class>
                <class>org.jbpm.task.Status</class>
                <class>org.jbpm.task.SubTasksStrategy</class>
                <class>org.jbpm.task.Task</class>
                <class>org.jbpm.task.TaskData</class>
                <class>org.jbpm.task.User</class>
            
                <properties>
                  <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
                  <property name="hibernate.max_fetch_depth" value="3" />
                  <property name="hibernate.hbm2ddl.auto" value="create" />
                  <property name="hibernate.show_sql" value="false" />
                  <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.NoJtaPlatform" />
                  <property name="hibernate.id.new_generator_mappings" value="false" />
                </properties>
            </persistence-unit>
            
            


            Can somebody explain how JUnit does the lookup?

            • 3. Re: Taskservice in Spring: NullPointerException on JtaTransactionManager
              Maciej Swiderski Master

              in fact you can use JTA transaction type in unit testing, that is how most of jbpm code base tests look like. Moreover it is required to be able to use LocalTaskService that joins the same transaction that runtime engine uses. Here you can find an example of persistence.xml that uses JTA transaction type.

               

              P.S.

              Does that mean that changing the jta platform fixed your issue?