From 20843c7f224fe68e772b0518532c881fff483483 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 01:56:25 +0200 Subject: [PATCH 1/9] BAEL-3863 Introduction to Finagle --- finagle/README.md | 7 +++++ finagle/pom.xml | 30 +++++++++++++++++++ .../java/com/baeldung/finagle/Client.java | 24 +++++++++++++++ .../com/baeldung/finagle/GreetingService.java | 18 +++++++++++ .../java/com/baeldung/finagle/LogFilter.java | 17 +++++++++++ .../java/com/baeldung/finagle/Server.java | 15 ++++++++++ finagle/src/main/resources/logback.xml | 13 ++++++++ .../src/main/resources/twitter4j.properties | 4 +++ 8 files changed, 128 insertions(+) create mode 100644 finagle/README.md create mode 100644 finagle/pom.xml create mode 100644 finagle/src/main/java/com/baeldung/finagle/Client.java create mode 100644 finagle/src/main/java/com/baeldung/finagle/GreetingService.java create mode 100644 finagle/src/main/java/com/baeldung/finagle/LogFilter.java create mode 100644 finagle/src/main/java/com/baeldung/finagle/Server.java create mode 100644 finagle/src/main/resources/logback.xml create mode 100644 finagle/src/main/resources/twitter4j.properties diff --git a/finagle/README.md b/finagle/README.md new file mode 100644 index 0000000000..d16afdc10d --- /dev/null +++ b/finagle/README.md @@ -0,0 +1,7 @@ +## Finagle + +This module contains articles about Finagle and associated libraries. + +### Relevant articles + +- [Introduction to Finagle](https://www.baeldung.com/introduction-to-finagle) diff --git a/finagle/pom.xml b/finagle/pom.xml new file mode 100644 index 0000000000..f1dc932c13 --- /dev/null +++ b/finagle/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + Finagle + Finagle + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + com.twitter + finagle-core_2.13 + 20.4.0 + + + com.twitter + finagle-http_2.13 + 20.4.0 + + + + + + diff --git a/finagle/src/main/java/com/baeldung/finagle/Client.java b/finagle/src/main/java/com/baeldung/finagle/Client.java new file mode 100644 index 0000000000..a881f1c37c --- /dev/null +++ b/finagle/src/main/java/com/baeldung/finagle/Client.java @@ -0,0 +1,24 @@ +package com.baeldung.finagle; + +import com.twitter.finagle.Http; +import com.twitter.finagle.Service; +import com.twitter.finagle.http.Method; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.util.Await; +import com.twitter.util.Future; +import scala.runtime.BoxedUnit; + +public class Client { + public static void main(String[] args) throws Exception { + Service service = new LogFilter().andThen(Http.newService(":8080")); + Request request = Request.apply(Method.Get(), "/?name=John"); + request.host("localhost"); + Future response = service.apply(request); + Await.result(response + .onSuccess(r -> { + System.out.println(r.getContentString()); + return BoxedUnit.UNIT; + })); + } +} diff --git a/finagle/src/main/java/com/baeldung/finagle/GreetingService.java b/finagle/src/main/java/com/baeldung/finagle/GreetingService.java new file mode 100644 index 0000000000..c795724192 --- /dev/null +++ b/finagle/src/main/java/com/baeldung/finagle/GreetingService.java @@ -0,0 +1,18 @@ +package com.baeldung.finagle; + +import com.twitter.finagle.Service; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.finagle.http.Status; +import com.twitter.io.Buf; +import com.twitter.io.Reader; +import com.twitter.util.Future; + +public class GreetingService extends Service { + @Override + public Future apply(Request request) { + String greeting = "Hello " + request.getParam("name"); + Reader reader = Reader.fromBuf(new Buf.ByteArray(greeting.getBytes(), 0, greeting.length())); + return Future.value(Response.apply(request.version(), Status.Ok(), reader)); + } +} diff --git a/finagle/src/main/java/com/baeldung/finagle/LogFilter.java b/finagle/src/main/java/com/baeldung/finagle/LogFilter.java new file mode 100644 index 0000000000..d381456cfe --- /dev/null +++ b/finagle/src/main/java/com/baeldung/finagle/LogFilter.java @@ -0,0 +1,17 @@ +package com.baeldung.finagle; + +import com.twitter.finagle.Service; +import com.twitter.finagle.SimpleFilter; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.util.Future; + +public class LogFilter extends SimpleFilter { + @Override + public Future apply(Request request, Service service) { + System.out.println("Request host:" + request.host().getOrElse(() -> "")); + System.out.println("Request params:"); + request.getParams().forEach(entry -> System.out.println("\t" + entry.getKey() + " : " + entry.getValue())); + return service.apply(request); + } +} diff --git a/finagle/src/main/java/com/baeldung/finagle/Server.java b/finagle/src/main/java/com/baeldung/finagle/Server.java new file mode 100644 index 0000000000..8a9c9be192 --- /dev/null +++ b/finagle/src/main/java/com/baeldung/finagle/Server.java @@ -0,0 +1,15 @@ +package com.baeldung.finagle; + +import com.twitter.finagle.Http; +import com.twitter.finagle.ListeningServer; +import com.twitter.finagle.Service; +import com.twitter.util.Await; +import com.twitter.util.TimeoutException; + +public class Server { + public static void main(String[] args) throws TimeoutException, InterruptedException { + Service service = new LogFilter().andThen(new GreetingService()); + ListeningServer server = Http.serve(":8080", service); + Await.ready(server); + } +} diff --git a/finagle/src/main/resources/logback.xml b/finagle/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/finagle/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/finagle/src/main/resources/twitter4j.properties b/finagle/src/main/resources/twitter4j.properties new file mode 100644 index 0000000000..ee11dc62a1 --- /dev/null +++ b/finagle/src/main/resources/twitter4j.properties @@ -0,0 +1,4 @@ +oauth.consumerKey=//TODO +oauth.consumerSecret=//TODO +oauth.accessToken=//TODO +oauth.accessTokenSecret=//TODO From 33edef47e18c16e03ce056af609dc984d6f4a566 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 02:05:49 +0200 Subject: [PATCH 2/9] BAEL-3863 add Finagle module to root pom.xml --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 34703c6f73..813a42c93d 100644 --- a/pom.xml +++ b/pom.xml @@ -418,6 +418,7 @@ ethereum feign + finagle flyway-cdi-extension geotools From ff684f13b6d4d27f063d0e912ca9e6b0151acb0e Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 20:52:23 +0200 Subject: [PATCH 3/9] BAEL-3863 moved Finagle package to libraries-rpc, added test --- {finagle => libraries-rpc}/README.md | 0 {finagle => libraries-rpc}/pom.xml | 4 +- .../com/baeldung/rpc}/finagle/Client.java | 2 +- .../rpc}/finagle/GreetingService.java | 2 +- .../com/baeldung/rpc}/finagle/LogFilter.java | 2 +- .../com/baeldung/rpc}/finagle/Server.java | 2 +- .../src/main/resources/logback.xml | 0 .../src/main/resources/twitter4j.properties | 0 .../rpc/finagle/ClientServerTest.java | 40 +++++++++++++++++++ pom.xml | 2 +- 10 files changed, 47 insertions(+), 7 deletions(-) rename {finagle => libraries-rpc}/README.md (100%) rename {finagle => libraries-rpc}/pom.xml (92%) rename {finagle/src/main/java/com/baeldung => libraries-rpc/src/main/java/com/baeldung/rpc}/finagle/Client.java (96%) rename {finagle/src/main/java/com/baeldung => libraries-rpc/src/main/java/com/baeldung/rpc}/finagle/GreetingService.java (95%) rename {finagle/src/main/java/com/baeldung => libraries-rpc/src/main/java/com/baeldung/rpc}/finagle/LogFilter.java (95%) rename {finagle/src/main/java/com/baeldung => libraries-rpc/src/main/java/com/baeldung/rpc}/finagle/Server.java (93%) rename {finagle => libraries-rpc}/src/main/resources/logback.xml (100%) rename {finagle => libraries-rpc}/src/main/resources/twitter4j.properties (100%) create mode 100644 libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java diff --git a/finagle/README.md b/libraries-rpc/README.md similarity index 100% rename from finagle/README.md rename to libraries-rpc/README.md diff --git a/finagle/pom.xml b/libraries-rpc/pom.xml similarity index 92% rename from finagle/pom.xml rename to libraries-rpc/pom.xml index f1dc932c13..145693cac1 100644 --- a/finagle/pom.xml +++ b/libraries-rpc/pom.xml @@ -2,8 +2,8 @@ 4.0.0 - Finagle - Finagle + libraries-rpc + libraries-rpc jar diff --git a/finagle/src/main/java/com/baeldung/finagle/Client.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java similarity index 96% rename from finagle/src/main/java/com/baeldung/finagle/Client.java rename to libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java index a881f1c37c..989c79f302 100644 --- a/finagle/src/main/java/com/baeldung/finagle/Client.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java @@ -1,4 +1,4 @@ -package com.baeldung.finagle; +package com.baeldung.rpc.finagle; import com.twitter.finagle.Http; import com.twitter.finagle.Service; diff --git a/finagle/src/main/java/com/baeldung/finagle/GreetingService.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java similarity index 95% rename from finagle/src/main/java/com/baeldung/finagle/GreetingService.java rename to libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java index c795724192..7f3d741f94 100644 --- a/finagle/src/main/java/com/baeldung/finagle/GreetingService.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java @@ -1,4 +1,4 @@ -package com.baeldung.finagle; +package com.baeldung.rpc.finagle; import com.twitter.finagle.Service; import com.twitter.finagle.http.Request; diff --git a/finagle/src/main/java/com/baeldung/finagle/LogFilter.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java similarity index 95% rename from finagle/src/main/java/com/baeldung/finagle/LogFilter.java rename to libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java index d381456cfe..3ef57ed18f 100644 --- a/finagle/src/main/java/com/baeldung/finagle/LogFilter.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java @@ -1,4 +1,4 @@ -package com.baeldung.finagle; +package com.baeldung.rpc.finagle; import com.twitter.finagle.Service; import com.twitter.finagle.SimpleFilter; diff --git a/finagle/src/main/java/com/baeldung/finagle/Server.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java similarity index 93% rename from finagle/src/main/java/com/baeldung/finagle/Server.java rename to libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java index 8a9c9be192..5bc8d93eaf 100644 --- a/finagle/src/main/java/com/baeldung/finagle/Server.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java @@ -1,4 +1,4 @@ -package com.baeldung.finagle; +package com.baeldung.rpc.finagle; import com.twitter.finagle.Http; import com.twitter.finagle.ListeningServer; diff --git a/finagle/src/main/resources/logback.xml b/libraries-rpc/src/main/resources/logback.xml similarity index 100% rename from finagle/src/main/resources/logback.xml rename to libraries-rpc/src/main/resources/logback.xml diff --git a/finagle/src/main/resources/twitter4j.properties b/libraries-rpc/src/main/resources/twitter4j.properties similarity index 100% rename from finagle/src/main/resources/twitter4j.properties rename to libraries-rpc/src/main/resources/twitter4j.properties diff --git a/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java new file mode 100644 index 0000000000..b3faa1968c --- /dev/null +++ b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java @@ -0,0 +1,40 @@ +package com.baeldung.rpc.finagle; + +import com.twitter.finagle.Http; +import com.twitter.finagle.Service; +import com.twitter.finagle.http.Method; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.util.Await; +import com.twitter.util.Future; +import org.junit.Test; +import scala.runtime.BoxedUnit; + +import static org.junit.Assert.assertEquals; + +public class ClientServerTest { + @Test + public void clientShouldReceiveResponseFromServer() throws Exception { + // given + Service serverService = new LogFilter().andThen(new GreetingService()); + Http.serve(":8080", serverService); + + Service clientService = new LogFilter().andThen(Http.newService(":8080")); + + // when + Request request = Request.apply(Method.Get(), "/?name=John"); + request.host("localhost"); + Future response = clientService.apply(request); + + // then + Await.result(response + .onSuccess(r -> { + assertEquals("Hello John", r.getContentString()); + return BoxedUnit.UNIT; + }) + .onFailure(r -> { + throw new RuntimeException(r); + }) + ); + } +} diff --git a/pom.xml b/pom.xml index 813a42c93d..c16775bfc9 100644 --- a/pom.xml +++ b/pom.xml @@ -418,7 +418,6 @@ ethereum feign - finagle flyway-cdi-extension geotools @@ -509,6 +508,7 @@ libraries-http-2 libraries-io libraries-primitive + libraries-rpc libraries-security libraries-server libraries-testing From 3e0c3a0d0a621a3dcf909314fffb5a25b3a17a93 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 20:54:23 +0200 Subject: [PATCH 4/9] BAEL-3863 removed README.md --- libraries-rpc/README.md | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 libraries-rpc/README.md diff --git a/libraries-rpc/README.md b/libraries-rpc/README.md deleted file mode 100644 index d16afdc10d..0000000000 --- a/libraries-rpc/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## Finagle - -This module contains articles about Finagle and associated libraries. - -### Relevant articles - -- [Introduction to Finagle](https://www.baeldung.com/introduction-to-finagle) From ca7d40de8fdca6741021f8273613a8ee53f7c907 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 20:59:20 +0200 Subject: [PATCH 5/9] BAEL-3863 removed redundant examples --- .../java/com/baeldung/rpc/finagle/Client.java | 24 ------------------- .../java/com/baeldung/rpc/finagle/Server.java | 15 ------------ 2 files changed, 39 deletions(-) delete mode 100644 libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java delete mode 100644 libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java diff --git a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java deleted file mode 100644 index 989c79f302..0000000000 --- a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.rpc.finagle; - -import com.twitter.finagle.Http; -import com.twitter.finagle.Service; -import com.twitter.finagle.http.Method; -import com.twitter.finagle.http.Request; -import com.twitter.finagle.http.Response; -import com.twitter.util.Await; -import com.twitter.util.Future; -import scala.runtime.BoxedUnit; - -public class Client { - public static void main(String[] args) throws Exception { - Service service = new LogFilter().andThen(Http.newService(":8080")); - Request request = Request.apply(Method.Get(), "/?name=John"); - request.host("localhost"); - Future response = service.apply(request); - Await.result(response - .onSuccess(r -> { - System.out.println(r.getContentString()); - return BoxedUnit.UNIT; - })); - } -} diff --git a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java deleted file mode 100644 index 5bc8d93eaf..0000000000 --- a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.rpc.finagle; - -import com.twitter.finagle.Http; -import com.twitter.finagle.ListeningServer; -import com.twitter.finagle.Service; -import com.twitter.util.Await; -import com.twitter.util.TimeoutException; - -public class Server { - public static void main(String[] args) throws TimeoutException, InterruptedException { - Service service = new LogFilter().andThen(new GreetingService()); - ListeningServer server = Http.serve(":8080", service); - Await.ready(server); - } -} From cf6cbe92b9e229c28a354d683d6a3cb6a2d8eb3a Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sat, 25 Apr 2020 18:07:27 +0200 Subject: [PATCH 6/9] BAEL-3863 Renamed tests to follow convention --- .../{ClientServerTest.java => FinagleIntegrationTest.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename libraries-rpc/src/test/java/com/baeldung/rpc/finagle/{ClientServerTest.java => FinagleIntegrationTest.java} (88%) diff --git a/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java similarity index 88% rename from libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java rename to libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java index b3faa1968c..8dcdb19e7e 100644 --- a/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java +++ b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java @@ -12,9 +12,9 @@ import scala.runtime.BoxedUnit; import static org.junit.Assert.assertEquals; -public class ClientServerTest { +public class FinagleIntegrationTest { @Test - public void clientShouldReceiveResponseFromServer() throws Exception { + public void givenServerAndClient_whenRequestSent_thenClientShouldReceiveResponseFromServer() throws Exception { // given Service serverService = new LogFilter().andThen(new GreetingService()); Http.serve(":8080", serverService); From 6deafc178d25d2b6247074bf02249349591dba4c Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sun, 26 Apr 2020 16:15:17 +0200 Subject: [PATCH 7/9] BAEL-3863 replace stdouts with logger --- .../main/java/com/baeldung/rpc/finagle/LogFilter.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java index 3ef57ed18f..7fc5440016 100644 --- a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java @@ -5,13 +5,18 @@ import com.twitter.finagle.SimpleFilter; import com.twitter.finagle.http.Request; import com.twitter.finagle.http.Response; import com.twitter.util.Future; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class LogFilter extends SimpleFilter { + + private static final Logger logger = LoggerFactory.getLogger(LogFilter.class); + @Override public Future apply(Request request, Service service) { - System.out.println("Request host:" + request.host().getOrElse(() -> "")); - System.out.println("Request params:"); - request.getParams().forEach(entry -> System.out.println("\t" + entry.getKey() + " : " + entry.getValue())); + logger.info("Request host:" + request.host().getOrElse(() -> "")); + logger.info("Request params:"); + request.getParams().forEach(entry -> logger.info("\t" + entry.getKey() + " : " + entry.getValue())); return service.apply(request); } } From 7da38b99cb4d8f4d02ef4eac8efc4143433671d6 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sun, 26 Apr 2020 16:25:51 +0200 Subject: [PATCH 8/9] BAEL-3863 add versions properties --- libraries-rpc/pom.xml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libraries-rpc/pom.xml b/libraries-rpc/pom.xml index 145693cac1..8741a41062 100644 --- a/libraries-rpc/pom.xml +++ b/libraries-rpc/pom.xml @@ -16,15 +16,20 @@ com.twitter finagle-core_2.13 - 20.4.0 + ${finagle.core.version} com.twitter finagle-http_2.13 - 20.4.0 + ${finagle.http.version} + + 20.4.0 + 20.4.0 + + From 6709017cc02040e210265e91a9792fdc405d6aef Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sat, 9 May 2020 18:01:17 +0200 Subject: [PATCH 9/9] BAEL-3863 removed redundant twitter4j.properties file --- libraries-rpc/src/main/resources/twitter4j.properties | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 libraries-rpc/src/main/resources/twitter4j.properties diff --git a/libraries-rpc/src/main/resources/twitter4j.properties b/libraries-rpc/src/main/resources/twitter4j.properties deleted file mode 100644 index ee11dc62a1..0000000000 --- a/libraries-rpc/src/main/resources/twitter4j.properties +++ /dev/null @@ -1,4 +0,0 @@ -oauth.consumerKey=//TODO -oauth.consumerSecret=//TODO -oauth.accessToken=//TODO -oauth.accessTokenSecret=//TODO