I am newbie to Akka, Akka not executing parallely with out Routers.
Kindly find my Producer.java & Consumer.java for reference.
*Below code executes jobs one by one*
ActorRef actorRef = system.actorOf(Props.create(Consumer.class));
actorRef .tell(job1..)
actorRef .tell(job2..)
actorRef .tell(job3..)
actorRef .tell(job4..)
*Below code executes jobs parallely, it spans 4 thread*
ActorRef printNumbersConsumer =
system.actorOf(Props.create(Consumer.class).withRouter(new
RoundRobinRouter(4)),"router");
actorRef .tell(job1..)
actorRef .tell(job2..)
actorRef .tell(job3..)
actorRef .tell(job4..)
Kindly correct if my understanding is wrong.
--
>>>>>>>>>> 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 https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
package com.kk;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import akka.actor.UntypedActor;
public class Consumer extends UntypedActor {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
@Override
public void onReceive(Object msg) throws Exception {
if(msg instanceof Map) {
Map<String,String> value=(Map) msg;
Date now = new Date();
System.out.println(sdfDate.format(now) +" :: "+"<<< Receiving & printing " +" :: "+ msg);
BufferedWriter bw = new BufferedWriter(new FileWriter("MyFile-"+value.get("name")+".html"));
StringBuilder textView=new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(value.get("web"));
HttpResponse response = client.execute(request);
// Get the response
BufferedReader rd = new BufferedReader
(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
textView.append(line);
}
bw.write(textView.toString());
bw.close();
System.out.println(sdfDate.format(now) +" :: "+value.get("name")+" Done");
}
}
}
package com.kk;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.routing.RoundRobinRouter;
public class Producer {
public static void main(String[] args) throws InterruptedException {
Thread.sleep(15000);
long startTime=System.currentTimeMillis();
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
ActorSystem system = ActorSystem.create("generate-numbers-one-to-ten");
//ActorRef printNumbersConsumer = system.actorOf(Props.create(Consumer.class).withRouter(new RoundRobinRouter(14)),"router");
ActorRef printNumbersConsumer = system.actorOf(Props.create(Consumer.class));
Map<String,String> msg =new HashMap<String,String>();
msg.put("name","google");
msg.put("web","http://localhost:8080/test");
Date now = new Date();
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg);
printNumbersConsumer.tell(msg, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg1 =new HashMap<String,String>();
msg1.put("name","youtube");
msg1.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg1);
printNumbersConsumer.tell(msg1, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg2 =new HashMap<String,String>();
msg2.put("name","mkyong");
msg2.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg2);
printNumbersConsumer.tell(msg2, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg3 =new HashMap<String,String>();
msg3.put("name","wiki");
msg3.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg3);
printNumbersConsumer.tell(msg3, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg4 =new HashMap<String,String>();
msg4.put("name","blog");
msg4.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg4);
printNumbersConsumer.tell(msg4, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg5 =new HashMap<String,String>();
msg5.put("name","stackoverflow");
msg5.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg5);
printNumbersConsumer.tell(msg5, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg6 =new HashMap<String,String>();
msg6.put("name","vogella");
msg6.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg6);
printNumbersConsumer.tell(msg6, ActorRef.noSender());
Map<String,String> msg7 =new HashMap<String,String>();
msg7.put("name","python");
msg7.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg7);
printNumbersConsumer.tell(msg7, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg8 =new HashMap<String,String>();
msg8.put("name","typesafe");
msg8.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg8);
printNumbersConsumer.tell(msg8, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg9 =new HashMap<String,String>();
msg9.put("name","play");
msg9.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg9);
printNumbersConsumer.tell(msg9, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg10 =new HashMap<String,String>();
msg10.put("name","akka");
msg10.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg10);
printNumbersConsumer.tell(msg10, ActorRef.noSender());
Map<String,String> msg11 =new HashMap<String,String>();
msg11.put("name","oracle");
msg11.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg11);
printNumbersConsumer.tell(msg11, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg12 =new HashMap<String,String>();
msg12.put("name","inautix");
msg12.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg12);
printNumbersConsumer.tell(msg12, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg13 =new HashMap<String,String>();
msg13.put("name","windstream");
msg13.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg13);
printNumbersConsumer.tell(msg13, ActorRef.noSender());
TimeUnit.SECONDS.sleep(1);
Map<String,String> msg14 =new HashMap<String,String>();
msg14.put("name","prodapt");
msg14.put("web","http://localhost:8080/test");
System.out.println(">>> Producing & sending a number on " +sdfDate.format(now) +" :: "+ msg14);
printNumbersConsumer.tell(msg14, ActorRef.noSender());
System.out.println("==============================================================");
System.out.println("===== Finished producing & sending numbers "+(System.currentTimeMillis()-startTime));
}
}