Search in sources :

Example 1 with Container

use of org.jboss.stm.Container in project narayana by jbosstm.

the class EchoClient method start.

@Override
public void start() throws Exception {
    vertx.createNetClient().connect(1234, "localhost", res -> {
        if (res.succeeded()) {
            NetSocket socket = res.result();
            socket.handler(buffer -> {
                System.out.println("Net client receiving: " + buffer.toString("UTF-8"));
            });
            // Now send some data
            for (int i = 0; i < 10; i++) {
                String str = "hello " + i + "\n";
                System.out.println("Net client sending: " + str);
                socket.write(str);
            }
            /*
	   * If you are running this for the first time then leave as is.
	   * If you are running this more than once and want clients to share the STM objects between
	   * address spaces then go into the ObjectStore dir and look for the Uid that represents the state
	   * you want to share. Then uncomment the Uid line below and replace the Uid in quotes with the Uid
	   * you have selected. Uncomment the other obj1 creation line and comment out the original.
	   *
	   * If you want to see how this might work then just go with the example state in the ObjectStore
	   * shipped as part of this example and uncomment the lines.
	   */
            /*
	   * STM states are identified by Uids in the ObjectStore. This is an example.
	   */
            // Modify this line if sharing state and uncomment.
            // Uid u = new Uid("0:ffffc0a80003:c915:529f59de:1");
            Container<Sample> theContainer = new Container<Sample>("Demo", Container.TYPE.PERSISTENT, Container.MODEL.SHARED);
            // Modify this line if sharing state and uncomment.
            // Sample obj1 = theContainer.clone(new SampleLockable(10), u);
            Sample obj1 = theContainer.create(new SampleLockable(10));
            System.out.println("Object name: " + theContainer.getIdentifier(obj1));
            // Now send some data
            for (int i = 0; i < 10; i++) {
                AtomicAction A = new AtomicAction();
                A.begin();
                obj1.increment();
                String str = "hello" + obj1.value() + "\n";
                System.out.print("Net client sending: " + str);
                socket.write(str);
                A.commit();
            }
        } else {
            System.out.println("Failed to connect " + res.cause());
        }
    });
}
Also used : NetSocket(io.vertx.core.net.NetSocket) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) Container(org.jboss.stm.Container)

Example 2 with Container

use of org.jboss.stm.Container in project narayana by jbosstm.

the class BasicUnitTest method testExampleSTM.

@Test
public void testExampleSTM() throws Exception {
    /*
	 * Create the container for the Transactional interface.
	 */
    Container<Atomic> theContainer = new Container<Atomic>();
    /*
	 * Create the instance of the class. But this won't be an STM object yet, so don't
	 * manipulate it just yet.
	 */
    ExampleSTM basic = new ExampleSTM();
    boolean success = true;
    /*
	 * This object will be the one we actually use.
	 */
    Atomic obj = null;
    try {
        /*
	     * Pass the instance we created previously to the Container so it
	     * can then create an STM object which we then use to manipulate
	     * the first object in a transactional manner.
	     */
        obj = theContainer.create(basic);
    } catch (final Throwable ex) {
        ex.printStackTrace();
        success = false;
    }
    assertTrue(success);
    // a transaction!
    AtomicAction a = new AtomicAction();
    a.begin();
    obj.set(1234);
    a.commit();
    assertEquals(obj.get(), 1234);
    a = new AtomicAction();
    a.begin();
    // the value at this stage will be 1235
    obj.change(1);
    a.abort();
    // we aborted, so the value should be back to 1234
    assertEquals(obj.get(), 1234);
}
Also used : AtomicAction(com.arjuna.ats.arjuna.AtomicAction) Container(org.jboss.stm.Container) Test(org.junit.Test)

Example 3 with Container

use of org.jboss.stm.Container in project narayana by jbosstm.

the class SampleVerticle2 method start.

public void start() {
    LocalMap<String, String> map = vertx.sharedData().getLocalMap("demo.mymap");
    Container<Sample> theContainer = new Container<>("Demo", Container.TYPE.PERSISTENT, Container.MODEL.SHARED);
    String uidName = map.get(ClientVerticle.LEADER);
    Sample obj1 = theContainer.clone(new SampleLockable(10), new Uid(uidName));
    AtomicAction A = new AtomicAction();
    int value = -1;
    int initialValue = -1;
    boolean shouldCommit = true;
    A.begin();
    try {
        initialValue = obj1.value();
        obj1.increment();
    } catch (final Throwable ex) {
        ex.printStackTrace();
        shouldCommit = false;
    }
    try {
        if (shouldCommit) {
            obj1.increment();
            value = obj1.value();
        }
    } catch (final Throwable ex) {
        ex.printStackTrace();
        shouldCommit = false;
    }
    if (shouldCommit)
        A.commit();
    else {
        A.abort();
        value = -1;
    }
    System.err.println("SampleVerticle2 initialised state with: " + value);
    if (value == initialValue + 2)
        System.err.println("SampleVerticle2 SUCCEEDED!");
    else
        System.err.println("SampleVerticle2 FAILED!");
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) Container(org.jboss.stm.Container)

Example 4 with Container

use of org.jboss.stm.Container in project narayana by jbosstm.

the class SampleVerticle1 method start.

public void start() {
    LocalMap<String, String> map = vertx.sharedData().getLocalMap("demo.mymap");
    Container<Sample> theContainer = new Container<Sample>("Demo", Container.TYPE.PERSISTENT, Container.MODEL.SHARED);
    String uidName = map.get(ClientVerticle.LEADER);
    Sample obj1 = theContainer.clone(new SampleLockable(10), new Uid(uidName));
    AtomicAction A = new AtomicAction();
    int value = -1;
    int initialValue = -1;
    boolean shouldCommit = true;
    A.begin();
    try {
        initialValue = obj1.value();
        obj1.increment();
        value = obj1.value();
    } catch (final Throwable ex) {
        ex.printStackTrace();
        shouldCommit = false;
    }
    if (shouldCommit)
        A.commit();
    else {
        A.abort();
        value = -1;
    }
    System.err.println("SampleVerticle1 initialised state with: " + value);
    if (value == initialValue + 1)
        System.err.println("SampleVerticle1 SUCCEEDED!");
    else
        System.err.println("SampleVerticle1 FAILED!");
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) Container(org.jboss.stm.Container)

Example 5 with Container

use of org.jboss.stm.Container in project narayana by jbosstm.

the class ClientVerticle method start.

public void start() {
    LocalMap<String, String> map = vertx.sharedData().getLocalMap("demo.mymap");
    Container<Sample> theContainer = new Container<Sample>("Demo", Container.TYPE.PERSISTENT, Container.MODEL.SHARED);
    Sample obj1 = theContainer.create(new SampleLockable(10));
    map.put(LEADER, theContainer.getIdentifier(obj1).toString());
    vertx.deployVerticle(new SampleVerticle1());
    final int INSTANCE_CNT = 0;
    for (int i = 0; i < INSTANCE_CNT; i++) vertx.deployVerticle(new SampleVerticle2());
    System.out.println("Object name: " + theContainer.getIdentifier(obj1));
    // Now send some data
    for (int i = 0; i < 10; i++) {
        AtomicAction A = new AtomicAction();
        boolean shouldCommit = true;
        A.begin();
        try {
            obj1.increment();
            System.out.println("State value is: " + obj1.value());
        } catch (final Throwable ex) {
            ex.printStackTrace();
            shouldCommit = false;
        }
        if (shouldCommit)
            A.commit();
        else
            A.abort();
    }
    AtomicAction B = new AtomicAction();
    int value = -1;
    boolean doCommit = true;
    B.begin();
    try {
        value = obj1.value();
    } catch (final Throwable ex) {
        doCommit = false;
    }
    if (doCommit) {
        B.commit();
        System.out.println("ClientVerticle initialised state: " + value);
    } else {
        B.abort();
        System.out.println("ClientVerticle could not initialise state.");
    }
}
Also used : AtomicAction(com.arjuna.ats.arjuna.AtomicAction) Container(org.jboss.stm.Container)

Aggregations

AtomicAction (com.arjuna.ats.arjuna.AtomicAction)5 Container (org.jboss.stm.Container)5 Uid (com.arjuna.ats.arjuna.common.Uid)2 NetSocket (io.vertx.core.net.NetSocket)1 Test (org.junit.Test)1