Akka Getting Started-simple example

Source: Internet
Author: User
Tags constructor sleep tostring uuid xmlns log4j
The following procedure demonstrates a simple example of Akka. Create an actor to process a command that interacts with the message delivery.


I use the Akka version and the associated jar package see the Pom file:

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion >4.0.0 </modelVersion> <groupid >com.focusedu.akka </groupId> <artifactid >ak 

  ka-first</artifactid> <version >0.0.1-snapshot </version> <packaging >jar </packaging> <name >akka-first</name> <url >http://maven.apache.org </url> <properties > &L t;project.build.sourceencoding> utf-8</project.build.sourceencoding> </properties > < Dependencies > <dependency > <groupid >junit</groupId> <artifactid >junit</a rtifactid> <version >3.8.1 </version> <scope >test </scope> </dependency
    ; <dependency > <groupId> Com.typesaFe.akka</groupid > <artifactId> akka-actor_2.10 </artifactId> <version> 2.3.10&L
           t;/version> </dependency > <dependency > <groupId> log4j</groupid>  <artifactId> log4j</artifactid> <version> 1.2.17</version> </dependency > <dependency > <groupId> com.google.guava</groupid > <artifactId> Guava</artifactid > <version> 18.0</version> </dependency > <dependency &
        Gt <groupId> ch.qos.logback</groupid > <artifactId> logback-classic </artifactId> &lt ;version> 1.1.1</version> </dependency > <dependency > <groupId> com.typesafe. Akka</groupid > <artifactId> akka-persistence-experimental_2.10 </artifactId> <versio N> 2.3.10</version> </dependency > </dependencies > </project>
  

1. Creating Event Events

Import java.io.Serializable;

public class Event implements Serializable {

  private String data;
  Private String uuid;


  Public Event (String data) {
    super ();
    This. data = data;
  }

  Public Event (string data, string uuid) {this
    . data = data;
    This. UUID = UUID;
  }

  Public String GetData () {
    return data;
  }

  Public String Getuuid () {
    return uuid;
  }

  @Override public
  String toString () {
    return "event{" + "Data= '" + data + ' \ ' + ", uuid= ' + uuid + ' \ ' + '} ';
  }
}

2. Create a Command object
The main method is to override the OnReceive method, which is used to process the received message.

Import java.io.Serializable;

public class Command implements Serializable {
     private static final long serialversionuid = 1L;
     Private final String data;

     Public Command (String data) {
           this.data = data;
     }

     Public String GetData () {
           return this. data;
     }

     @Override public
     String toString () {
           return "command{" + "Data= '" + data + ' \ ' + '} ';}
     }

3. Create an Actor object

Import Java.util.UUID;

Import Com.center.akka.simple.command.Command;
Import com.center.akka.simple.event.Event;

Import Akka.actor.UntypedActor;
Import akka.event.Logging;
Import Akka.event.LoggingAdapter;

public class Simpleactor extends Untypedactor {

     Loggingadapter log = Logging.getlogger (GetContext (). System (), this) ;

     Public Simpleactor () {
           log.info ("Simpleactor constructor");
     }

     @Override public
     void OnReceive (Object msg) throws Exception {

           log.info ("Received Command:" + msg);
           if (msg instanceof Command) {
               final String data = (Command) msg). GetData ();
               Final Event Event = new event (data, Uuid.randomuuid (). toString ());
               Emmit an event somewhere ...

          } else if (msg. Equals ("echo")) {
               log.info ("echo!");}}
}

4. Test class

Import Org.slf4j.Logger;

Import Org.slf4j.LoggerFactory;
Import Akka.actor.ActorRef;
Import Akka.actor.ActorSystem;

Import Akka.actor.Props;
Import Com.center.akka.simple.actor.SimpleActor;

Import Com.center.akka.simple.command.Command; /** * * @author LCQ * * */public class System {public static final Logger log = Loggerfactory.getlogger (System.clas

  s); public static void Main (String ... args) throws Exception {final Actorsystem Actorsystem = actorsystem.create ("actor-

    System ");

    Thread.Sleep (5000);

    Final Actorref actorref = Actorsystem.actorof (Props.create (Simpleactor. Class), "Simple-actor");
    Actorref.tell (New Command ("CMD 1"), null);
    Actorref.tell (New Command ("CMD 2"), NULL);
    Actorref.tell (New Command ("CMD 3"), NULL);
    Actorref.tell (New Command ("CMD 4"), NULL);

    Actorref.tell (New Command ("CMD 5"), NULL);

    Thread.Sleep (5000);

    Log.debug ("Actor System Shutdown starting ...");
  Actorsystem.shutdown ();
 }
}

The two concepts that need to be differentiated are:
Actorref.tell: Asynchronously sends a message and returns immediately;
Actorref.ask: Asynchronously sends a message and returns a future that represents a possible response;
So always use tell to be more performance, unless you have to use ask


Operation Result:

[INFO] [05/17/2015 17:46:44.224] [Actor-system-akka.actor.default-dispatcher-3] [Akka://actor-system/user/simple-actor] SimpleActor constructor [ INFO] [05/17/2015 17:46:44.225] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/user/ Simple-actor] Received command:command{data= ' CMD 1 '} [INFO] [05/17/2015 17:46:44.312] [ ACTOR-SYSTEM-AKKA.ACTOR.DEFAULT-DISPATCHER-3] [Akka://actor-system/user/simple-actor] Received command:command{ Data= ' CMD 2 '} [INFO] [05/17/2015 17:46:44.312] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/ User/simple-actor] Received command:command{data= ' CMD 3 '} [INFO] [05/17/2015 17:46:44.312] [ ACTOR-SYSTEM-AKKA.ACTOR.DEFAULT-DISPATCHER-3] [Akka://actor-system/user/simple-actor] Received command:command{ Data= ' CMD 4 '} [INFO] [05/17/2015 17:46:44.312] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/ User/simple-actor] Received command:command{data= ' CMD 5 '} 17:46:49.226 [main] DEBUG Com.center.akka.simple.app.SysTem-actor System Shutdown Starting ...
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.