Skip to content

Instantly share code, notes, and snippets.

@write2munish
Created March 16, 2012 04:53
Show Gist options
  • Save write2munish/2048560 to your computer and use it in GitHub Desktop.
Save write2munish/2048560 to your computer and use it in GitHub Desktop.
Load Generation Example Code
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");
}
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();
}
}
}
}
}
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