8

在阅读 play2 文档时,我发现了这一点:

由于 Play 2.0 的工作方式,动作代码必须尽可能快(即非阻塞)。那么如果我们还不能计算它,我们应该返回什么结果呢?回应应该是对结果的承诺!

哇!这当然让我对playakkaakka 感兴趣。我目前正在构建一个与弹性搜索集成的自动完成应用程序,所以这将是一个完美的选择!

控制器:

public class AutoComplete extends Controller {

    @BodyParser.Of(value = BodyParser.Json.class)
    public static Result complete(final String term) {

        F.Promise<List<String>> list = Akka.future(new Callable<List<String>>() {
            public List<String> call() throws Exception {
                List<String> list = IndexService.find(term);
                return list;
            }
        });    

        return async(list.map(new F.Function<List<String>, Result>() {
            @Override
            public Result apply(List<String> list) throws Throwable {
                return ok(Json.toJson(list));
            }
        }));
}

服务:

public static List<String> find(final String term) {

        IndexQuery < SearchWord > query = SearchWord.find.query();
        query.setQuery("{\n" +
                "    \"bool\": {\n" +
                "        \"should\": [\n" +
                "            {\n" +
                "                \"text\": {\n" +
                "                    \"search_word.ngrams\": {\n" +
                "                        \"operator\": \"and\",\n" +
                "                        \"query\": \""+term+"\"\n" +
                "                    }\n" +
                "                }\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\": {\n" +
                "                    \"search_word.full\": {\n" +
                "                        \"boost\": 1,\n" +
                "                        \"query\": \""+term+"\"\n" +
                "                    }\n" +
                "                }\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}");
        IndexResults<SearchWord> indexResults = SearchWord.find.search(query);

        List<String> list = new ArrayList<String>();
        for(SearchWord word : indexResults.getResults()){
            list.add(word.getWord());
        }

        return list;
    }
}

搜索词:

@IndexType(name = "search_word")
public class SearchWord extends Index {

    // Find method static for request
    public static Index.Finder<SearchWord> find = new Index.Finder<SearchWord>(SearchWord.class);

    public enum WordType {
        NAME,
        STRONG_SEARCH_WORD,
        WEAK_SEARCH_WORD,
        BANNED
    }

    private String word;
    private WordType wordType;

    public SearchWord() {
    }

    public SearchWord(IndexWord indexWord) {
        super.id = ""+indexWord.getId();
        this.word = StringUtils.lowerCase(indexWord.getWord());
        this.wordType = WordType.valueOf(indexWord.getType());
    }

    public String getId() {
        return super.id;
    }

    public void setId(String id) {
        super.id = id;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public WordType getWordType() {
        return wordType;
    }

    public void setWordType(WordType wordType) {
        this.wordType = wordType;
    }

    @Override
    public Map toIndex() {
        HashMap map = new HashMap();
        map.put("id", super.id);
        map.put("word", word);
        map.put("word_type", wordType.toString());
        return map;
    }

    @Override
    public Indexable fromIndex(Map map) {
        if (map == null) {
            return this;
        }
        this.word = (String) map.get("word");
        this.wordType = WordType.valueOf((String)map.get("word_type"));
        return this;
    }


}

该代码运行良好,但我必须说我不确定我是否正确实现了这一点。我真的很难理解文档。所以我的问题基本上是:

  1. 我是否正确实施了 Future 和 Promise?
  2. 创建一个自定义演员会更好,并在该演员中执行索引搜索,如文档中的示例:

=====

 return async(
        Akka.asPromise(ask(myActor,"hello", 1000)).map(
          new Function<Object,Result>() {
            public Result apply(Object response) {
              return ok(response.toString());
            }
          }
        )
      );
  1. 也许你有一些我还没有找到的好例子?
4

2 回答 2

2

AFAIK,你的代码完全没问题。

我可能错了,但我认为第二个选项严格等同于第一个选项,因为该Akka.future()方法是该方法的包装器Akka.promise()

来自Play 2.0.4 的 Akka 类源代码

/**
 * Executes a block of code asynchronously in the application Akka Actor system.
 */
public static <T> Promise<T> future(java.util.concurrent.Callable<T> callable) {
    return asPromise(akka.dispatch.Futures.future(callable, system().dispatcher()));
}
于 2013-01-03T10:17:58.040 回答
0

尽管您已经正确实现了 Promise 和 Future,但我不认为这段代码是“非阻塞的”......

似乎阻塞调用是

List<String> list = IndexService.find(term);

虽然这现在包含在一个承诺/未来中,但它仍然是一个阻塞调用......如果你想真正做到非阻塞(具有所有好处),你必须让你的数据访问(查询)非- 阻止...

哦,一个非阻塞的动作方法应该返回一个结果的承诺,而不是一个结果......

这就是我应该如何编写您的代码:

@BodyParser.Of(value = BodyParser.Json.class)
public static F.Promise<Result> complete(final String term) {    
    scala.concurrent.Future<List<String>> listFuture = IndexService.find(term);

    F.Promise<List<String>> listPromise = F.Promise.wrap(listFuture);

    return listPromise.map(new F.Function<List<String>, Result>() {
       @Override
       public Result apply(List<String> list) throws Throwable {
          return ok(Json.toJson(list));
       }
    });
}

希望这可以帮助!

于 2014-01-08T18:04:17.867 回答