• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

linkedin/parseq: Asynchronous Java made easier

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

linkedin/parseq

开源软件地址:

https://github.com/linkedin/parseq

开源编程语言:

Java 92.4%

开源软件介绍:

ParSeq

License Build Status

ParSeq is a framework that makes it easier to write asynchronous code in Java.

Some of the key benefits of ParSeq include:

Our Wiki includes an introductory example, a User's Guide, javadoc, and more.

See CHANGELOG for list of changes.

Introductory Example

In this example we show how to fetch several pages in parallel and how to combine them once they've all been retrieved.

You can find source code here: IntroductoryExample.

First we can retrieve a single page using an asynchronous HTTP client as follows:

    final Task<Response> google = HttpClient.get("http://www.google.com").task();
    engine.run(google);
    google.await();
    System.out.println("Google Page: " + google.get().getResponseBody());

This will print:

Google Page: <!doctype html><html>...

In this code snippet we don't really get any benefit from ParSeq. Essentially we create a task that can be run asynchronously, but then we block for completion using google.await(). In this case, the code is more complicated than issuing a simple synchronous call. We can improve this by making it asynchronous:

    final Task<String> google = HttpClient.get("http://www.google.com").task()
        .map(Response::getResponseBody)
        .andThen(body -> System.out.println("Google Page: " + body));

    engine.run(google);

We used map method to transform Response into the String and andThen method to print out result. Now, let's expand the example so that we can fetch a few more pages in parallel. First, let's create a helper method that creates a task responsible for fetching page body given a URL.

    private Task<String> fetchBody(String url) {
      return HttpClient.get(url).task().map(Response::getResponseBody);
    }

Next, we will compose tasks to run in parallel using Task.par.

    final Task<String> google = fetchBody("http://www.google.com");
    final Task<String> yahoo = fetchBody("http://www.yahoo.com");
    final Task<String> bing = fetchBody("http://www.bing.com");

    final Task<String> plan = Task.par(google, yahoo, bing)
        .map((g, y, b) -> "Google Page: " + g +" \n" +
                          "Yahoo Page: " + y + "\n" +
                          "Bing Page: " + b + "\n")
        .andThen(System.out::println);

    engine.run(plan);

This example is fully asynchronous. The home pages for Google, Yahoo, and Bing are all fetched in parallel while the original thread has returned to the calling code. We used Tasks.par to tell the engine to parallelize these HTTP requests. Once all of the responses have been retrieved they are transformed into a String that is finally printed out.

We can do various transforms on the data we retrieved. Here's a very simple transform that sums the length of the 3 pages that were fetched:

    final Task<Integer> sumLengths =
        Task.par(google.map(String::length),
                 yahoo.map(String::length),
                 bing.map(String::length))
             .map("sum", (g, y, b) -> g + y + b);

The sumLengths task can be given to an engine for execution and its result value will be set to the sum of the length of the 3 fetched pages.

Notice that we added descriptions to tasks. e.g. map("sum", (g, y, b) -> g + y + b). Using ParSeq's trace visualization tools we can visualize execution of the plan. Waterfall graph shows tasks execution in time (notice how all GET requests are executed in parallel):

sum-lengths-waterfall-example.png

Graphviz diagram best describes relationships between tasks:

sum-lengths-graphviz-example.png

For more in-depth description of ParSeq please visit User's Guide.

For many more examples, please see the parseq-examples contrib project in the source code.

Build

Build and test whole parseq code ./gradlew clean build

Build ParSeq subproject(modules) instead of the whole project: ./gradlew :<module_name>:build

Building on MacOS Catalina (>=10.15): Follow this guide to install the required Xcode Command Line Tools and add below environment variables

export LDFLAGS="-mmacosx-version-min=10.13"
export CXXFLAGS="-mmacosx-version-min=10.13"



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap