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. https://github.com/AsyncHttpClient/async-http-clientnonblockingの使用方法を説明する次の記事http://nurkiewicz.blogspot.de/2012/11/non-blocking-io-discovering-akka.htmlを見つけましたakkaを使用したhttpクライアント。しかし、Scalaで書かれています。

ノンブロッキングhttpリクエストを行うアクターを作成するにはどうすればよいですか?

リモートURLページをファイルとしてダウンロードし、生成されたファイルオブジェクトをマスターアクターに送信する必要があります。次に、マスターアクターはこのリクエストをパーサーアクターに送信してファイルを解析します。

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 に答える