BAEL-3863 Introduction to Finagle
This commit is contained in:
parent
5d81fd3f6f
commit
20843c7f22
|
@ -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)
|
|
@ -0,0 +1,30 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>Finagle</artifactId>
|
||||
<name>Finagle</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.twitter</groupId>
|
||||
<artifactId>finagle-core_2.13</artifactId>
|
||||
<version>20.4.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.twitter</groupId>
|
||||
<artifactId>finagle-http_2.13</artifactId>
|
||||
<version>20.4.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -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<Request, Response> service = new LogFilter().andThen(Http.newService(":8080"));
|
||||
Request request = Request.apply(Method.Get(), "/?name=John");
|
||||
request.host("localhost");
|
||||
Future<Response> response = service.apply(request);
|
||||
Await.result(response
|
||||
.onSuccess(r -> {
|
||||
System.out.println(r.getContentString());
|
||||
return BoxedUnit.UNIT;
|
||||
}));
|
||||
}
|
||||
}
|
|
@ -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<Request, Response> {
|
||||
@Override
|
||||
public Future<Response> apply(Request request) {
|
||||
String greeting = "Hello " + request.getParam("name");
|
||||
Reader<Buf> reader = Reader.fromBuf(new Buf.ByteArray(greeting.getBytes(), 0, greeting.length()));
|
||||
return Future.value(Response.apply(request.version(), Status.Ok(), reader));
|
||||
}
|
||||
}
|
|
@ -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<Request, Response> {
|
||||
@Override
|
||||
public Future<Response> apply(Request request, Service<Request, Response> 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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,4 @@
|
|||
oauth.consumerKey=//TODO
|
||||
oauth.consumerSecret=//TODO
|
||||
oauth.accessToken=//TODO
|
||||
oauth.accessTokenSecret=//TODO
|
Loading…
Reference in New Issue