freeflow4j examples

Compulsory Hello World Example

We can build a very simple "Hello, World!" workflow out of two WorkflowSteps. It might seem a bit strange to not see any linkage between your WorkflowSteps, but that's kind of the point:

StepOne.java

package net.sf.freeflow4j.examples;

import net.sf.freeflow4j.WorkflowProcessingContext;
import net.sf.freeflow4j.support.SimpleWorkflowStep;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StepOne extends SimpleWorkflowStep
{
    private static final Log LOG = LogFactory.getLog(StepOne.class);

    public WorkflowProcessingContext executeWorkflowStep(
        WorkflowProcessingContext ctx) throws Exception
    {
        LOG.debug("Preparing a very profound message...");
        
        ctx.put("msg", "Hello Workflow!");
        
        return ctx;
    }
}

StepTwo.java

package net.sf.freeflow4j.examples;

import net.sf.freeflow4j.WorkflowProcessingContext;
import net.sf.freeflow4j.support.SimpleWorkflowStep;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StepTwo extends SimpleWorkflowStep
{
    private static final Log LOG = LogFactory.getLog(StepTwo.class);
    
    public WorkflowProcessingContext executeWorkflowStep(
        WorkflowProcessingContext ctx) throws Exception
    {
        LOG.debug("Displaying message [" + ctx.get("msg") + "]");
        return ctx;
    }
}

Declarative Usage (with an IoC container like the Spring Framework)

It's fairly straight-forward to use freeflow4j. First configure your WorkflowSteps in a bean context:

<beans>

        <bean name="stepOne" class="net.sf.freeflow4j.examples.StepOne">
                <property name="nextStep" ref="stepTwo" />
        </bean>

        <bean name="stepTwo" class="net.sf.freeflow4j.examples.StepTwo">
                <!-- No 'nextStep' property need is set since this is the last step. -->
        </bean>

</beans>

Then to start the workflow you get a reference to the first WorkflowStep bean, initialize a WorkflowProcessingContext and kick it off:

package net.sf.freeflow4j.examples;

import net.sf.freeflow4j.WorkflowProcessingContext;
import net.sf.freeflow4j.WorkflowStep;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DeclarativeMain
{
    public static void main(String[] args)
    {        
        ApplicationContext appContext = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
        WorkflowStep stepOne = (WorkflowStep) appContext
            .getBean("stepOne");
        stepOne.process(new WorkflowProcessingContext());
    }
}

Programmatic Usage

You don't have to use an IoC container like Spring to hook up a workflow:

package net.sf.freeflow4j.examples;

import net.sf.freeflow4j.WorkflowProcessingContext;
import net.sf.freeflow4j.support.SimpleWorkflowStep;

public class ProgrammaticMain
{
    public static void main(String[] args)
    {
        SimpleWorkflowStep stepOne = new StepOne();
        stepOne.setNextStep(new StepTwo());
        stepOne.process(new WorkflowProcessingContext());
    }
}