I don't see any problems with that.

/Patrik

On Thu, Feb 12, 2015 at 2:36 PM, LIANG CAO <[email protected]> wrote:

> public class TokenRepository {
> private static final ActorSelection DB_ROUTER_SELECTION;
> private static final String DB_ROUTER_SELECTION_PATH = "/user/JDBCRouter";
> private static final int EXECUTE_TIME_OUT = 1000;
> private static final String SAVE_TOKEN_STR = "INSERT INTO Tokens " +
>         "(uuid, creation, email, expiration, isSignUp) " +
>         "VALUES (?,?,?,?,?)";
>
> private static final String FIND_TOKEN_STR = "SELECT * FROM Tokens " +
>         "WHERE uuid = ?";
>
> private static final String DELETE_TOKEN_STR = "DELETE FROM Tokens" +
>         "WHERE uuid = ?";
>
>
> static {
>     DB_ROUTER_SELECTION = 
> AkkaActorSystem.getInstance().actorSelection(DB_ROUTER_SELECTION_PATH);
> }
>
>
> private static class saveTokenExecutor implements SQLExecutable {
>     private final Token token;
>
>     saveTokenExecutor(Token token) {
>         this.token = token;
>     }
>
>     @Override
>     public void execute(Connection connection, UntypedActorContext 
> untypedActorContext) throws SQLException {
>         connection.setAutoCommit(false);
>         java.sql.PreparedStatement preparedStatement = 
> connection.prepareStatement(SAVE_TOKEN_STR);
>         preparedStatement.setString(1, token.getUuid());
>         preparedStatement.setTimestamp(2, new 
> java.sql.Timestamp(token.getCreationTime().getMillis()));
>         preparedStatement.setString(3, token.getEmail());
>         preparedStatement.setTimestamp(4, new 
> java.sql.Timestamp(token.getCreationTime().plusMinutes(5).getMillis()));
>         preparedStatement.setBoolean(5, token.getIsSignUp());
>         preparedStatement.execute();
>         connection.commit();
>         untypedActorContext.sender().tell(token, ActorRef.noSender());
>
>     }
> }
>
> private static class findTokenExecutor implements SQLExecutable {
>     private final String uuid;
>
>     public findTokenExecutor(String uuid) {
>         this.uuid = uuid;
>     }
>
>     @Override
>     public void execute(Connection connection, UntypedActorContext 
> untypedActorContext) throws SQLException {
>         connection.setAutoCommit(false);
>         java.sql.PreparedStatement preparedStatement = 
> connection.prepareStatement(FIND_TOKEN_STR);
>         preparedStatement.setString(1, uuid);
>         preparedStatement.execute();
>         ResultSet resultSet = preparedStatement.getResultSet();
>         resultSet.next();
>         Token token = new Token();
>         token.setEmail(resultSet.getString("email"));
>         token.setIsSignUp(resultSet.getBoolean("isSignUp"));
>         token.setCreationTime(new 
> DateTime(resultSet.getTimestamp("creation")));
>         token.setExpirationTime(new 
> DateTime(resultSet.getTimestamp("expiration")));
>         token.setUuid(resultSet.getString("uuid"));
>         connection.commit();
>         untypedActorContext.sender().tell(token, ActorRef.noSender());
>     }
> }
>
> private static class deleteTokenExecutor implements SQLExecutable {
>     private final String uuid;
>
>     public deleteTokenExecutor(String uuid) {
>         this.uuid = uuid;
>     }
>
>     @Override
>     public void execute(Connection connection, UntypedActorContext 
> untypedActorContext) throws SQLException {
>         java.sql.PreparedStatement preparedStatement = 
> connection.prepareStatement(DELETE_TOKEN_STR);
>         preparedStatement.setString(1, uuid);
>     }
> }
>
> public static Future<Token> saveToken(Token token) {
>     Future<Object> tokenFuture = ask(DB_ROUTER_SELECTION, new 
> saveTokenExecutor(token), 1000);
>     return tokenFuture.flatMap(new Mapper<Object, Future<Token>>() {
>         @Override
>         public Future<Token> apply(Object parameter) {
>             return Futures.successful((Token) parameter);
>         }
>     }, AkkaActorSystem.getInstance().dispatcher());
> }
>
> public static Future<Token> findToken(String uuid) {
>     Future<Object> tokenFuture = ask(DB_ROUTER_SELECTION, new 
> findTokenExecutor(uuid), EXECUTE_TIME_OUT);
>     return tokenFuture.flatMap(new Mapper<Object, Future<Token>>() {
>         @Override
>         public Future<Token> apply(Object parameter) {
>             return Futures.successful((Token) parameter);
>         }
>     }, AkkaActorSystem.getInstance().dispatcher());
>
> }
>
> /**
>  * delete token
>  *
>  * @param uuid
>  * @return future<int>
>  */
> public static Future<Object> deleteToken(String uuid) {
>     return ask(DB_ROUTER_SELECTION, new deleteTokenExecutor(uuid), 
> EXECUTE_TIME_OUT);
> }
>
>
> public class DBWorker extends UntypedActor {
> private final String host, user, password, db;
> private final int port ;
> private java.sql.Connection conn;
>
> private static class DBWorkerCreator implements Creator<DBWorker> {
>     private final String host, user, password, db;
>     private final int port;
>
>     public DBWorkerCreator(String host, int port, String user, String 
> password, String db){
>         this.host = host;
>         this.user = user;
>         this.port = port;
>         this.password = password;
>         this.db = db;
>     }
>
>     @Override
>     public DBWorker create() throws Exception {
>         return new DBWorker(host, port, user, password, db);
>     }
> }
>
> public static Props getProps(String host, int port, String user, String 
> password,String db){
>     return Props.create(new DBWorkerCreator(host, port, user, password, db));
> }
>
> public DBWorker(String host, int port, String user, String password, String 
> db){
>     this.host = host;
>     this.port = port;
>     this.user = user;
>     this.password = password;
>     this.db = db;
> }
>
> /**
>  * initialization: database connection
>  */
> @Override
> public void preStart() throws Exception {
>     try {
>         String url = "jdbc:mysql://"+ host + ":" + port + "/" + db;
>         System.out.println(url);
>         conn = DriverManager.getConnection(url,user,password );
>     } catch (SQLException e) {
>         //something happens when connecting to the database, throw exception 
> to DB Router
>         e.printStackTrace();
>         throw e;
>     }
> }
>
>
> //cancel post start
> @Override
> public void postRestart(Throwable reason){
>
> }
>
> @Override
> public void onReceive(Object message) throws Exception {
>
>
>     try {
>         if(message instanceof SQLExecutable){
>             ((SQLExecutable) message).execute(conn,getContext());
>         } else if (message instanceof String){
>             conn.createStatement().execute((String)message);
>         }
>         else {
>             // unknown message, do nothing here
>         }
>     } catch (SQLException e) {
>         e.printStackTrace();
>
>         try {
>             //if SQLException is throwing up, rollback
>             conn.rollback();
>         } catch (SQLException e1) {
>             // something serious happens, throw the exception to DB router
>             e1.printStackTrace();
>             throw e1;
>         }
>
>     } finally {
>
>     }
> }
>
>
>
> 在 2015年2月12日星期四 UTC+1下午2:03:14,LIANG CAO写道:
>>
>> I recently began to learn AKKA,
>>
>> I have created a router actor, there are a bunch of database worker
>> actors sitting behind the router. I have came with this implementation,
>> each worker actor will have one database connection, I enclose database
>> logic into each message (by implementing interface SQLExecutable), actor
>> will execute each message. If the connection dies, exception will be
>> handled by router's supervision strategy.
>>
>> My question is that I know each message in AKKA should be immutable, here
>> I am enclosing the logic inside the message, I am not sure if this will
>> cause any unexpected behaviour. My intention just want to decouple business
>> logic and database connection. You can follow the link to my question in
>> stackoverflow.
>> http://stackoverflow.com/questions/28461927/enclosing-
>> logic-in-akka-actor-message
>>
>  --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 

Patrik Nordwall
Typesafe <http://typesafe.com/> -  Reactive apps on the JVM
Twitter: @patriknw

[image: Scala Days] <http://event.scaladays.org/scaladays-sanfran-2015>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to