Git Product home page Git Product logo

quarkus-concurrency's Introduction

Quarkus Concurrency in Practice

This browser does not support PDFs. Please download the PDF to view it: Download PDF.

Code snippets

Generate app

quarkus create app \
  --stream=3.0 \
  --maven --no-wrapper \
  -x quarkus-resteasy-reactive,quarkus-resteasy-reactive-jackson,quarkus-vertx \
  org.acme:quarkus-concurrency:1.0.0-SNAPSHOT

Add dep

<dependency>
    <groupId>io.smallrye.reactive</groupId>
    <artifactId>smallrye-mutiny-vertx-web-client</artifactId>
    <version>2.21.0</version>
</dependency>

Slide.3

@Path("/hello")
public class ReactiveGreetingResource {

    @GET
    @Path("imperative")
    @Produces(MediaType.TEXT_PLAIN)
    public String imperativeHello(@QueryParam("name") String name) {
        System.out.println(Thread.currentThread().getName());
        return greeting(name);
    }

    @GET
    @Path("reactive")
    @Produces(MediaType.TEXT_PLAIN)
    public CompletionStage<String> reactiveHello(@QueryParam("name") String name) {
        System.out.println(Thread.currentThread().getName());
        return CompletableFuture.supplyAsync(() -> greeting(name)).minimalCompletionStage();
    }

    private String greeting (String name) {
        return "Hello " + (null == name || name.isBlank() ? "World !" : name);
    }

}

Slide.16

@Path("/handleit")
public class ReactiveGreetingResource {

    @GET
    public void handleit() {
        Multi.createFrom().range(0,10)
                .onSubscription().invoke(sub -> System.out.println("Received Subscription: " + sub))
                .onRequest().invoke(req -> System.out.println("Got a request: " + req))
                .select().where((i -> i % 2 == 0))
                .onItem().transform(i -> i * 100);
    }

}
@Path("/handleit")
public class ReactiveGreetingResource {

    @GET
    public void handleit() {
        Multi.createFrom().range(0,10)
                .onSubscription().invoke(sub -> System.out.println("Received Subscription: " + sub))
                .onRequest().invoke(req -> System.out.println("Got a request: " + req))
                .select().where((i -> i % 2 == 0))
                .onItem().transform(i -> i * 100)
                .subscribe().with(
                        i -> System.out.println("i: " + i)
                );
    }

}

Slide.19

@Path("/hello")
public class ReactiveGreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        System.out.println(Thread.currentThread().getName());
        return "Hello RESTEasy Reactive";
    }

}
@Path("/hello")
public class ReactiveGreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<String> hello() {
        System.out.println(Thread.currentThread().getName());
        return Uni.createFrom().item("Hello RESTEasy Reactive");
    }

}
@Path("/hello")
public class ReactiveGreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Blocking    
    public Uni<String> hello() {
        System.out.println(Thread.currentThread().getName());
        return Uni.createFrom().item("Hello RESTEasy Reactive");
    }

}

Slide.24

@Path("/hello")
public class ReactiveGreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<String> hello() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        Thread.sleep(10000);
        return Uni.createFrom().item("Hello RESTEasy Reactive");
    }

}

Slide.25

@Path("/hello")
public class ReactiveGreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<String> hello() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        return Uni.createFrom().item("Hello RESTEasy Reactive").onItem().delayIt().by(Duration.ofSeconds(10));
    }

}

Slide.26

@Path("/hello")
public class ReactiveGreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<String> hello() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        Uni.createFrom().item("Mutiny").emitOn(Infrastructure.getDefaultWorkerPool()).subscribe().with(
                item -> System.out.println(item + ":" + Thread.currentThread().getName())
        );
        return Uni.createFrom().item("Hello RESTEasy Reactive");
    }

}

Slide.27

@Path("/hello")
public class ReactiveGreetingResource {

    @Inject
    EventBus bus;

    @GET
    @NonBlocking
    @Produces(MediaType.TEXT_PLAIN)
    public void hello() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
        Arrays.asList("Hello", "Aloha", "Konichiwa").forEach(
                item -> bus.<String>request("hi", item)
        );
    }

    @ConsumeEvent(value = "hi", blocking = true)
    public void doSomething(String greeting) {
        System.out.println(greeting + ":" + Thread.currentThread().getName());
    }

}

Slide.28

package org.acme;

import io.smallrye.mutiny.Uni;
import io.vertx.core.json.JsonObject;
import io.vertx.mutiny.core.Vertx;
import io.vertx.mutiny.ext.web.client.HttpResponse;
import io.vertx.mutiny.ext.web.client.WebClient;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/joke")
public class ReactiveGreetingResource {

    @Inject
    Vertx vertx;

    private final String URL = "https://api.chucknorris.io/jokes/random";

    @GET
    public Uni<JsonObject> chuckNorrisJokes() {
        return WebClient.create(vertx).getAbs(URL).send()
                .onItem().transform(HttpResponse::bodyAsJsonObject);
    }

}

quarkus-concurrency's People

Contributors

eformat avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.