AKKA2 use Explore 7--"cloud computing" sample (Actor, Future, Remoting, Router, Deploy, asynchronous, concurrent use demo)

Source: Internet
Author: User
Tags iterable

Assuming that there is a time-consuming operation, a single machine has been unable to meet the requirements, then you can think of a number of computer collaboration to complete. Specifically how to do it.

For a very simple example, suppose that this time-consuming operation is from 1 to 100000 and you now have two servers that allow the two servers to be completed from 1 to 50000, and from 50001 to 100000, and then this machine completes the sum of the two results.

The two servers start with two Akka server, and there is also a calcactor. This calculation actor receives two parameters: integer start and integer end, which can be added from start to the last, and the result is returned to the sender: Getsender (). Tell (Results).

 @Log4j class Calcactor extends Untypedactor {@Override void onreceive (Object message) {log.debug "C"
            Alcactor Received: ${message}----self:${getself ()},sender:${getsender ()} "if (message instanceof String) { string[] args = Message.split (",") int start = Integer.parseint (args[0)) int end = INTEGER.PA Rseint (args[1]) int result = 0 println ("Start calc:" + start + "upto" + end) start.u  PTO (end) {results + = it} sleep (5000)//simulation also takes an additional 5 seconds println ("Result:" + Result) Getsender (). Tell (Result)} else {unhandled (message)}}} Two servers are: /pre>
Akkaserverapp ServerA = new Akkaserverapp ("SC", "10.68.3.122", 8888, "Calc")//akkasystemname to SC, IP is 10.68.3.122 and the port is 8888,servicename to Calc.

    Akkaserverapp ServerA = new Akkaserverapp ("SP", "10.68.3.124", 8888, "Calc")//akkasystemname to SP, IP is 10.68.3.124 and the port is 8888,servicename to Calc.

The main code is at the client:
    public static void Main (string[] args) throws Exception {final Akkaserverapp app = new Akkaserverapp ("XWC", "127.0.0.1", 6666, "client"),//clients Akka configuration Actorref remoteCalcA1 = App.getsystem (). Actorof (New Props (Calcactor.class ).. Withdeploy (New Deploy (New Remotescope ("Akka", "SC", "10.68.3.122", 8888)), "clientCalcA1"); Publish Calcactor to a remote 10.68.3.122 actorref remoteCalcA2 = App.getsystem (). Actorof (New Props (Calcactor.class). Withdeploy (New Deploy (New Remotescope ("Akka", "SC", "10.68.3.122", 8888)), "clientCalcA2"); Publish Calcactor to the remote 10.68.3.124 final list<future<double>> FRS = new Arraylist<future<double>&gt ;()//Asynchronous return results future stored in the list//tell only request, whether or not to respond to it completely unaware.        
Ask is a request, and it is clear that the future will be appropriate.
Remotecalca.tell ("1,10000", App.getserveractor ());
        Remotecalcb.tell ("10001,20000", App.getserveractor ()); Future f1 = Akka.pattern.Patterns.ask (remoteCalcA1, "1,50000", 150000);//Let remote 122 calculate from 1 to 50000, timeout time is150 sec Future F2 = akka.pattern.Patterns.ask (remoteCalcA1, "50001,100000", 150000);//concurrency Let remote 124 compute from 50001 to 100000, timeout 1
        50 seconds Frs.add (F1);

        Frs.add (F2); future<iterable<double>> Future = futures.sequence (FRS, App.getsystem (). Dispatcher ()); Converts the results of future returns into Future<iterable<double>> future<double> FR = Future.map (New Mapper<iterable<dou
                Ble&gt, double> () {@Override public Double apply (iterable<double> parameter) {
                Double result = 0d;
                for (Double s:parameter) {//Calculates the result of two server returns = S;
            return result;

        }

        });
                Fr.onsuccess (New onsuccess<double> () {@Override public void onsuccess (Double) {
            System.out.println ("Cloud return results-----" + result);
    }
        }); }

You can also have the server concurrent processing: Divide the task from 1 to 50000 into 5 threads in parallel: 1. 10000,10001..20000,20001..30000,30001..40000,40001..50000, this can better improve efficiency.

If you press the above method to simply publish multiple remote actor:

Actorref Remotecalcan = App.getsystem (). Actorof (New Props (Calcactor.class). Withdeploy (New Deploy (New Remotescope ("Akka", "SC", "10.68.3.122", 8888))), "Clientcalcan");

is not able to improve efficiency, because at this time the Calcactor is single-threaded, it will only receive 1 first. 10000, after processing and then receive 10001. 20000 and deal with ...

Allowing it to be handled in parallel is simple, and adding withroute when creating remoteactor:

Actorref Remotecalcan = App.getsystem (). Actorof (New Props (Calcactor.class). Withrouter (New Roundrobinrouter (5)). Withdeploy (New Deploy (New Remotescope ("Akka", "SC", "10.68.3.122", 8888))), "Clientcalcan");  Roundrobinrouter parameter 5 can be understood as allocating 5 threads in parallel processing

The code is basically the same as above
    public static void Main (string[] args) throws Exception {final Akkaserverapp app = new Akkaserverapp ("XWC",
        "127.0.0.1", 6666, "client"); Actorref remoteCalcA1 = App.getsystem (). Actorof (New Props (Calcactor.class). Withrouter (New Roundrobinrouter (4)).    
        Withdeploy (New Deploy (New Remotescope ("Akka", "SC", "10.68.3.122", 8888))), "clientCalcA1"); Actorref remoteCalcB1 = App.getsystem (). Actorof (New Props (Calcactor.class). Withrouter (New Roundrobinrouter (4)).
   
        Withdeploy (New Deploy (New Remotescope ("Akka", "sp", "10.68.3.124", 8888)), "clientCalcB1");
        
        Final list<future<double>> FRS = new arraylist<future<double>> ();
        Future f1 = Akka.pattern.Patterns.ask (remoteCalcA1, "1,10000", 150000);
        Future F2 = Akka.pattern.Patterns.ask (remoteCalcA1, "10001,20000", 150000);
        Future F3 = Akka.pattern.Patterns.ask (remoteCalcA1, "20001,30000", 150000); Future F4 = Akka.pattern.PaTterns.ask (remoteCalcA1, "30001,40000", 150000);
        Future f5 = Akka.pattern.Patterns.ask (remoteCalcB1, "40001,50000", 150000);
        Future f6 = Akka.pattern.Patterns.ask (remoteCalcB1, "50001,60000", 150000);
        Future F7 = Akka.pattern.Patterns.ask (remoteCalcB1, "60001,70000", 150000);
        Future F8 = Akka.pattern.Patterns.ask (remoteCalcB1, "70001,80000", 150000);
        Frs.add (F1);
        Frs.add (F2);
        Frs.add (F3);
        Frs.add (F4);
        Frs.add (F5);
        Frs.add (f6);
        Frs.add (F7);

        Frs.add (f8);
        future<iterable<double>> Future = futures.sequence (FRS, App.getsystem (). Dispatcher ());
            Future<double> FR = Future.map (new Mapper<iterable<double>, double> () {@Override
                Public Double apply (iterable<double> parameter) {double result = 0d;
                for (Double s:parameter) {result = S; } RETurn result;

        }

        });
                Fr.onsuccess (New onsuccess<double> () {@Override public void onsuccess (Double) {
            SYSTEM.OUT.PRINTLN ("Cloud computing returns from 1 to 80000 results-----" + result);
    }
        }); }

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.