[jboss-user] [jBPM] - Re: jBPM 4 - Deploy only if ProcessDefinition changed or new

Andrea Castangelo do-not-reply at jboss.com
Wed Nov 10 04:35:00 EST 2010


rapa [http://community.jboss.org/people/rapa] created the discussion

"Re: jBPM 4 - Deploy only if ProcessDefinition changed or new"

To view the discussion, visit: http://community.jboss.org/message/570435#570435

--------------------------------------------------------------
Thanks Michael, that worked.

I have to admit that I would have been happier to use a cleaner solution provided by the api, but well...
Anyway, if anyone is interested: here is the solution to deploy a process if it is new or it has changed.

Method executed at startup in the Jbpm class:

/**
     * Deploys all changed or new process definitions.
     */
    private void deployProcessDefinitionsIfNewOrChanged() {
 
        // Maps process definitions XML to resource names for the latest deployment of each process.
        this.processDefinitionsXMLbyResourceName = this.processEngine.execute(new GetDeploymentXmlMapCmd());
 
        String currentDeploymentId = null;
        for (String processDefinition : this.processDefinitions) {
            
            currentDeploymentId = deployIfNewOrChanged(processDefinition);
            if (currentDeploymentId != null) {
                info("Deployed process definition {0} with id {1}", processDefinition, currentDeploymentId);
            } else {
                info("Process definition {0} did not change from last deployment.", processDefinition);
            }
            currentDeploymentId = null;
        }
    }
    
    /**
     * Deploys a process definition if it is new or it has been modified since the current deployment.
     * @param resourceName
     * @return the deployment id of the deployed process definition or null if the no deployment was needed.
     */
    private String deployIfNewOrChanged(String resourceName) {
 
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
 
        // Get the content of the process definition XML file from the resource path
        DeploymentImpl newDeployment = (DeploymentImpl) repositoryService.createDeployment()
            .addResourceFromClasspath(resourceName);
        String newXml = new String(newDeployment.getBytes(resourceName));
 
        // Get the currently deployed process definition (gets the latest version)
        String currentXml = this.processDefinitionsXMLbyResourceName.get(resourceName);
        
        if(currentXml != null ? !newXml.equals(currentXml) : true) {
            return newDeployment.deploy();
        }
 
        return null;
    }
   /**
     * Deploys all changed or new process definitions.
     */
    private void deployProcessDefinitionsIfNewOrChanged() {
 
        // Maps process definitions XML to resource names for the latest deployment of each process.
        this.processDefinitionsXMLbyResourceName = this.processEngine.execute(new GetDeploymentXmlMapCmd());
 
        String currentDeploymentId = null;
        for (String processDefinition : this.processDefinitions) {
 
            currentDeploymentId = deployIfNewOrChanged(processDefinition);
            if (currentDeploymentId != null) {
                info("Deployed process definition {0} with id {1}", processDefinition, currentDeploymentId);
            } else {
                info("Process definition {0} did not change from last deployment.", processDefinition);
            }
            currentDeploymentId = null;
        }
    }
 
    /**
     * Deploys a process definition if it is new or it has been modified since the current deployment.
     * @param resourceName
     * @return the deployment id of the deployed process definition or null if the no deployment was needed.
     */
    private String deployIfNewOrChanged(String resourceName) {
 
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
 
        // Get the content of the process definition XML file from the resource path
        DeploymentImpl newDeployment = (DeploymentImpl) repositoryService.createDeployment()
            .addResourceFromClasspath(resourceName);
        String newXml = new String(newDeployment.getBytes(resourceName));
 
        // Get the currently deployed process definition (gets the latest version)
        String currentXml = this.processDefinitionsXMLbyResourceName.get(resourceName);
 
        if(currentXml != null ? !newXml.equals(currentXml) : true) {
            return newDeployment.deploy();
        }
 
        return null;
    }


GetDeploymentXmlMapCmd class

public class GetDeploymentXmlMapCmd implements Command<Map<String, String>> {
 
    private static final long serialVersionUID = 1L;
 
    @Override
    @SuppressWarnings("unchecked")
    public Map<String, String> execute(Environment environment) throws Exception {
        DbSessionImpl dbSessionImpl = environment.get(DbSessionImpl.class);
        Session session = dbSessionImpl.getSession();
 
        // Gets the the process definition key property for the latest deployment of each process
        List<DeploymentProperty> dps = session
            .createQuery("select d from DeploymentProperty d where d.key=:key1 and d.deployment.dbid in " +
                      "(select max(di.deployment.dbid) from DeploymentProperty di where di.key=:key2 " +
                      "group by di.stringValue)")
            .setParameter("key1", "pdkey")
            .setParameter("key2", "pdkey")
            .list();
        
        // Creates the map
        Map<String, String> processDefinitionsXMLbyResourceName = new HashMap<String, String>();
        DeploymentImpl deployment;
        String resourceName, xml;
        for(DeploymentProperty dp : dps) {
            deployment = dp.getDeployment();
            if(deployment.getResourceNames().size() > 0) {
                // all deployments always contain 1 resource name
                resourceName = deployment.getResourceNames().iterator().next();
                xml = new String(deployment.getBytes(resourceName));
                processDefinitionsXMLbyResourceName.put(resourceName, xml);
            }
        }
        return processDefinitionsXMLbyResourceName;
    }
    
}



Cheers!
Andrea
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/570435#570435]

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/20101110/b6363dec/attachment-0001.html 


More information about the jboss-user mailing list