Red Hat 㧠Solution Architect ã¨ã㦠Quarkus ãæ å½ãã¦ããä¼è¤ã¡ã²ãï¼@chiroitoï¼ã§ãã
ãã®è¨äºã¯ãQuarkus.io ã®ããã°è¨äºãConcurrent asynchronous actions with Mutiny ã®ç¿»è¨³è¨äºã§ãã
ä»é±ã¯ã並è¡å¦çãä¸å¿ã¨ããåºãæ®åãã¦ããã¦ã¼ã¹ã±ã¼ã¹ã«ã¤ãã¦èããã¾ããããã®ã¦ã¼ã¶ã¼ã¯ã2ã¤ã®ãã¤ã¯ããµã¼ãã¹ã並è¡ãã¦å¼ã³åºãã両æ¹ã®çµæãåºããããããçµåãã¦å¦çãç¶ãããã¨èãã¦ãã¾ãããåºæ¬çã«ã¯ä»¥ä¸ã®ãã¿ã¼ã³ã§ãã
éãªã¢ã¯ãã£ããªã¢ããã¼ãã§ã¯ã両æ¹ã®å¼ã³åºãã¯å¼ã³åºãå ã®ã¹ã¬ããããããã¯ããã¯ã¼ã«ã¼ã¹ã¬ãããã¼ã«ã使ç¨ãã¦ããå ´åãé¤ããå¼ã³åºãã¯åæé²è¡ã§ã¯ããã¾ãããã¯ã¼ã«ã¼ã¹ã¬ãããã¼ã«ã使ç¨ãã¦ãã¦ãããããã®ã¹ã¬ããã¯ãããã¯ããã¦ããå¯è½æ§ãé«ããç¡é§ã«ãªã½ã¼ã¹ãæ¶è²»ãã¦ãã¾ãã¾ãã
ããããå¿é ã¯ããã¾ãããQuarkus ã®ãªã¢ã¯ãã£ã㨠Mutiny ã¯ããã®ã·ããªãªãå¦çããããã®ãã¹ã¦ãæã£ã¦ãã¾ãã
2ã¤ã®ãµã¼ãã¹ãå¼ã³ã ãã
ä»åã®è¨äºã§ã¯ãVert.x Web Client ã¨ãããªã¢ã¯ãã£ã㪠HTTP ã¯ã©ã¤ã¢ã³ãã使ã£ã¦ã¿ã¾ãããã³ããããã³ã° I/O ãæ´»ç¨ãã¦ãé«ãããã©ã¼ãã³ã¹ã¨çã®ãã³ããããã³ã°ãå®ç¾ãã¦ãã¾ããé ããã¹ã¬ãããã¼ã«ã«ã¯ä¾åãã¾ãããQuarkus Rest Client ã使ããã¨ãã§ãã¾ããããããä»ã®ã¨ããã¯ã¼ã«ã¼ã¹ã¬ããã使ã£ã¦ãã¾ãã
ã©ã®ã¯ã©ã¤ã¢ã³ãã使ãã«ãã¦ããå¼ã³åºãããã«ããã¤ãã®ãªã¢ã¼ããµã¼ãã¹ãå¿ è¦ã§ãã使ã£ã¦ã¿ã¾ãããã
- https://programming-quotes-api.herokuapp.com/ - ããã°ã©ãã³ã°ã®è¦ç©ãããåã
- https://api.chucknorris.io/ - Chuck Norris ã«ã¤ãã¦ã®è¦ç©ãããåã
ã¾ãæåã«ãç§ãã¡ã®è¦ç©ããåå¾ããããã«å¿ è¦ãªã³ã¼ããè¦ã¦ã¿ã¾ããããã©ã¡ãã®ãµã¼ãã¹ãä¼¼ããããªãã®ã§ã¯ããã¾ããã対å¿ã®æ§é ãå°ãç°ãªãã¾ããçµå±ã¯ããã§çµããã§ããã
private static Uni<String> getProgrammingQuote(WebClient client) { return client.getAbs(PROGRAMMING_QUOTE) .as(BodyCodec.jsonObject()) .send() .onItem().transform(r -> r.body().getString("en") + " (" + r.body().getString("author") + ")"); } private static Uni<String> getChuckNorrisQuote(WebClient client) { return client.getAbs(CHUCK_NORRIS_QUOTE) .as(BodyCodec.jsonObject()) .send() .onItem().transform(r -> r.body().getString("value")); }
ããã2ã¤ã®ã¡ã½ããã¯ãWebClient
ãåãåãã¾ããããã¦ããµã¼ãã¹ãå¼ã³åºããJSON ã¬ã¹ãã³ã¹ãåå¾ãããããæ½åºãã¾ãããããã¯ä¸¡æ¹ã¨ã Uni
ãè¿ããããããããã¯éåæãªãã§ãããã®çµæï¼è¦ç©æ¸ï¼ã¯ãå©ç¨å¯è½ãªå ´åã«ã¯å¾ã»ã©æä¾ããã¾ããã¾ããUni
ãè¿ãã¨ãããã¨ã¯ãè¿ããã Uni
ã誰ãããµãã¹ã¯ã©ã¤ãããæã«ã®ã¿ãµã¼ãã¹ãèµ·åããã¨ãããã¨ã§ããè¤æ°åãµãã¹ã¯ã©ã¤ããã¦ããå ´åã¯ãè¤æ°åã«åãã¦å¼åºããããã¨ã«ãªãã¾ãã
Unis ã®çµã¿åãã
ä»ã®ã¨ããããµã¼ãã¹ãå¼ã³åºãæ¹æ³ã¯2ã¤ããã¾ããããããä¸ã®å³ã®ããã«åæã«ä¸¦è¡ã«å¼ã³ããã§ãã
Mutiny ã§ã¯ãUnis
ãçç£ããã¢ã¤ãã ãçµã¿åãããæ¹æ³ãæä¾ãã¦ãã¾ãã
Uni<Tuple2<String, String>> tuple = Uni.combine().all() .unis(getProgrammingQuote(client), getChuckNorrisQuote(client)) .asTuple();
誰ãã Uni tuple
ããµãã¹ã¯ã©ã¤ãããã¨ããµã¼ãã¹ãå¼ã³åºã getProgrammingQuote(client)
㨠getChuckNorrisQuote(client) Unis
ãè³¼èªãã¾ãããã®ããããããã®ãªã¯ã¨ã¹ããæ¾åºããã¾ãããµã¼ãã¹ã並è¡ãã¦å¼ã³åºããã¾ãã
両æ¹ã®è¿çãããå ´åã¯ãè¤æ°ã®é ç®ãéã¶ã·ã³ãã«ãªæ§é ä½ã§ããã¿ãã«ã«çµåãã¾ãã
ã¤ã¾ããç§ãã¡ã®ãµã¼ãã¹ãåæã«å¼ã³åºããã¨ã¯ãé常ã«ç°¡åãªãã¨ã§ããå®ç¾ããããµã¼ãã¹ãéåæã¢ã¯ã·ã§ã³ã表ã Unis
ãä½æãã¾ããããã¦ãUni.combine().all()
ã使ç¨ãã¦ããããçµåããã ãã§ãã ã¿ãã«ã使ç¨ãã¦çµæãçµåããããcombinator é¢æ°ã使ç¨ãã¦çµåãããã決ãããã¨ãã§ãã¾ãã
ãã¹ã¦ãã¾ã¨ãã
// Web Client ãä½æ WebClient client = WebClient.create(vertx); // 2ã¤ã®Unisã®çµæãã¿ãã«ã«çµå Uni.combine().all() .unis(getProgrammingQuote(client), getChuckNorrisQuote(client)) .asTuple() // ãµãã¹ã¯ã©ã¤ãï¼ãããå¼ã³åºãã®ããªã¬ã¼ã¨ãªãï¼ .subscribe().with(tuple -> { System.out.println("Programming Quote: " + tuple.getItem1()); System.out.println("Chuck Norris Quote: " + tuple.getItem2()); });
ããã ï¼ãã®ã³ã¼ããå®éã«åä½ãã¦ããã®ãè¦ããå ´åã¯ããã® gist ã確èªãã¦ãã ãããJBangã§ç´æ¥å®è¡ã§ãã¾ãã
jbang https://gist.github.com/cescoffier/1ed68bef12b798529e10350f77686e9a
楽ããã