如何在桌面应用程序中设置jBPM?

时间:2015-07-30 05:47:52

标签: java persistence desktop-application jbpm

我想要的是什么:

我正在创建一个原型,它应该读取bpmn2文件并列出其所有任务。

我正在使用的是什么:

目前我有一个使用jBPM 6的简单Maven应用程序。我不需要任何应用程序服务器,工作台等,因为它只是一个简单的桌面应用程序。

问题:

如果我像这样初始化引擎(as suggested by the official documentation):

RuntimeEnvironment environment = 
                RuntimeEnvironmentBuilder.Factory.get()
                .newDefaultInMemoryBuilder()
                .persistence(false)
                .addAsset(ResourceFactory.newClassPathResource("poc4.bpmn2"), ResourceType.BPMN2)
                .get();

我收到此错误:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named org.jbpm.persistence.jpa
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at org.jbpm.runtime.manager.impl.DefaultRuntimeEnvironment.init(DefaultRuntimeEnvironment.java:72)
    at org.jbpm.runtime.manager.impl.RuntimeEnvironmentBuilder.get(RuntimeEnvironmentBuilder.java:314)
    at org.jbpm.runtime.manager.impl.RuntimeEnvironmentBuilder.get(RuntimeEnvironmentBuilder.java:56)
    at com.test.jbpmpoc.main(App.java:11)

请注意,我正在尝试使用明确禁用的持久性来启动引擎,但我认为不需要,因为文档说the engine does NOT use persistence by default

我所知道的:

根据我的研究,至少有两种初始化引擎的方法。

  1. 使用提供运行时环境的构建器 - 我已经在做了。这似乎是the most recommended way of doing it,因为默认情况下会初始化所有服务。但是,如上所述herehere,如果您想使用任务(这是我做的事情),此方法需要设置持久性;

  2. 手动初始化所​​有内容。我不喜欢这个,因为运行一个简单的应用程序听起来太费劲了。

  3. 使用其他运行时环境构建器。

  4. 我尝试过:

    首先,我尝试使用persistence.xml suggested by the documentation,但在创建EntityManagerFactory时出现了不同的错误:

    Exception in thread "main" javax.persistence.PersistenceException: Unable to build entity manager factory
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:83)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
        at com.test.jbpmpoc.App.main(App.java:30)
    Caused by: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [jdbc/jbpm-ds]
        at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:141)
        at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:112)
        at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
        at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
        at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
        at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
        ... 3 more
    Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
        at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.getNameParser(Unknown Source)
        at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:135)
        ... 20 more
    

    我没有使用应用程序服务器,也没有JTA数据源和ORM文件,所以我注释了指向这些文件的行以修复此JNDI错误,然后我得到了另外一个例外:

    Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.NamedQuery.lockMode()Ljavax/persistence/LockModeType;
        at org.hibernate.cfg.annotations.QueryHintDefinition.determineLockOptions(QueryHintDefinition.java:138)
        at org.hibernate.cfg.annotations.QueryBinder.bindQuery(QueryBinder.java:72)
        at org.hibernate.cfg.annotations.QueryBinder.bindQueries(QueryBinder.java:219)
        at org.hibernate.cfg.AnnotationBinder.bindQueries(AnnotationBinder.java:374)
        at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:618)
        at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
        at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
        at com.test.jbpmpoc.App.main(App.java:30)
    

    我很确定我离此更近了。如果我在应用程序服务器外运行,是否真的需要JTA数据源和ORM文件? jBPM安装程序没有这些,我在哪里可以找到它们?

    我还尝试使用空构建器而不是其中一个默认构建器,因此我需要手动初始化所​​有内容,包括TaskService,我相信如果我想管理任务,我需要启动它(list,execute等)。但是,它也需要persistence.xml,因为我不断收到No Persistence provider消息。

    修改

    我之前应该猜到NoSuchMethodError异常是由错误版本的Maven工件引起的。我还在我的持久性定义中使用JTA作为我的事务策略,但是没有提供任何jta数据源。但是,现在我已经更改了我的pom.xml和persistence.xml,我收到了这个错误:

    Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: org.jbpm.persistence.jpa] Unable to build EntityManagerFactory
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900)
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
        at com.test.jbpmpoc.App.main(App.java:30)
    Caused by: org.hibernate.HibernateException: Errors in named queries: GetProcessInstanceIdByCorrelation
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:435)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:891)
        ... 4 more
    

    似乎JBPMorm.xml文件中的一个命名查询是错误的。我从JBPM source code复制了这个文件。

    有关我的Java项目的更多信息:

    我发现的所有示例和教程都专注于创建在应用程序服务器上运行的Web应用程序,但在我的情况下,缺少应用程序服务器需要一个我仍未找到的解决方法。 是否有人设法使用jBPM 6运行简单的桌面应用程序?

1 个答案:

答案 0 :(得分:3)

我可能没有直接回答你的问题,但我觉得以下答案可以指导你找到解决方案。

我从问题的推论是,基本上我们想要以编程方式(没有像数据库这样的外部依赖关系)创建jBPM运行时并对其进行操作。

我建议你阅读用户指南 - 第6.8节。测试可以帮助您测试流程,测试流程是否在特定用例中按预期运行,

http://docs.jboss.org/jbpm/v6.2/userguide/jBPMBPMN2.html#d0e4026

文档建议您扩展JbpmJUnitBaseTestCase并创建junit测试用例。 JbpmJUnitBaseTestCase类将逻辑抽象为createRuntimeManager,assertProcessInstanceCompleted等...您可以引用或重用此类来构建应用程序。

请参阅https://github.com/droolsjbpm/jbpm/blob/master/jbpm-test/src/main/java/org/jbpm/test/JbpmJUnitBaseTestCase.java