Tuesday, April 7, 2009

Working with jBPM workflows in Alfresco - Part 3 : Alfresco scripts

In the previous blog entry: Working with jBPM Workflows in Alfresco - Part 1 : jBPM, we saw how to create a simple jBPM workflow and test it.

In a blog entry before that: Working with jBPM Workflows in Alfresco - Part 2 : Embedded Alf - SDK, we saw how to run our workflow in Alfresco Embedded using the SDK.

In this blog entry, we will add some Alfresco scripts into our workflow that make use of Alfresco APIs.

Step 1 - Logging in JavaScript via Alfresco logger

Starting simple, lets modify our process definition to print out a log message every time we transition to a new state. This is a handy technique if we're not sure our logic is going the way we think it should. Here is the updated processs definiton:
<?xml version="1.0" encoding="UTF-8"?>

<process-definition xmlns="urn:jbpm.org:jpdl-3.2" name="publishContentBasic">
<start-state name="start">
<transition name="to_requested" to="requested">
<action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
<script>
logger.log("Going to requested state");
</script>
</action>
</transition>
</start-state>

<state name="requested">
<transition to="processing" name="to_processing">
<action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
<script>
logger.log("Going to processing state");
</script>
</action>
</transition>
</state>

<state name="processing">
<transition to="succeeded" name="to_succeeded">
<action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
<script>
logger.log("Going to succeeded state");
</script>
</action>
</transition>
<transition to="failed" name="to_failed">
<action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
<script>
logger.log("Going to failed state");
</script>
</action>
</transition>
</state>

<state name="succeeded">
<transition to="end" name="to_end">
<action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
<script>
logger.log("Going to end state");
</script>
</action>
</transition>
</state>

<state name="failed">
<transition to="end" name="to_end">
<action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
<script>
logger.log("Finishing");
</script>
</action>
</transition>
</state>

<end-state name="end"></end-state>
</process-definition>
Here we see the new process containing an action element in the transition element calling out the org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript class. This class provides a java script environment with the Alfresco JavaScript API default objects instantiated and ready to use. One of which is the logger.
This action wraps a <script> element containing the javascript to run.

To view the results, first change the log4j.properties config to show the javascipt log messages (and reduce some of the noise).
# Set root logger level to DEBUG and its only appender to CONSOLE.
log4j.rootLogger=WARN, CONSOLE

# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %C{1} : %m%n

# LIMIT CATEGORIES
log4j.logger.org.jbpm=INFO
#log4j.logger.org.jbpm.graph=DEBUG
log4j.logger.com.sample=DEBUG
# Hibernate debugging levels and their output
log4j.logger.org.hibernate=WARN
#Log all SQL DML statements as they are executed
#log4j.logger.org.hibernate.SQL=TRACE
#Log all JDBC parameters
#log4j.logger.org.hibernate.type=TRACE
#Log all SQL DDL statements as they are executed
#log4j.logger.org.hibernate.tool.hbm2ddl=DEBUG
#Log the state of all entities (max 20 entities) associated with the session at flush time
#log4j.logger.org.hibernate.pretty=DEBUG
#Log all second-level cache activity
#log4j.logger.org.hibernate.cache=DEBUG
#Log transaction related activity
#log4j.logger.org.hibernate.transaction=DEBUG
#Log all JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=TRACE
#Log HQL and SQL ASTs and other information about query parsing
#log4j.logger.org.hibernate.hql.ast=DEBUG
#Log all JAAS authorization requests
#log4j.logger.org.hibernate.secure=DEBUG
#Log everything (a lot of information, but very useful for troubleshooting)
#log4j.logger.org.hibernate=DEBUG
#log4j.logger.org.hibernate.tools=DEBUG

log4j.logger.org.alfresco=INFO

log4j.logger.org.alfresco.repo.jscript=DEBUG



The last line: log4j.logger.org.alfresco.repo.jscript=DEBUG does the trick. Now run the PublishContentBasicProcessAlfTest as a JUnit test and see the results. You should get something like the following in the log:
20:42:22,574 [main] DEBUG ScriptResourceHelper : Imports resolved, adding resource '_root
20:42:22,761 [main] DEBUG ScriptLogger : Going to requested state
20:42:22,765 [main] DEBUG RhinoScriptProcessor : Time to execute script: 189ms
20:42:22,893 [main] DEBUG ScriptResourceHelper : Imports resolved, adding resource '_root
20:42:22,899 [main] DEBUG ScriptLogger : Going to processing state
20:42:22,901 [main] DEBUG RhinoScriptProcessor : Time to execute script: 6ms
20:42:22,936 [main] DEBUG ScriptResourceHelper : Imports resolved, adding resource '_root
20:42:22,942 [main] DEBUG ScriptLogger : Going to succeeded state
20:42:22,943 [main] DEBUG RhinoScriptProcessor : Time to execute script: 5ms
20:42:23,004 [main] DEBUG ScriptResourceHelper : Imports resolved, adding resource '_root
20:42:23,010 [main] DEBUG ScriptLogger : Going to end state
20:42:23,011 [main] DEBUG RhinoScriptProcessor : Time to execute script: 5ms

Lets do something more interesting. Supposing we wanted to create and/or update some content. The Alfresco JavaScipt API cookbook has an interesting example which I will steal for this purpose. Add the following javascript to the script element of the 'to_requested' transition:
<script>
logger.log("Going to requested state");
logger.log("trying to create file and make it versionable");

var doc = userhome.createFile("checkmeout.txt");
doc.addAspect("cm:versionable");
doc.content = "original text";
logger.log("created versionable doc with content '"+doc.content+"'");

var workingCopy = doc.checkout();
workingCopy.content = "updated text 1";

doc = workingCopy.checkin();

workingCopy = doc.checkout();
workingCopy.content = "updated text 2";

doc = workingCopy.checkin("a history note", true);
logger.log("checked doc out and in a couple of times");
</script>


In this example, we create a new item of content with some text and make it versionable. Then we check it out and updated it. And again checkout and update it, and on check in we use a comment.

Note: be careful as to what syntax you use in the script element. Since this appears in XML, we must escape any characters that can cause this script body to parse incorrectly (such as < and > characters). I had to remove the // comment lines to make this work correctly.

Step 2 - Running a Java class with Alfresco

Now we will configure to run alfresco java foundation API services.

END

Working with jBPM workflows in Alfresco - Part 2: Embedded Alf - SDK

This part focuses on extending the previous blog post: Working with Workflows in Alfresco - Part 1: jBPM to run as an Embedded Alf SDK project, and to configure and use Alfresco scripts as actions in the workflows.

Step 1 - Install SDK
Make sure you install the SDK according to the documentation on the Alfresco Wiki
Import the SDK projects into the same workspace you are using for this tutorial

Step 2 - Configure Project to Run with Embedded Alfresco
  1. Go to project properties > Java buid path > Projects tab and select 'add' to add SDK AlfrescoEmbedded
  2. From the SDK FirstFoundationClient, copy alfresco.extensions package from the 'source' source folder and paste into the 'src/main/config' source directory of your project (i.e. 'orchestration-example'). This will include the files 'custom-alfresco-shared.xml', 'custom-repository-context.xml' and 'custom-repository.properties'
  3. From the SDK FirstFoundationClient, copy the org.alfresco.sample package and paste into the src/main/java source folder of your project. This will give us something to test running as Alfresco Embedded
  4. Update the 'custom-repository.properties' to point dir.root to your Alfresco installation
  5. Remove the JBPM library from project properties, as it will conflict with the Alfresco version of these dependencies
  6. I recommend that you modify the log4j.properties to set the root logger to INFO, CONSOLE not DEBUG, CONSOLE or you will be watching the log messages for half the day
Step 3 - Test it out
Run the FirstFoundationClient as Java Application. There should be no 'red ink' and the line near the end of the log messages should read something like 'Alfresco started (Labs): Current version 3.0.0 (Stable 1526) schema 1002 - Installed version 3.1.0 (142) schema 1008'
If you see this line, likely you were able to run using Alfresco Embedded SDK

Step 4 - Create an Embedded Alf test

To test within the Alfresco context using Embedded approach, your workflow must be deployed, and you must run your test code, like the FirstFoundationClient example within a transaction service callback.
We will create a base test class, extending TestCase of JUnit 3 to simplify writing tests like this.
package com.sample;

import junit.framework.TestCase;

import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

public abstract class EmbeddedAlfTestBase extends TestCase {

Logger logger = Logger.getLogger(EmbeddedAlfTestBase.class);
protected ServiceRegistry serviceRegistry;
protected TransactionService transactionService;
protected ApplicationContext ctx;

public void setUp() throws Exception {
ctx = ApplicationContextHelper.getApplicationContext();
serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
transactionService = serviceRegistry.getTransactionService();
}

public void tearDown() throws Exception {
}

public void runTestInEmbeddedAlf(RetryingTransactionCallback<Object> testWork) throws Exception {
transactionService.getRetryingTransactionHelper().doInTransaction(testWork);
}
}
This code gets the application context and gets some services from Spring, including the serviceRegistry and the transactionService. The helper method, runtestInEmbeddedAlf takes a class of type RetryingtransactionCallback and runs it it using the transactionService's RetryingTransactionHelper doInTransaction(callback) method.

We will subclass this base test class and write our process test.
  1. Create a class PublishContentBasicProcessAlfTest, copying from PublishContentBasicProcessTest created in the previous blog entry.
  2. Change it to extend from EmbeddedAlfTestBase class
  3. Rename testSimpleProcess to a signature 'public void doTestSimpleProcess(ServiceRegistry serviceRegistry) throws Exception'
  4. Create a new testSimpleProcess like the following:
    public void testSimpleProcess() throws Exception {
RetryingTransactionCallback<Object> publishContentWFExistsCB = new RetryingTransactionCallback<Object>() {
public Object execute() throws Exception {
doTestSimpleProcess(serviceRegistry);
return null;
}
};
this.runTestInEmbeddedAlf(publishContentWFExistsCB);
}
Now run 'PublishContentBasicProcessAlfTest' as a JUnit test.
This should run as an alfresco project properly. But we are not leveraging Alfresco's services yet.

Step 5 - Test in Alf Using Workflow Service (foundation services)

What we are going to do:
  1. Authenticate and deploy in setUp for test
  2. Start workflow using workflow service
  3. Signal workflow using workflow service
Create a class PublishContentBasicProcessAlfTest extends EmbeddedAlfTestBase
To start, we will create a setUp() method to set us up for testing, like authenticating and deploying the service
    private WorkflowService workflowService;

public void setUp() throws Exception {
super.setUp();
workflowService = serviceRegistry.getWorkflowService();

authenticate("admin","admin");

deployDefinition(PROCESS_DEF_FILE);
}

We get the workflow service from the service registry (obtained in the setUp of the EmbeddedAlfTestBase class). Then we authenticate, and then deploy our process definition 'publichContentBasic/processdefinition.xml'.
Lets look at the methods we need to create:

authenticate method
    private void authenticate(String user, String password) {
AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
authenticationService.authenticate(user, password.toCharArray());

}

We get the AuthenticationService, and call authenticate. We need to do this first or else we wont be able to deploy our workflow.

deployDefinition method
    private WorkflowDeployment deployDefinition(String processDefName) {
//Deploy definition
String engineId = ENGINE_ID;
InputStream workflowDefinition = getClass().getResourceAsStream("/"+processDefName);
String mimeType = XML_MIMETYPE;
return workflowService.deployDefinition(engineId, workflowDefinition, mimeType);
}

We use the deployDefinition method of the workflow service. THis requires us to pass an engineId. this Id for jbpm is 'jbpm'. The mime type is 'text/xml'. The stream is obtained as a resource from this class loader by reading the 'publishContentBasic/processdefinition.xml' file.

Now we start the test like we did before, using the transactionservice and its callback:
    public void testSimpleProcess() throws Exception {
RetryingTransactionCallback<Object> publishContentWFExistsCB = new RetryingTransactionCallback<Object>() {
public Object execute() throws Exception {
doTestSimpleProcessInAlf();
return null;
}
};
this.runTestInEmbeddedAlf(publishContentWFExistsCB);

}

public void doTestSimpleProcessInAlf() throws Exception {

}


Ok. Now that we've got the test method ready. Lets put something in it. The first thing we have to do is start our workflow:
    public void doTestSimpleProcessInAlf() throws Exception {


NodeRef content = null;
String wfAssigneeName = "admin";
String workflowName = ENGINE_ID+"$"+PROCESS_DEF_NAME;

WorkflowPath wfPath = startWorkflow(content, wfAssigneeName, workflowName);
assertNotNull(wfPath);

assertEquals(
"Instance is in start state",
wfPath.node.name,
"start");

This calls the startWorkflow helper method with the content to go in the workflow package (currently null for testing purposes). We also need a name to assign the workflow to. We use 'admin'. And, finally we need the workflow name to start. Alfresco uses a naming convention prepending the engine id and a '$' to the beginning of BPM engine objects. The value for jbpm is 'jbpm'.

startWorkflow method
    private WorkflowPath startWorkflow(NodeRef content, String wfAssigneeName, String workflowName) throws Exception {
//Start workflow
NodeRef wfPackage = workflowService.createPackage(content );

PersonService personService = serviceRegistry.getPersonService();
NodeRef assigneeNodeRef = personService.getPerson(wfAssigneeName );

Map<QName, Serializable> workflowProps = new HashMap<QName, Serializable>(16);
workflowProps.put(WorkflowModel.ASSOC_PACKAGE, wfPackage);
workflowProps.put(WorkflowModel.ASSOC_ASSIGNEE, assigneeNodeRef);

// get the moderated workflow

WorkflowDefinition wfDefinition = workflowService.getDefinitionByName(workflowName );
if (wfDefinition == null) {
// handle workflow definition does not exist
throw new Exception("noworkflow: " + workflowName);
}

// start the workflow
WorkflowPath wfPath = workflowService.startWorkflow(wfDefinition.getId(), workflowProps);
return wfPath;
}
In this code, we create the package for the workflow that should contain the content we are workflowing. This is a content manager after all :). Next we get the user we will assign this workflow too. Since this process has no 'tasks', nothing will be noticable to the user. For this kind of process orchestration, the 'admin' user will do nicely. Perhaps in a real system, we want to create a special user just for back office processing like this?

Next we get the definition from the service using the given name 'jbpm$publishContentBasic'. Finally, we use the service to start the workflow, passing the definition and the workflow package. The startWorkflow service returns a WorkflowPath object. This corresponds to the workflow token of the execution of the workflow.

We can test this now to make sure we can deploy and start our workflow.

Next, we will signal the workflow to transition to the next state. This code snippet should be added to the end of the doTestSimpleProcessInAlf method
        // Move the process instance from its start state to the first state.
wfPath = signalTransition(wfPath, null);
assertEquals(
"Instance is in reqested state",
wfPath.node.name,
"requested");

Here we call the 'signalTransition' helper method passing the current path (token) and a transition name to take. Our helper method takes the first transition from the current state if no transition name is supplied.
signalTransition method
    private WorkflowPath signalTransition(WorkflowPath wfPath, String transitionName) throws Exception {
String wfPathId = wfPath.id;
WorkflowTransition[] wfTransitions = wfPath.node.transitions;
String wfTransitionId = null;
if (transitionName == null || transitionName.trim().length()==0) {
WorkflowTransition wfTransition = wfTransitions[0];
wfTransitionId = wfTransition.id;
} else {
int i = 0;
for (i = 0; i<wfTransitions.length; i++) {
if (wfTransitions[i].title.equals(transitionName)) break;
}
if (i > wfTransitions.length) throw new Exception("Failed to find transition with nanme '"+transitionName+"'");
WorkflowTransition wfTransition = wfTransitions[i];
wfTransitionId = wfTransition.id;
}

wfPath = workflowService.signal(wfPathId, wfTransitionId);
return wfPath;
}

Ok. This pattern can be repeated to transition to the next states to complete our test
        //move to processing state
wfPath = signalTransition(wfPath, null);
assertEquals(
"Instance is in processing state",
"processing",
wfPath.node.name);

//move to succeeded state
wfPath = signalTransition(wfPath, "to_succeeded");
assertEquals(
"Instance is in processing state",
"succeeded",
wfPath.node.name);

// Move the process instance to the end state. The configured action
// should execute again. The message variable contains a new value.
wfPath = signalTransition(wfPath, null);
assertEquals(
"Instance is in end state",
"end",
wfPath.node.name);
}



This completes the code to use the workflow service foundation client approach to deploy, start and signal our workflow.

Here is the complete code of the PublishContentBasicAlfTest
package com.sample;

import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.workflow.WorkflowModel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.workflow.WorkflowDefinition;
import org.alfresco.service.cmr.workflow.WorkflowDeployment;
import org.alfresco.service.cmr.workflow.WorkflowPath;
import org.alfresco.service.cmr.workflow.WorkflowService;
import org.alfresco.service.cmr.workflow.WorkflowTransition;
import org.alfresco.service.namespace.QName;

public class PublishContentBasicProcessAlfTest extends EmbeddedAlfTestBase {

private static final String XML_MIMETYPE = "text/xml";
private static final String PROCESS_DEF_FILE = "publishContentBasic/processdefinition.xml";
private static final String PROCESS_DEF_NAME = "publishContentBasic";
private static final String ENGINE_ID = "jbpm";

private WorkflowService workflowService;

public void setUp() throws Exception {
super.setUp();
workflowService = serviceRegistry.getWorkflowService();

authenticate("admin","admin");

deployDefinition(PROCESS_DEF_FILE);
}

public void tearDown() throws Exception {
super.tearDown();
}

public void testSimpleProcess() throws Exception {
RetryingTransactionCallback<Object> publishContentWFExistsCB = new RetryingTransactionCallback<Object>() {
public Object execute() throws Exception {
doTestSimpleProcessInAlf();
return null;
}
};
this.runTestInEmbeddedAlf(publishContentWFExistsCB);

}

public void doTestSimpleProcessInAlf() throws Exception {


NodeRef content = null;
String wfAssigneeName = "admin";
String workflowName = ENGINE_ID+"$"+PROCESS_DEF_NAME;

WorkflowPath wfPath = startWorkflow(content, wfAssigneeName, workflowName);
assertNotNull(wfPath);

assertEquals(
"Instance is in start state",
wfPath.node.name,
"start");

// Move the process instance from its start state to the first state.
wfPath = signalTransition(wfPath, null);
assertEquals(
"Instance is in reqested state",
wfPath.node.name,
"requested");

//move to processing state
wfPath = signalTransition(wfPath, null);
assertEquals(
"Instance is in processing state",
"processing",
wfPath.node.name);

//move to succeeded state
wfPath = signalTransition(wfPath, "to_succeeded");
assertEquals(
"Instance is in processing state",
"succeeded",
wfPath.node.name);

// Move the process instance to the end state. The configured action
// should execute again. The message variable contains a new value.
wfPath = signalTransition(wfPath, null);
assertEquals(
"Instance is in end state",
"end",
wfPath.node.name);
}

private WorkflowPath startWorkflow(NodeRef content, String wfAssigneeName, String workflowName) throws Exception {
//Start workflow
NodeRef wfPackage = workflowService.createPackage(content );

PersonService personService = serviceRegistry.getPersonService();
NodeRef assigneeNodeRef = personService.getPerson(wfAssigneeName );

Map<QName, Serializable> workflowProps = new HashMap<QName, Serializable>(16);
workflowProps.put(WorkflowModel.ASSOC_PACKAGE, wfPackage);
workflowProps.put(WorkflowModel.ASSOC_ASSIGNEE, assigneeNodeRef);

// get the moderated workflow

WorkflowDefinition wfDefinition = workflowService.getDefinitionByName(workflowName );
if (wfDefinition == null) {
// handle workflow definition does not exist
throw new Exception("noworkflow: " + workflowName);
}

// start the workflow
WorkflowPath wfPath = workflowService.startWorkflow(wfDefinition.getId(), workflowProps);
return wfPath;
}

private WorkflowDeployment deployDefinition(String processDefName) {
//Deploy definition
String engineId = ENGINE_ID;
InputStream workflowDefinition = getClass().getResourceAsStream("/"+processDefName);
String mimeType = XML_MIMETYPE;
return workflowService.deployDefinition(engineId, workflowDefinition, mimeType);
}

private WorkflowPath signalTransition(WorkflowPath wfPath, String transitionName) throws Exception {
String wfPathId = wfPath.id;
WorkflowTransition[] wfTransitions = wfPath.node.transitions;
String wfTransitionId = null;
if (transitionName == null || transitionName.trim().length()==0) {
WorkflowTransition wfTransition = wfTransitions[0];
wfTransitionId = wfTransition.id;
} else {
int i = 0;
for (i = 0; i<wfTransitions.length; i++) {
if (wfTransitions[i].title.equals(transitionName)) break;
}
if (i > wfTransitions.length) throw new Exception("Failed to find transition with nanme '"+transitionName+"'");
WorkflowTransition wfTransition = wfTransitions[i];
wfTransitionId = wfTransition.id;
}

wfPath = workflowService.signal(wfPathId, wfTransitionId);
return wfPath;
}

private boolean authenticate(String user, String password) {
AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
authenticationService.authenticate(user, password.toCharArray());
return authenticationService.authenticationExists(user);
}
}



In the next blog post, we will use alfresco scripts in the workflow definition we have just created. And finally, for orchestration, we will interact with JMS.

Working with jBPM workflows in Alfresco - Part 1: jBPM

The following set of blog articles will track my usage of jBPM in Alfresco to help orchestrate external content processing for publishing, and integrate with Review and Approval Advanced workflows.

Step 1 - Design
In order to collaborate with external publishing processes, we have chosen to use JMS topics. This approach offers robustness and flexibility in communication. The workflow described here will be an 'orchestration' workflow. Its responsibility is to call out external processes and monitor progress. This workflow will be associated to content, however, the content will be in the form of a 'work order' that is created when a user requests to publish a set of content. The user may view the workflow on this work order to see the progress of the external processes orchestrated by the workflow. Later on, we will integrate with the publish review and approve workflow where user tasks will be created to coordinate user activity.

Alfresco will be the container for the orchestration workflows. Actions in the workflow will be responsible for creating and sending messages to JMS topics to choreograph external publishing processes. A simulator will be used to act as the publisher processes for testing purposes. ActiveMQ is used as the JMS provider in these examples. There will also be a message handler created with the responsibi8lity of subscribing to topics that the publisher processes use to provide feedback and communicate status to the workflow. The following diagram represents this intended configuration.


Component descriptions:
  • Alfresco - contains jbpm implementation and content being workflowed
  • jBPM - library for jBPM accessed via the workflowService
  • process definition - jBPM process defining the process steps to orchestrate the publishing process
  • content event producer action - BPM action invoked from process to publish a content event message to the content topic
  • content event message - jms message indicating which content to publish and where to publish too
  • content event topic - jms topic storing the content event messages to be processed by the publisher
  • content event consumer - consumes content event messages and calls publishing services to invoke the publishing processes
  • publish process - implements the publishing functionality and calls publisher event producer to
  • publish event producer - creates and publishes publish event messages to the publish event topic indicating status of publishing
  • publish event consumer handler - consumes publish event messages and signals corresponding transitions on the publish process definition via the jbpm library

Step 2 - Environment Setup
  1. Install Alfresco SDK: follow instructions on the alfresco wiki
  2. Install ActiveMQ: follow the instructions on the ActiveMQ site
  3. Install jBPM (3.2.x): follow the instructions on the JBoss jBPM site: I installed jbpm-3.2.2 for best compatibility with Alfresco
  4. Install jBPM Process Designer
Note: compatibility problems. As of Alfresco 3.1, the jBPM 3.2.2 is supported. The only issue I had with this version is that there are no hibernate tasks for the Mail Node.

Step 3 - Create the Eclipse Project
For this project, I created a project called 'orchestration -example' using the jboss - jbpm process project' wizard.

Step 4 - Create the process
The following image shows the workflow configuration created to orchestrate and monitor the publishing process.
Workflow states:
  • Start - initial state when workflow is created
  • Requested - state indicating that a request was made to the publisher (the content event msg was sent to the content topic)
  • Processing - state indicating that the publisher has received the request and is processing
  • Succeeded - the publish process completed normally
  • Failed - the publish process failed
  • End - workflow has ended
Using this workflow, the state can be queried to indicate the status of the publishing process.
Here is the first version of this process definition:
<?xml version="1.0" encoding="UTF-8"?>

<process-definition xmlns="urn:jbpm.org:jpdl-3.2" name="publishContentBasic">
<start-state name="start">
<transition name="to_requested" to="requested">
<action name="action" class="com.sample.action.MessageActionHandler">
<message>Going to the first state - requested!</message>
</action>
</transition>
</start-state>

<state name="requested">
<transition to="processing" name="to_processing"></transition>
</state>

<state name="processing">
<transition to="succeeded" name="to_succeeded"></transition>
<transition to="failed" name="to_failed"></transition>
</state>

<state name="succeeded">
<transition to="end" name="to_end">
<action name="action" class="com.sample.action.MessageActionHandler">
<message>About to finish - succeeded!</message>
</action>
</transition>
</state>

<state name="failed">
<transition to="end" name="to_end">
<action name="action" class="com.sample.action.MessageActionHandler">
<message>About to finish - failed!</message>
</action>
</transition>
</state>

<end-state name="end"></end-state>
</process-definition>
This first configuration of the process is basic and simple. It will be embellished later with processing and business logic. This example uses the com.sample.action.MessageActionHandler class that is created by default using the 'create jbpm project' option in Eclipse.

Step 5 - The first test
In this first pass, we are using jBPM out of the box, without Alfresco. This mode allows us to configure and test our process in as much isolation as possible. Later we will use the Alfresco SDK to run as an Embedded Alfresco when we need Alfresco workflow services.

package com.sample;

import junit.framework.TestCase;

import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;

public class PublishContentBasicProcessTest extends TestCase {

public void testSimpleProcess() throws Exception {

// Extract a process definition from the processdefinition.xml file.
ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("publishContentBasic/processdefinition.xml");
assertNotNull("Definition should not be null", processDefinition);

// Create an instance of the process definition.
ProcessInstance instance = new ProcessInstance(processDefinition);
assertEquals(
"Instance is in start state",
instance.getRootToken().getNode().getName(),
"start");
assertNull(
"Message variable should not exist yet",
instance.getContextInstance().getVariable("message"));

// Move the process instance from its start state to the first state.
// The configured action should execute and the appropriate message
// should appear in the message process variable.
instance.signal();
assertEquals(
"Instance is in reqested state",
instance.getRootToken().getNode().getName(),
"requested");
assertEquals(
"Message variable contains message",
"Going to the first state - requested!",
instance.getContextInstance().getVariable("message"));

//move to processing state
instance.signal();
assertEquals(
"Instance is in processing state",
"processing",
instance.getRootToken().getNode().getName());

//move to succeeded state
instance.signal("to_succeeded");
assertEquals(
"Instance is in processing state",
"succeeded",
instance.getRootToken().getNode().getName());

// Move the process instance to the end state. The configured action
// should execute again. The message variable contains a new value.
instance.signal();
assertEquals(
"Instance is in end state",
"end",
instance.getRootToken().getNode().getName());
assertTrue("Instance has ended", instance.hasEnded());
assertEquals(
"Message variable contains message",
"About to finish!",
instance.getContextInstance().getVariable("message"));
}
}
Next Part, on to running workflows in Alfresco Embedded mode and running alfresco scripts in the workflow.


This