4

在 AKKA 文档中写道

...参与者不应阻塞(即在占用线程时被动等待)某些外部实体,可能是锁、网络套接字等。阻塞操作应该在一些特殊情况下的线程中完成,该线程将消息发送到对他们采取行动的行动者。 来源http://doc.akka.io/docs/akka/2.0/general/actor-systems.html#Actor_Best_Practices

目前我发现了以下信息:

  1. 我阅读了从 Akka / Scala 发送出站 HTTP 请求并检查了https://github.com/dsciamma/fbgl1上的示例

  2. 我发现以下文章http://nurkiewicz.blogspot.de/2012/11/non-blocking-io-discovering-akka.html解释了如何使用https://github.com/AsyncHttpClient/async-http-client非阻塞带有 akka 的 http 客户端。但是是用 Scala 编写的。

我如何编写一个发出非阻塞 http 请求的演员?

它必须将远程 url 页面下载为文件,然后将生成的文件对象发送给主参与者。然后主actor将此请求发送到解析器actor以解析文件......

4

2 回答 2

4

在最后一个回复中,Koray 使用了错误的发件人参考,正确的做法是:

public class ReduceActor extends UntypedActor {

@Override
public void onReceive(Object message) throws Exception {
    if (message instanceof URI) {
        URI url = (URI) message;


        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        final ActorRef sender = getSender();
        asyncHttpClient.prepareGet(url.toURL().toString()).execute(new AsyncCompletionHandler<Response>() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                File f = new File("e:/tmp/crawler/" + UUID.randomUUID().toString() + ".html");
                // Do something with the Response
                // ...
                // System.out.println(response1.getStatusLine());
                FileOutputStream fao = new FileOutputStream(f);
                IOUtils.copy(response.getResponseBodyAsStream(), fao);
                System.out.println("File downloaded " + f);
                sender.tell(new WordCount(f));
                return response;
            }

            @Override
            public void onThrowable(Throwable t) {
                // Something wrong happened.
            }
        });
    } else
        unhandled(message);
  }

查看akka的另一个线程:https ://stackoverflow.com/a/11899690/575746

于 2013-02-21T19:04:54.107 回答
0

我已经以这种方式实现了这一点。

public class ReduceActor extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
    if (message instanceof URI) {
        URI url = (URI) message;

        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();

        asyncHttpClient.prepareGet(url.toURL().toString()).execute(new AsyncCompletionHandler<Response>() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                File f = new File("e:/tmp/crawler/" + UUID.randomUUID().toString() + ".html");
                // Do something with the Response
                // ...
                // System.out.println(response1.getStatusLine());
                FileOutputStream fao = new FileOutputStream(f);
                IOUtils.copy(response.getResponseBodyAsStream(), fao);
                System.out.println("File downloaded " + f);
                getSender().tell(new WordCount(f));
                return response;
            }

            @Override
            public void onThrowable(Throwable t) {
                // Something wrong happened.
            }
        });
    } else
        unhandled(message);
}
于 2012-11-08T20:26:23.007 回答