JBoss Community

Use JBPM 5 in a session bean with injected EntityManager

created by Carlos Ramirez in jBPM - View the full discussion

Hello I was able to use JBPM 5 with an injected EntityManager, but I had to search in source code and finally I reached to:

 

 

/**

* Session Bean implementation class WorkflowServiceBean

*/

@Stateful(name = IWorkflowService.SERVICE_NAME)

@Remote(IWorkflowService.class)

@Local(IWorkflowServiceLocal.class)

public class WorkflowServiceBean implements IWorkflowService,

        IWorkflowServiceLocal {

 

    @Resource

    private EJBContext context;

 

    @PersistenceContext(unitName = "org.jbpm.persistence.jpa")

    EntityManager entityManager;

 

    KnowledgeBase knowledgeBase;

 

    private StatefulKnowledgeSession ksession;

 

    /**

     * Default constructor.

     */

    public WorkflowServiceBean() {

        // TODO Auto-generated constructor stub

    }

 

    /**

     * {@inheritDoc}

     */

    public void init(byte[] processData) {

        try {

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory

.newKnowledgeBuilder();

            kbuilder.add(ResourceFactory.newByteArrayResource(processData),

ResourceType.BPMN2);

            this.knowledgeBase = kbuilder.newKnowledgeBase();

 

Environment env = KnowledgeBaseFactory.newEnvironment();

 

            MyPersistenceContextManager persistenceContextManager = new MyPersistenceContextManager();

 

            env = KnowledgeBaseFactory.newEnvironment();

env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER,

persistenceContextManager);

env.set(EnvironmentName.TRANSACTION_MANAGER,

new JtaTransactionManager(null, null, null));

 

            this.ksession = JPAKnowledgeService

.newStatefulKnowledgeSession(knowledgeBase, null,

env);

            int sessionId = ksession.getId();

 

        } catch (Exception e) {

            // TODO Auto-generated catch block

e.printStackTrace();

        }

    }

 

    @Remove

    void remove() {

        ksession.dispose();

 

    }

 

    class MyPersistenceContextManager implements PersistenceContextManager,

ProcessPersistenceContextManager {

 

        public org.drools.persistence.PersistenceContext getApplicationScopedPersistenceContext() {

            return new JpaPersistenceContext(entityManager);

        }

 

        public org.drools.persistence.PersistenceContext getCommandScopedPersistenceContext() {

            // TODO Auto-generated method stub

            return null;

        }

 

        public void beginCommandScopedEntityManager() {

            // TODO Auto-generated method stub

        }

 

        public void endCommandScopedEntityManager() {

            // TODO Auto-generated method stub

        }

 

        public void dispose() {

            // TODO Auto-generated method stub

        }

 

        public ProcessPersistenceContext getProcessPersistenceContext() {

            return new JpaProcessPersistenceContext(entityManager);

        }

 

    }

 

    public void startProcess(String processName) {

        ksession.startProcess(processName);

 

    }

 

}

 

My question is there is no way to make it simpler? I didn’t find anything in documentation, and just I am including it into a JBoss 5.1 application server. If I use the Jpa approach that is included in documentation, it creates two EntityManager, initializaes twice hibernate and it complains that the database conections were closed by hibernate instead of me

 

Regards,

Carlos

Reply to this message by going to Community

Start a new discussion in jBPM at Community