Exporting and Importing Workflows using XML
Using the Flux APIs, you can easily export your workflows to XML, then import them into a different environment.
This can be useful in a variety of situations – for example:
- Moving workflows from development to production.
 - Upgrading to a newer Flux release with a new database schema.
 - Migrating between databases (e.g., moving from MySQL to PostgreSQL)
 
NOTE: if you are running a secured engine, use only the code marked “Secured Engine” in the steps below. If you are using an unsecured engine, use the “Unsecured Engine” code instead.
The export/import process can be completed as follows (where source designates the existing Flux environment, and target designates the environment that data will be copied into):
- If it does not already exist, create a new database schema for the target environment.
 - Run the export code (see below) in the source environment. This will save all of the running workflow data to an XML file.
 - Run the export repository code in the source environment. This will save the repository to a separate XML file.
 - Make sure all Flux engines from the source environment are disposed.
 - Run the import running workflows code in the target environment.
 - Run the import repository code in the target environment.
 
Export Running Workflows
The following code exports all running workflows to an XML file:
import flux.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
public class ExportFlows {
    public static void main(String[] args) throws NotBoundException, EngineException, RemoteException, FileNotFoundException {
//    Look up the engine on the specified host and port, with the SSL setting given.
        Engine engine = Factory.makeInstance().lookupEngine("localhost", 7520, true);
        try {
//      Edit this to use a valid login for your engine.
//      If security is not enabled, comment out the line below, as well as the
//      try / finally block and the logout line.
            engine.login("admin", "admin");
            List workflows = new ArrayList();
            FlowChartIterator fcit = engine.get();
            try {
                while (fcit.hasNext()) {
                    workflows.add(fcit.next());
                }
            } finally {
                fcit.close();
            }
//      Export the workflows to XML - make sure to update
//      the path to a location on your own server.
            EngineHelper.makeXmlFromFlowCharts(workflows, new FileOutputStream("/path/to/workflows.xml"), true);
        } finally {
            engine.logout();
        }
    }
}
Import Running Workflows
The following code loads an XML file and imports all running workflows onto the target engine:
import flux.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;
public class ImportFlows {
public static void main(String[] args) throws NotBoundException, EngineException, RemoteException, FileNotFoundException {
//    Look up the engine on the specified host and port, with the SSL setting given.
Engine engine = Factory.makeInstance().lookupEngine("localhost", 7520, true);
try {
//      Edit this to use a valid login for your engine.
//      If security is not enabled, comment out the line below, as well as the
//      try / finally block and the logout line.
engine.login("admin", "admin");
//     Make sure to use the same file path that you previously exported to.
List<FlowChart> workflows = EngineHelper.makeFlowChartsFromXml(new FileInputStream("/path/to/workflows.xml"), true);
//      Add the flows to the engine.
for (FlowChart workflow : workflows) {
engine.put(workflow);
}
} finally {
engine.logout();
}
}
}
Export Repository Workflows
The following code exports all repository workflows to an XML file:
import flux.*;
import flux.repository.RepositoryAdministrator;
import flux.repository.RepositoryElement;
import flux.repository.RepositoryIterator;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
public class ExportFlows {
    public static void main(String[] args) throws NotBoundException, EngineException, RemoteException, FileNotFoundException {
//    Look up the engine on the specified host and port, with the SSL setting given.
        Engine engine = Factory.makeInstance().lookupEngine("localhost", 7520, true);
        try {
//      Edit this to use a valid login for your engine.
//      If security is not enabled, comment out the line below, as well as the
//      try / finally block and the logout line.
            engine.login("admin", "admin");
            List<FlowChart> workflows = new ArrayList<FlowChart>();
            RepositoryAdministrator repoAdmin = engine.getRepositoryAdministrator();
            RepositoryIterator repoIt = repoAdmin.get("/");
            try {
                while (repoIt.hasNext()) {
                    RepositoryElement repoEl = repoIt.next();
                    if (repoEl.isFlowChart()) {
                        workflows.add(repoEl.getFlowChart());
                    }
                }
            } finally {
                repoIt.close();
            }
//    Exoport the workflows to XML - make sure to update the path below to a
//    location on your own server.
            EngineHelper.makeXmlFromFlowCharts(workflows, new FileOutputStream("/path/to/repository-workflows.xml"), true);
        } finally {
            engine.logout();
        }
    }
}
Import Repository Workflows
The following code loads an XML file and imports all repository workflows from that file onto an engine:
import flux.*;
import flux.repository.RepositoryAdministrator;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;
public class ImportFlows {
    public static void main(String[] args) throws NotBoundException, EngineException, RemoteException, FileNotFoundException {
//    Look up the engine on the specified host and port, with the SSL setting given.
        Engine engine = Factory.makeInstance().lookupEngine("localhost", 7520, true);
        try {
//      Edit this to use a valid login for your engine.
//      If security is not enabled, comment out the line below, as well as the
//      try / finally block and the logout line.
            engine.login("admin", "admin");
//      Make sure to use the same file path that you previously exported to.
            List<FlowChart> workflows = EngineHelper.makeFlowChartsFromXml(new FileInputStream("/path/to/repository-workflows.xml"), true);
            RepositoryAdministrator repoAdmin = engine.getRepositoryAdministrator();
//      Add the flows to the engine.
            for (FlowChart workflow : workflows) {
//        Overwrites any existing workflows with the same name – set the second argument
//        to false if you do not want to overwrite.
                repoAdmin.put(workflow, true);
            }
        } finally {
            engine.logout();
        }
    }
}