Example #1
0
  @Test
  public void run() throws TestException {
    AtomicObject foo = null;
    Uid u = null;

    if (u == null) foo = new AtomicObject();
    else foo = new AtomicObject(u);

    AtomicAction A = new AtomicAction();

    int value = foo.get();
    try {
      A.begin();

      foo.set(foo.get() + 2);

      A.commit();

      assertEquals(value + 2, foo.get());

    } catch (Exception e) {
      A.abort();
      fail("AtomicObject exception raised.");
    }

    System.out.println("\nWill now try some erroneous conditions.\n");

    AtomicAction B = new AtomicAction();

    u = new Uid();
    foo = new AtomicObject(u);

    B.begin();

    try {
      System.out.println("attempting to get value from non-existent object: " + foo.get());
    } catch (Exception e) {
    }

    System.out.println("trying to set value to 5");

    try {
      foo.set(5);
    } catch (Exception e) {
    }

    try {
      System.out.println("attempting to get value again: " + foo.get());
    } catch (Exception e) {
    }

    B.commit();
  }
  @Test
  public void testOrderedOnePhase() throws Exception {
    System.setProperty("com.arjuna.ats.arjuna.common.propertiesFile", "jbossts-properties.xml");

    Uid firstToCommit = new Uid();
    Uid secondToCommit = new Uid();
    AtomicAction A = new AtomicAction();

    // These user defined records could wrap anything such as 1PC JDBC or messages
    OrderedOnePhaseAbstractRecord rec1 = new OrderedOnePhaseAbstractRecord(secondToCommit);
    OrderedOnePhaseAbstractRecord rec2 = new OrderedOnePhaseAbstractRecord(firstToCommit);

    A.begin();

    // Doesn't matter of the order
    A.add(rec1);
    A.add(rec2);

    // Do some work you could save some concept of the work in the abstract record save_state
    // rec1.sendMessage
    // rec2.doSQL

    // This is just so we can see the opportunity for failure recovery
    rec1.causeTransientFailure();

    // Commit, this would make sure the database (rec2) was committed first
    A.commit();

    // This shows recovery working, we should see Uid2 called again, if you had encoded some
    // information in rec2 you could
    // maybe
    // retry the sending of a message or similar
    RecordTypeManager.manager()
        .add(
            new RecordTypeMap() {

              @Override
              public int getType() {
                return OrderedOnePhaseAbstractRecord.typeId();
              }

              @Override
              public Class<? extends AbstractRecord> getRecordClass() {
                return OrderedOnePhaseAbstractRecord.class;
              }
            });
    recoveryPropertyManager.getRecoveryEnvironmentBean().setRecoveryBackoffPeriod(1);
    RecoveryManager.manager(RecoveryManager.DIRECT_MANAGEMENT)
        .addModule(new AtomicActionRecoveryModule());
    RecoveryManager.manager().scan();
  }