io.github.matzoliv

async-channels-core

Лицензия

Лицензия

Группа

Группа

io.github.matzoliv
Идентификатор

Идентификатор

async-channels-core
Последняя версия

Последняя версия

0.0.1-alpha
Дата

Дата

Тип

Тип

jar
Описание

Описание

io.github.matzoliv
async-channels-core
Система контроля версий

Система контроля версий

https://github.com/matzoliv/async-channels

Скачать async-channels-core

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/io.github.matzoliv/async-channels-core/ -->
<dependency>
    <groupId>io.github.matzoliv</groupId>
    <artifactId>async-channels-core</artifactId>
    <version>0.0.1-alpha</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.matzoliv/async-channels-core/
implementation 'io.github.matzoliv:async-channels-core:0.0.1-alpha'
// https://jarcasting.com/artifacts/io.github.matzoliv/async-channels-core/
implementation ("io.github.matzoliv:async-channels-core:0.0.1-alpha")
'io.github.matzoliv:async-channels-core:jar:0.0.1-alpha'
<dependency org="io.github.matzoliv" name="async-channels-core" rev="0.0.1-alpha">
  <artifact name="async-channels-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.matzoliv', module='async-channels-core', version='0.0.1-alpha')
)
libraryDependencies += "io.github.matzoliv" % "async-channels-core" % "0.0.1-alpha"
[io.github.matzoliv/async-channels-core "0.0.1-alpha"]

Зависимости

Библиотека не имеет зависимостей. Это самодостаточное приложение, которое не зависит ни от каких других библиотек.

Модули Проекта

Данный проект не имеет модулей.

Java implementation of Clojure's core.async channels. The important logic was transposed with little modifications. It has no dependencies and uses built in standard Future APIs in both Java and Scala.

Ping pong example in Java :

import io.github.matzoliv.asyncchannel.AsyncChannel;
import io.github.matzoliv.asyncchannel.Channels;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

public class Test {
    public static CompletableFuture<Void> pingPongLoop(String id, int loopLeft, AsyncChannel in, AsyncChannel out) {
        return in.readAsync()
                .thenComposeAsync(msg -> {
                    System.out.println(String.format("Task %s: received %s, left %d", id, msg, loopLeft));
                    if (msg.equals("ping")) {
                        return out.putAsync("pong");
                    } else {
                        return out.putAsync("ping");
                    }
                })
                .thenComposeAsync((Void x) -> {
                    if (loopLeft > 1) {
                        return pingPongLoop(id, loopLeft - 1, in, out);
                    } else {
                        return CompletableFuture.completedFuture(null);
                    }
                });
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
        AsyncChannel c1 = Channels.create();
        AsyncChannel c2 = Channels.create();

        c1.putAsync("ping");

        CompletableFuture.anyOf(
            pingPongLoop("A", 100, c1, c2),
            pingPongLoop("B", 100, c2, c1)
        ).get();
    }
}

Ping pong example in Scala :

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global

object Test {

  def main(args: Array[String]) = {

    def loop(id: String, loopLeft: Int, in: AsyncChannel, out: AsyncChannel): Future[Unit] = for {
      msg <- in.readAsync()
      _ <- {
        println(s"Task $id received $msg, left $loopLeft")
        out.putAsync(if (msg == "ping") { "pong" } else { "ping" })
      }
      _ <- if (loopLeft > 0) {
        loop(id, loopLeft - 1, in, out)
      } else {
        Future()
      }
    } yield ()

    val c1 = Channels.create()
    val c2 = Channels.create()

    c1.putAsync("ping")

    Await.result(Future.firstCompletedOf(Seq(
      loop("A", 100, c1, c2),
      loop("B", 100, c2, c1)
    )), atMost = 10 seconds)
  }
}

Версии библиотеки

Версия
0.0.1-alpha