Created
March 16, 2012 04:53
-
-
Save write2munish/2048560 to your computer and use it in GitHub Desktop.
Load Generation Example Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private ActorSystem system; | |
| private final ActorRef router; | |
| private final static int no_of_msgs = 10 * 1000000; | |
| public ApplicationManagerSystem() { | |
| final int no_of_workers = 10; | |
| system = ActorSystem.create("LoadGeneratorApp"); | |
| final ActorRef appManager = system.actorOf( | |
| new Props(new UntypedActorFactory() { | |
| public UntypedActor create() { | |
| return new JobControllerActor(no_of_msgs); | |
| } | |
| }), "jobController"); | |
| router = system.actorOf(new Props(new UntypedActorFactory() { | |
| public UntypedActor create() { | |
| return new WorkerActor(appManager); | |
| } | |
| }).withRouter(new RoundRobinRouter(no_of_workers))); | |
| } | |
| private void generateLoad() { | |
| for (int i = no_of_msgs; i >= 0; i--) { | |
| router.tell("Job Id " + i + "# send"); | |
| } | |
| System.out.println("All jobs sent successfully"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class JobControllerActor extends UntypedActor { | |
| int count = 0; | |
| long startedTime = System.currentTimeMillis(); | |
| int no_of_msgs = 0; | |
| @Override | |
| public void onReceive(Object message) throws Exception { | |
| if (message instanceof String) { | |
| if (((String) message).compareTo("Done") == 0) { | |
| count++; | |
| if (count == no_of_msgs) { | |
| long now = System.currentTimeMillis(); | |
| System.out.println("All messages processed in " | |
| + (now - startedTime) / 1000 + " seconds"); | |
| System.out.println("Total Number of messages processed " | |
| + count); | |
| getContext().system().shutdown(); | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class WorkerActor extends UntypedActor { | |
| private ActorRef jobController; | |
| @Override | |
| public void onReceive(Object message) throws Exception { | |
| // using scheduler to send the reply after 1000 milliseconds | |
| getContext() | |
| .system() | |
| .scheduler() | |
| .scheduleOnce(Duration.create(1000, TimeUnit.MILLISECONDS), | |
| jobController, "Done"); | |
| } | |
| public WorkerActor(ActorRef inJobController) { | |
| jobController = inJobController; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment