public synchronized void save() throws IOException {
    OutputStream os = null;
    try {
      File newSaved = new File(saved.getParentFile(), saved.getName() + ".new");
      File backupSaved = new File(saved.getParentFile(), saved.getName() + ".bak");
      os = new FileOutputStream(newSaved);
      marshaller.marshall(os, session);
      os.close();
      os = null;

      if (backupSaved.exists()) {
        if (!backupSaved.delete()) {
          throw new IOException("could not remove backup " + backupSaved.getAbsolutePath());
        }
      }
      if (saved.exists()) {
        if (!saved.renameTo(backupSaved)) {
          throw new IOException("could not backup " + saved.getAbsolutePath());
        }
      }
      if (!newSaved.renameTo(saved)) {
        backupSaved.renameTo(saved);
        throw new IOException("could not rename " + saved.getAbsolutePath());
      }

    } finally {
      if (os != null) os.close();
    }
  }
  public void test6() throws Exception {
    String process =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n"
            + "  xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
            + "  xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n"
            + "  type=\"RuleFlow\" name=\"ruleflow\" id=\"com.sample.ruleflow\" package-name=\"com.sample\" >\n"
            + "\n"
            + "    <header>\n"
            + "    </header>\n"
            + "\n"
            + "    <nodes>\n"
            + "      <start id=\"1\" name=\"Start\" />\n"
            + "      <timerNode id=\"4\" name=\"Timer\" delay=\"200\" />\n"
            + "      <end id=\"3\" name=\"End\" />\n"
            + "    </nodes>\n"
            + "\n"
            + "    <connections>\n"
            + "      <connection from=\"1\" to=\"4\" />\n"
            + "      <connection from=\"4\" to=\"3\" />\n"
            + "    </connections>\n"
            + "\n"
            + "</process>\n";
    final PackageBuilder builder = new PackageBuilder();
    builder.addProcessFromXml(new StringReader(process));
    final Package pkg = builder.getPackage();
    final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    ruleBase.addPackage(pkg);

    final StatefulSession session = ruleBase.newStatefulSession();

    new Thread(
            new Runnable() {
              public void run() {
                session.fireUntilHalt();
              }
            })
        .start();

    session.startProcess("com.sample.ruleflow", null);
    assertEquals(1, session.getProcessInstances().size());

    StatefulKnowledgeSession ksession =
        new StatefulKnowledgeSessionImpl((ReteooWorkingMemory) session);
    Marshaller marshaller = MarshallerFactory.newMarshaller(ksession.getKnowledgeBase());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshall(baos, ksession);
    byte[] b1 = baos.toByteArray();
    session.halt();
    session.dispose();
    Thread.sleep(400);

    ByteArrayInputStream bais = new ByteArrayInputStream(b1);
    final StatefulSession session2 =
        (StatefulSession) ((StatefulKnowledgeSessionImpl) marshaller.unmarshall(bais)).session;

    new Thread(
            new Runnable() {
              public void run() {
                session2.fireUntilHalt();
              }
            })
        .start();

    Thread.sleep(100);

    assertEquals(0, session2.getProcessInstances().size());
    session2.halt();
  }