cleaned up packaging in samples to all be "sample.x"

This commit is contained in:
Jonas Bonér 2010-04-05 12:32:15 +02:00
parent f04fbba47b
commit 30badd6cc0
9 changed files with 133 additions and 90 deletions

View file

@ -0,0 +1,23 @@
package sample.rest.java;
import se.scalablesolutions.akka.config.ActiveObjectConfigurator;
import static se.scalablesolutions.akka.config.JavaConfig.*;
public class Boot {
final private ActiveObjectConfigurator manager = new ActiveObjectConfigurator();
public Boot() throws Exception {
manager.configure(
new RestartStrategy(new OneForOne(), 3, 5000, new Class[]{Exception.class}),
new Component[] {
new Component(
SimpleService.class,
new LifeCycle(new Permanent()),
1000),
new Component(
PersistentSimpleService.class,
new LifeCycle(new Permanent()),
1000)
}).supervise();
}
}

View file

@ -0,0 +1,59 @@
/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package sample.rest.java;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import se.scalablesolutions.akka.actor.annotation.transactionrequired;
import se.scalablesolutions.akka.actor.annotation.prerestart;
import se.scalablesolutions.akka.actor.annotation.postrestart;
import se.scalablesolutions.akka.persistence.common.PersistentMap;
import se.scalablesolutions.akka.persistence.cassandra.CassandraStorage;
import java.nio.ByteBuffer;
/**
* Try service out by invoking (multiple times):
* <pre>
* curl http://localhost:9998/persistentjavacount
* </pre>
* Or browse to the URL from a web browser.
*/
@Path("/persistentjavacount")
@transactionrequired
public class PersistentSimpleService {
private String KEY = "COUNTER";
private boolean hasStartedTicking = false;
private PersistentMap<byte[], byte[]> storage;
@GET
@Produces({"application/html"})
public String count() {
if (storage == null) storage = CassandraStorage.newMap();
if (!hasStartedTicking) {
storage.put(KEY.getBytes(), ByteBuffer.allocate(2).putInt(0).array());
hasStartedTicking = true;
return "Tick: 0\n";
} else {
byte[] bytes = (byte[])storage.get(KEY.getBytes()).get();
int counter = ByteBuffer.wrap(bytes).getInt();
storage.put(KEY.getBytes(), ByteBuffer.allocate(4).putInt(counter + 1).array());
return "Tick: " + counter + "\n";
}
}
@prerestart
public void preRestart() {
System.out.println("Prepare for restart by supervisor");
}
@postrestart
public void postRestart() {
System.out.println("Reinitialize after restart by supervisor");
}
}

View file

@ -0,0 +1,56 @@
/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package sample.rest.java;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import se.scalablesolutions.akka.actor.annotation.transactionrequired;
import se.scalablesolutions.akka.actor.annotation.prerestart;
import se.scalablesolutions.akka.actor.annotation.postrestart;
import se.scalablesolutions.akka.stm.TransactionalState;
import se.scalablesolutions.akka.stm.TransactionalMap;
/**
* Try service out by invoking (multiple times):
* <pre>
* curl http://localhost:9998/javacount
* </pre>
* Or browse to the URL from a web browser.
*/
@Path("/javacount")
@transactionrequired
public class SimpleService {
private String KEY = "COUNTER";
private boolean hasStartedTicking = false;
private TransactionalMap<String, Integer> storage;
@GET
@Produces({"application/json"})
public String count() {
if (storage == null) storage = TransactionalState.newMap();
if (!hasStartedTicking) {
storage.put(KEY, 0);
hasStartedTicking = true;
return "Tick: 0\n";
} else {
int counter = (Integer)storage.get(KEY).get() + 1;
storage.put(KEY, counter);
return "Tick: " + counter + "\n";
}
}
@prerestart
public void preRestart() {
System.out.println("Prepare for restart by supervisor");
}
@postrestart
public void postRestart() {
System.out.println("Reinitialize after restart by supervisor");
}
}