[jboss-user] [JBoss jBPM] - Re: Solution: JBPM3 and Spring Integration

mr_magoo do-not-reply at jboss.com
Wed Aug 19 19:23:59 EDT 2009


The next issue is the extension of the jbpm configuration object.

In this example I would like my configuration to autodeploy my process definitions at startup and fail with warnings if something is not right. I would also like the option of safely starting the jobexecutor AFTER everything is deployed and safe for processing jobs that may have already been queued up. 
Notes: 
The object factory being injected is our own custom one that I will talk about next.

The line: 
JbpmConfiguration.Configs.setDefaultObjectFactory(objectFactory);
Is necessary because I noticed jbpm attempting to use the default factory if it was not set. This step was the one that was preventing me from completely removing all dependency on an xml file and caused some subtle transaction bugs. 
A bit random to be honest and I have not looked deeply into why it is trying to undermine my config behind my back. Nasty little troll...

JbpmSpringDelegationNode.beanFactory = applicationContext;
This is the bit of code I am most unhappy with. Basically there is no way currently (unlike in jbpm4) to hook the process def lookup to the spring beanfactory. So we have to inject it manually.
The new Spring 2.5/3.0 configuration annotations could be used here to force injection of properties, but we do not use them. Until then...yuck.

public class JbpmConfigurationFactoryBeanImpl implements ApplicationContextAware, FactoryBean, InitializingBean, ApplicationListener {
  |     private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(JbpmConfigurationFactoryBeanImpl.class);
  | 
  |     private JbpmConfiguration jbpmConfiguration;
  |     private TransactionTemplate transactionTemplate;
  |     private ObjectFactory objectFactory;
  |     private boolean startJobExecutor;
  |     private SessionFactory sessionFactory;
  | 
  |     private Resource[] processDefinitionsResources;
  | 
  |     @Override
  |     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  |         JbpmSpringDelegationNode.beanFactory = applicationContext;
  |     }
  | 
  |     public void afterPropertiesSet() throws Exception {
  |         JbpmConfiguration.Configs.setDefaultObjectFactory(objectFactory);
  |         jbpmConfiguration = new JbpmConfiguration(objectFactory);
  |         JbpmContext ctx = null;
  |         try {
  |             ctx = jbpmConfiguration.createJbpmContext();
  |             ctx.setSessionFactory(sessionFactory);
  |         } finally {
  |             if (ctx != null) {
  |                 ctx.close();
  |             }
  |         }
  |         if (getProcessDefinitionsResources() != null) {
  |             InputStream inputStream = null;
  |             for (int i = 0; i < getProcessDefinitionsResources().length; i++) {
  |                 try {
  |                     ctx = jbpmConfiguration.createJbpmContext();
  |                     Resource definitionLocation = getProcessDefinitionsResources();
  |                     inputStream = definitionLocation.getInputStream();
  |                     final ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(inputStream);
  |                     final JbpmContext finalContext = ctx;
  | 
  |                     getTransactionTemplate().execute(new TransactionCallback() {
  |                         @Override
  |                         public Object doInTransaction(TransactionStatus status) {
  |                             finalContext.deployProcessDefinition(processDefinition);
  |                             return null;
  |                         }
  |                     });
  |                 } finally {
  |                     if (inputStream != null) {
  |                         inputStream.close();
  |                     }
  |                     if (ctx != null) {
  |                         ctx.close();
  |                     }
  |                 }
  |             }
  |         }
  |         StaleObjectLogConfigurer.hideStaleObjectExceptions();
  |     }
  |     public Object getObject() throws Exception {
  |         return jbpmConfiguration;
  |     }
  |     @Override
  |     public Class getObjectType() {
  |         return JbpmConfiguration.class;
  |     }
  |     @Override
  |     public boolean isSingleton() {
  |         return true;
  |     }
  |     @Override
  |     public void onApplicationEvent(ApplicationEvent applicationEvent) {
  |         if (applicationEvent instanceof ContextClosedEvent) {
  |             jbpmConfiguration.getJobExecutor().stop();
  |         }
  |         if (applicationEvent instanceof ContextStartedEvent) {
  |             if (startJobExecutor) {
  |                 log.info("Starting job executor ...");
  |                 jbpmConfiguration.startJobExecutor();
  |                 log.info("Job executor started.");
  |             }
  |         }
  |     }
  |     public void setStartJobExecutor(boolean startJobExecutor) {
  |         this.startJobExecutor = startJobExecutor;
  |     }
  |     public void setObjectFactory(ObjectFactory objectFactory) {
  |         this.objectFactory = objectFactory;
  |     }
  |     public void setSessionFactory(SessionFactory sessionFactory) {
  |         this.sessionFactory = sessionFactory;
  |     }
  |     public Resource[] getProcessDefinitionsResources() {
  |         return processDefinitionsResources;
  |     }
  |     public void setProcessDefinitionsResources(Resource[] processDefinitionsResources) {
  |         this.processDefinitionsResources = processDefinitionsResources;
  |     }
  |     public TransactionTemplate getTransactionTemplate() {
  |         return transactionTemplate;
  |     }
  |     public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
  |         this.transactionTemplate = transactionTemplate;
  |     }
  | }

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4250513#4250513

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4250513



More information about the jboss-user mailing list