[jboss-user] [jBPM] - Use JBPM 5 in a session bean with injected EntityManager
Carlos Ramirez
do-not-reply at jboss.com
Fri May 27 13:11:45 EDT 2011
Carlos Ramirez [http://community.jboss.org/people/crramirez] created the discussion
"Use JBPM 5 in a session bean with injected EntityManager"
To view the discussion, visit: http://community.jboss.org/message/607560#607560
--------------------------------------------------------------
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
[http://community.jboss.org/message/607560#607560]
Start a new discussion in jBPM at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110527/bfd2d6f9/attachment-0001.html
More information about the jboss-user
mailing list