[JAVA-13854] Half list moved (#12598)
* [JAVA-13854] added parent module * [JAVA-13854] moved apache-tapestry(submodule) to web-modules(parent) * [JAVA-13854] moved bootique(submodule) to web-modules(parent) * [JAVA-13854] moved dropwizard(submodule) to web-modules(parent) * [JAVA-13854] moved blade(submodule) to web-modules(parent) * [JAVA-13854] moved java-lite(submodule) to web-modules(parent) * [JAVA-13854] moved jooby(submodule) to web-modules(parent) * [JAVA-13854] moved linkrest(submodule) to web-modules(parent) * [JAVA-13854] moved ninja(submodule) to web-modules(parent) * [JAVA-13854] moved ratpack(submodule) to web-modules(parent) * [JAVA-13854] moved resteasy(submodule) to web-modules(parent) * [JAVA-13854] moved restx(submodule) to web-modules(parent) * [JAVA-13854] moved spark-java(submodule) to web-modules(parent) * [JAVA-13854] moved vraptor(submodule) to web-modules(parent) * [JAVA-13854] delete modules that were moved * [JAVA-13854] * [JAVA-13854] * [JAVA-13854] delete ninja submodule + moved raml(submodule) to web-modules(parent) * [JAVA-13854] moved gwt(submodule) to web-modules(parent) * [JAVA-13854] moved jakarta-ee(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets-2(submodule) to web-modules(parent) * [JAVA-13854] moved jee-7(submodule) to web-modules(parent) * [JAVA-13854] moved play-framework(not a module) to web-modules * [JAVA-13854] fix failing test * [JAVA-13854] moved struts-2(submodule) to web-modules(parent) * [JAVA-13854] moved wicket(submodule) to web-modules(parent) * [JAVA-13854] deleted modules that were moved to web-modules * JAVA-13854 Removed moved modules from the main pom.xml Co-authored-by: panagiotiskakos <[email protected]> Co-authored-by: Dhawal Kapil <[email protected]>
This commit is contained in:
committed by
GitHub
co-authored by
panagiotiskakos
Dhawal Kapil
parent
842a71ad92
commit
0b9549bd3e
@@ -0,0 +1,14 @@
|
||||
## Ratpack
|
||||
|
||||
This module contains articles about Ratpack.
|
||||
|
||||
### Relevant articles
|
||||
|
||||
- [Introduction to Ratpack](https://www.baeldung.com/ratpack)
|
||||
- [Ratpack Google Guice Integration](https://www.baeldung.com/ratpack-google-guice)
|
||||
- [Ratpack Integration with Spring Boot](http://www.baeldung.com/ratpack-spring-boot)
|
||||
- [Ratpack with Hystrix](https://www.baeldung.com/ratpack-hystrix)
|
||||
- [Ratpack HTTP Client](https://www.baeldung.com/ratpack-http-client)
|
||||
- [Ratpack with RxJava](https://www.baeldung.com/ratpack-rxjava)
|
||||
- [Ratpack with Groovy](https://www.baeldung.com/ratpack-groovy)
|
||||
- [Reactive Streams API with Ratpack](https://www.baeldung.com/ratpack-reactive-streams-api)
|
||||
@@ -0,0 +1,37 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath "io.ratpack:ratpack-gradle:1.5.4"
|
||||
classpath "com.h2database:h2:1.4.193"
|
||||
}
|
||||
}
|
||||
|
||||
if (!JavaVersion.current().java8Compatible) {
|
||||
throw new IllegalStateException("Must be built with Java 8 or higher")
|
||||
}
|
||||
|
||||
apply plugin: "io.ratpack.ratpack-java"
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'groovy'
|
||||
apply plugin: 'io.ratpack.ratpack-groovy'
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile ratpack.dependency('hikari')
|
||||
compile 'com.h2database:h2:1.4.193'
|
||||
testCompile 'junit:junit:4.11'
|
||||
runtime "org.slf4j:slf4j-simple:1.7.21"
|
||||
}
|
||||
|
||||
test {
|
||||
testLogging {
|
||||
events 'started', 'passed'
|
||||
}
|
||||
}
|
||||
|
||||
mainClassName = "com.baeldung.Application"
|
||||
@@ -0,0 +1,97 @@
|
||||
<?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>ratpack</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>ratpack</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>web-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-spring-boot-starter</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-core</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-sql</artifactId>
|
||||
<version>${groovy.sql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-hikari</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-groovy-test</artifactId>
|
||||
<version>${ratpack.test.latest.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-hystrix</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.netflix.hystrix</groupId>
|
||||
<artifactId>hystrix-core</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.hystrix</groupId>
|
||||
<artifactId>hystrix-core</artifactId>
|
||||
<version>${hystrix.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-rx</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.ratpack</groupId>
|
||||
<artifactId>ratpack-test</artifactId>
|
||||
<version>${ratpack.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>${httpcore.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<ratpack.version>1.5.4</ratpack.version>
|
||||
<httpclient.version>4.5.3</httpclient.version>
|
||||
<httpcore.version>4.4.6</httpcore.version>
|
||||
<hystrix.version>1.5.12</hystrix.version>
|
||||
<groovy.sql.version>2.4.15</groovy.sql.version>
|
||||
<ratpack.test.latest.version>1.6.1</ratpack.test.latest.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung;
|
||||
|
||||
@Grab('io.ratpack:ratpack-groovy:1.6.1')
|
||||
import static ratpack.groovy.Groovy.ratpack
|
||||
|
||||
import com.baeldung.model.User
|
||||
import com.google.common.reflect.TypeToken
|
||||
import ratpack.exec.Promise
|
||||
import ratpack.handling.Context
|
||||
import ratpack.jackson.Jackson
|
||||
import groovy.sql.Sql
|
||||
import java.sql.Connection
|
||||
import java.sql.PreparedStatement
|
||||
import java.sql.ResultSet
|
||||
import ratpack.hikari.HikariModule
|
||||
import javax.sql.DataSource;
|
||||
|
||||
ratpack {
|
||||
serverConfig { port(5050) }
|
||||
bindings {
|
||||
module(HikariModule) { config ->
|
||||
config.dataSourceClassName = 'org.h2.jdbcx.JdbcDataSource'
|
||||
config.addDataSourceProperty('URL', "jdbc:h2:mem:devDB;INIT=RUNSCRIPT FROM 'classpath:/User.sql'")
|
||||
}
|
||||
}
|
||||
|
||||
handlers {
|
||||
|
||||
get { render 'Hello World from Ratpack with Groovy!!' }
|
||||
|
||||
get("greet/:name") { Context ctx ->
|
||||
render "Hello " + ctx.getPathTokens().get("name") + "!!!"
|
||||
}
|
||||
|
||||
get("data") {
|
||||
render Jackson.json([title: "Mr", name: "Norman", country: "USA"])
|
||||
}
|
||||
|
||||
post("user") {
|
||||
Promise<User> user = parse(Jackson.fromJson(User))
|
||||
user.then { u -> render u.name }
|
||||
}
|
||||
|
||||
get('fetchUserName/:id') { Context ctx ->
|
||||
Connection connection = ctx.get(DataSource.class).getConnection()
|
||||
PreparedStatement queryStatement = connection.prepareStatement("SELECT NAME FROM USER WHERE ID=?")
|
||||
queryStatement.setInt(1, Integer.parseInt(ctx.getPathTokens().get("id")))
|
||||
ResultSet resultSet = queryStatement.executeQuery()
|
||||
resultSet.next()
|
||||
render resultSet.getString(1)
|
||||
}
|
||||
|
||||
get('fetchUsers') {
|
||||
def db = [url:'jdbc:h2:mem:devDB']
|
||||
def sql = Sql.newInstance(db.url, db.user, db.password)
|
||||
def users = sql.rows("SELECT * FROM USER");
|
||||
render(Jackson.json(users))
|
||||
}
|
||||
|
||||
post('addUser') {
|
||||
parse(Jackson.fromJson(User))
|
||||
.then { u ->
|
||||
def db = [url:'jdbc:h2:mem:devDB']
|
||||
Sql sql = Sql.newInstance(db.url, db.user, db.password)
|
||||
sql.executeInsert("INSERT INTO USER VALUES (?,?,?,?)", [
|
||||
u.id,
|
||||
u.title,
|
||||
u.name,
|
||||
u.country
|
||||
])
|
||||
render "User $u.name inserted"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung;
|
||||
|
||||
public class RatpackGroovyApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
File file = new File("src/main/groovy/com/baeldung/Ratpack.groovy");
|
||||
def shell = new GroovyShell()
|
||||
shell.evaluate(file)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.model
|
||||
|
||||
class User {
|
||||
|
||||
long id
|
||||
String title
|
||||
String name
|
||||
String country
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.filter.RequestValidatorFilter;
|
||||
import com.baeldung.handler.EmployeeHandler;
|
||||
import com.baeldung.handler.RedirectHandler;
|
||||
import com.baeldung.model.Employee;
|
||||
import com.baeldung.repository.EmployeeRepository;
|
||||
import com.baeldung.repository.EmployeeRepositoryImpl;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import ratpack.func.Action;
|
||||
import ratpack.func.Function;
|
||||
import ratpack.guice.BindingsSpec;
|
||||
import ratpack.guice.Guice;
|
||||
import ratpack.handling.Chain;
|
||||
import ratpack.hikari.HikariModule;
|
||||
import ratpack.http.client.HttpClient;
|
||||
import ratpack.jackson.Jackson;
|
||||
import ratpack.registry.Registry;
|
||||
import ratpack.server.RatpackServer;
|
||||
import ratpack.server.RatpackServerSpec;
|
||||
import ratpack.server.ServerConfig;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
final Action<HikariConfig> hikariConfigAction = hikariConfig -> {
|
||||
hikariConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
|
||||
hikariConfig.addDataSourceProperty("URL", "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'");
|
||||
};
|
||||
|
||||
final Action<BindingsSpec> bindingsSpecAction = bindings -> bindings.module(HikariModule.class, hikariConfigAction);
|
||||
final HttpClient httpClient = HttpClient.of(httpClientSpec -> {
|
||||
httpClientSpec.poolSize(10)
|
||||
.connectTimeout(Duration.of(60, ChronoUnit.SECONDS))
|
||||
.maxContentLength(ServerConfig.DEFAULT_MAX_CONTENT_LENGTH)
|
||||
.responseMaxChunkSize(16384)
|
||||
.readTimeout(Duration.of(60, ChronoUnit.SECONDS))
|
||||
.byteBufAllocator(PooledByteBufAllocator.DEFAULT);
|
||||
});
|
||||
final Function<Registry, Registry> registryFunction = Guice.registry(bindingsSpecAction);
|
||||
|
||||
final Action<Chain> chainAction = chain -> chain.all(new RequestValidatorFilter())
|
||||
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
|
||||
.get("data/employees", ctx -> ctx.render(Jackson.json(createEmpList())))
|
||||
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens()
|
||||
.get("name") + "!!!"))
|
||||
.post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens()
|
||||
.get("amount") + " added successfully !!!"));
|
||||
|
||||
final Action<Chain> routerChainAction = routerChain -> {
|
||||
routerChain.path("redirect", new RedirectHandler())
|
||||
.prefix("employee", empChain -> {
|
||||
empChain.get(":id", new EmployeeHandler());
|
||||
});
|
||||
};
|
||||
final Action<RatpackServerSpec> ratpackServerSpecAction = serverSpec -> serverSpec.registry(registryFunction)
|
||||
.registryOf(registrySpec -> {
|
||||
registrySpec.add(EmployeeRepository.class, new EmployeeRepositoryImpl());
|
||||
registrySpec.add(HttpClient.class, httpClient);
|
||||
})
|
||||
.handlers(chain -> chain.insert(routerChainAction)
|
||||
.insert(chainAction));
|
||||
|
||||
RatpackServer.start(ratpackServerSpecAction);
|
||||
}
|
||||
|
||||
private static List<Employee> createEmpList() {
|
||||
List<Employee> employees = new ArrayList<>();
|
||||
employees.add(new Employee(1L, "Mr", "John Doe"));
|
||||
employees.add(new Employee(2L, "Mr", "White Snow"));
|
||||
return employees;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.filter;
|
||||
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
import ratpack.http.MutableHeaders;
|
||||
|
||||
public class RequestValidatorFilter implements Handler {
|
||||
|
||||
@Override
|
||||
public void handle(Context ctx) throws Exception {
|
||||
MutableHeaders headers = ctx.getResponse().getHeaders();
|
||||
headers.set("Access-Control-Allow-Origin", "*");
|
||||
ctx.next();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.guice;
|
||||
|
||||
import com.baeldung.guice.config.DependencyModule;
|
||||
import com.baeldung.guice.service.DataPumpService;
|
||||
import com.baeldung.guice.service.ServiceFactory;
|
||||
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
|
||||
|
||||
import ratpack.guice.Guice;
|
||||
import ratpack.server.RatpackServer;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
RatpackServer
|
||||
.start(server -> server.registry(Guice.registry(bindings -> bindings.module(DependencyModule.class)))
|
||||
.handlers(chain -> chain.get("randomString", ctx -> {
|
||||
DataPumpService dataPumpService = ctx.get(DataPumpService.class);
|
||||
ctx.render(dataPumpService.generate());
|
||||
}).get("factory", ctx -> ctx.render(ServiceFactory.getInstance().generate()))));
|
||||
|
||||
// RatpackServer.start(server -> server
|
||||
// .registry(Guice
|
||||
// .registry(bindings -> bindings.bindInstance(DataPumpService.class, new DataPumpServiceImpl())))
|
||||
// .handlers(chain -> chain.get("randomString", ctx -> {
|
||||
// DataPumpService dataPumpService = ctx.get(DataPumpService.class);
|
||||
// ctx.render(dataPumpService.generate());
|
||||
// }).get("factory", ctx -> ctx.render(ServiceFactory.getInstance().generate()))));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.guice.config;
|
||||
|
||||
import com.baeldung.guice.service.DataPumpService;
|
||||
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Scopes;
|
||||
|
||||
public class DependencyModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(DataPumpService.class).to(DataPumpServiceImpl.class)
|
||||
.in(Scopes.SINGLETON);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.guice.service;
|
||||
|
||||
public interface DataPumpService {
|
||||
|
||||
String generate();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.guice.service;
|
||||
|
||||
import com.baeldung.guice.service.impl.DataPumpServiceImpl;
|
||||
|
||||
public class ServiceFactory {
|
||||
|
||||
private static DataPumpService instance;
|
||||
|
||||
public static void setInstance(DataPumpService dataPumpService) {
|
||||
instance = dataPumpService;
|
||||
}
|
||||
|
||||
public static DataPumpService getInstance() {
|
||||
if (instance == null) {
|
||||
return new DataPumpServiceImpl();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.guice.service.impl;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import com.baeldung.guice.service.DataPumpService;
|
||||
|
||||
public class DataPumpServiceImpl implements DataPumpService {
|
||||
|
||||
@Override
|
||||
public String generate() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.handler;
|
||||
|
||||
import com.baeldung.repository.EmployeeRepository;
|
||||
import com.baeldung.model.Employee;
|
||||
import ratpack.exec.Promise;
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
|
||||
public class EmployeeHandler implements Handler {
|
||||
@Override
|
||||
public void handle(Context ctx) throws Exception {
|
||||
EmployeeRepository repository = ctx.get(EmployeeRepository.class);
|
||||
Long id = Long.valueOf(ctx.getPathTokens()
|
||||
.get("id"));
|
||||
Promise<Employee> employeePromise = repository.findEmployeeById(id);
|
||||
employeePromise.map(employee -> employee.getName())
|
||||
.then(name -> ctx.getResponse()
|
||||
.send(name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.handler;
|
||||
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
|
||||
public class FooHandler implements Handler {
|
||||
@Override
|
||||
public void handle(Context ctx) throws Exception {
|
||||
ctx.getResponse()
|
||||
.send("Hello Foo!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.handler;
|
||||
|
||||
import ratpack.exec.Promise;
|
||||
import ratpack.handling.Context;
|
||||
import ratpack.handling.Handler;
|
||||
import ratpack.http.client.HttpClient;
|
||||
import ratpack.http.client.ReceivedResponse;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
public class RedirectHandler implements Handler {
|
||||
@Override
|
||||
public void handle(Context ctx) throws Exception {
|
||||
HttpClient client = ctx.get(HttpClient.class);
|
||||
URI uri = URI.create("http://localhost:5050/employee/1");
|
||||
Promise<ReceivedResponse> responsePromise = client.get(uri);
|
||||
responsePromise.map(response -> response.getBody()
|
||||
.getText()
|
||||
.toUpperCase())
|
||||
.then(responseText -> ctx.getResponse()
|
||||
.send(responseText));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandProperties;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class HystrixAsyncHttpCommand extends HystrixCommand<String> {
|
||||
|
||||
private URI uri;
|
||||
private RequestConfig requestConfig;
|
||||
|
||||
HystrixAsyncHttpCommand(URI uri, int timeoutMillis) {
|
||||
super(Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-async"))
|
||||
.andCommandPropertiesDefaults(HystrixCommandProperties
|
||||
.Setter()
|
||||
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
|
||||
requestConfig = RequestConfig
|
||||
.custom()
|
||||
.setSocketTimeout(timeoutMillis)
|
||||
.setConnectTimeout(timeoutMillis)
|
||||
.setConnectionRequestTimeout(timeoutMillis)
|
||||
.build();
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String run() throws Exception {
|
||||
return EntityUtils.toString(HttpClientBuilder
|
||||
.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setDefaultHeaders(Collections.singleton(new BasicHeader("User-Agent", "Baeldung Blocking HttpClient")))
|
||||
.build()
|
||||
.execute(new HttpGet(uri))
|
||||
.getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFallback() {
|
||||
return "eugenp's async fallback profile";
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandProperties;
|
||||
import com.netflix.hystrix.HystrixObservableCommand;
|
||||
import ratpack.http.client.HttpClient;
|
||||
import ratpack.rx.RxRatpack;
|
||||
import rx.Observable;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class HystrixReactiveHttpCommand extends HystrixObservableCommand<String> {
|
||||
|
||||
private HttpClient httpClient;
|
||||
private URI uri;
|
||||
|
||||
HystrixReactiveHttpCommand(HttpClient httpClient, URI uri, int timeoutMillis) {
|
||||
super(Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-reactive"))
|
||||
.andCommandPropertiesDefaults(HystrixCommandProperties
|
||||
.Setter()
|
||||
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
|
||||
this.httpClient = httpClient;
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Observable<String> construct() {
|
||||
return RxRatpack.observe(httpClient
|
||||
.get(uri, requestSpec -> requestSpec.headers(mutableHeaders -> mutableHeaders.add("User-Agent", "Baeldung HttpClient")))
|
||||
.map(receivedResponse -> receivedResponse
|
||||
.getBody()
|
||||
.getText()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Observable<String> resumeWithFallback() {
|
||||
return Observable.just("eugenp's reactive fallback profile");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandProperties;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class HystrixSyncHttpCommand extends HystrixCommand<String> {
|
||||
|
||||
private URI uri;
|
||||
private RequestConfig requestConfig;
|
||||
|
||||
HystrixSyncHttpCommand(URI uri, int timeoutMillis) {
|
||||
super(Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-sync"))
|
||||
.andCommandPropertiesDefaults(HystrixCommandProperties
|
||||
.Setter()
|
||||
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
|
||||
requestConfig = RequestConfig
|
||||
.custom()
|
||||
.setSocketTimeout(timeoutMillis)
|
||||
.setConnectTimeout(timeoutMillis)
|
||||
.setConnectionRequestTimeout(timeoutMillis)
|
||||
.build();
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String run() throws Exception {
|
||||
HttpGet request = new HttpGet(uri);
|
||||
return EntityUtils.toString(HttpClientBuilder
|
||||
.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setDefaultHeaders(Collections.singleton(new BasicHeader("User-Agent", "Baeldung Blocking HttpClient")))
|
||||
.build()
|
||||
.execute(request)
|
||||
.getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFallback() {
|
||||
return "eugenp's sync fallback profile";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import ratpack.guice.Guice;
|
||||
import ratpack.http.client.HttpClient;
|
||||
import ratpack.hystrix.HystrixMetricsEventStreamHandler;
|
||||
import ratpack.hystrix.HystrixModule;
|
||||
import ratpack.server.RatpackServer;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
public class RatpackHystrixApp {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
final int timeout = Integer.valueOf(System.getProperty("ratpack.hystrix.timeout"));
|
||||
final URI eugenGithubProfileUri = new URI("https://api.github.com/users/eugenp");
|
||||
|
||||
RatpackServer.start(server -> server
|
||||
.registry(Guice.registry(bindingsSpec -> bindingsSpec.module(new HystrixModule().sse())))
|
||||
.handlers(chain -> chain
|
||||
.get("rx", ctx -> new HystrixReactiveHttpCommand(ctx.get(HttpClient.class), eugenGithubProfileUri, timeout)
|
||||
.toObservable()
|
||||
.subscribe(ctx::render))
|
||||
.get("async", ctx -> ctx.render(new HystrixAsyncHttpCommand(eugenGithubProfileUri, timeout)
|
||||
.queue()
|
||||
.get()))
|
||||
.get("sync", ctx -> ctx.render(new HystrixSyncHttpCommand(eugenGithubProfileUri, timeout).execute()))
|
||||
.get("hystrix", new HystrixMetricsEventStreamHandler())));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Employee implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3077867088762010705L;
|
||||
|
||||
private Long id;
|
||||
private String title;
|
||||
private String name;
|
||||
|
||||
public Employee(Long id, String title, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.model;
|
||||
/**
|
||||
*
|
||||
*POJO class for Movie object
|
||||
*/
|
||||
public class Movie {
|
||||
|
||||
private String name;
|
||||
|
||||
private String year;
|
||||
|
||||
private String director;
|
||||
|
||||
private Double rating;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public void setYear(String year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public String getDirector() {
|
||||
return director;
|
||||
}
|
||||
|
||||
public void setDirector(String director) {
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
public Double getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(Double rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public class Quote {
|
||||
|
||||
private Instant ts;
|
||||
private String symbol;
|
||||
private double value;
|
||||
|
||||
public Quote() {}
|
||||
|
||||
|
||||
public Quote(Instant ts, String symbol, double value) {
|
||||
this.ts = ts;
|
||||
this.symbol = symbol;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the ts
|
||||
*/
|
||||
public Instant getTs() {
|
||||
return ts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ts the ts to set
|
||||
*/
|
||||
public void setTs(Instant ts) {
|
||||
this.ts = ts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the symbol
|
||||
*/
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param symbol the symbol to set
|
||||
*/
|
||||
public void setSymbol(String symbol) {
|
||||
this.symbol = symbol;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the value
|
||||
*/
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param value the value to set
|
||||
*/
|
||||
public void setValue(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((symbol == null) ? 0 : symbol.hashCode());
|
||||
result = prime * result + ((ts == null) ? 0 : ts.hashCode());
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(value);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Quote other = (Quote) obj;
|
||||
if (symbol == null) {
|
||||
if (other.symbol != null)
|
||||
return false;
|
||||
} else if (!symbol.equals(other.symbol))
|
||||
return false;
|
||||
if (ts == null) {
|
||||
if (other.ts != null)
|
||||
return false;
|
||||
} else if (!ts.equals(other.ts))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(value) != Double.doubleToLongBits(other.value))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Quote [ts=" + ts + ", symbol=" + symbol + ", value=" + value + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.model.Employee;
|
||||
import ratpack.exec.Promise;
|
||||
|
||||
public interface EmployeeRepository {
|
||||
|
||||
Promise<Employee> findEmployeeById(Long id) throws Exception;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.model.Employee;
|
||||
import ratpack.exec.Promise;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class EmployeeRepositoryImpl implements EmployeeRepository {
|
||||
|
||||
private static final Map<Long, Employee> EMPLOYEE_MAP = new HashMap<>();
|
||||
|
||||
public EmployeeRepositoryImpl() {
|
||||
EMPLOYEE_MAP.put(1L, new Employee(1L, "Ms", "Jane Doe"));
|
||||
EMPLOYEE_MAP.put(2L, new Employee(2L, "Mr", "NY"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Promise<Employee> findEmployeeById(Long id) throws Exception {
|
||||
return Promise.async(downstream -> {
|
||||
Thread.sleep(500);
|
||||
downstream.success(EMPLOYEE_MAP.get(id));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.rxjava;
|
||||
|
||||
import ratpack.error.ServerErrorHandler;
|
||||
import ratpack.rx.RxRatpack;
|
||||
import ratpack.server.RatpackServer;
|
||||
import rx.Observable;
|
||||
|
||||
public class RatpackErrorHandlingApp {
|
||||
|
||||
/**
|
||||
* Try hitting http://localhost:5050/error to see the error handler in action
|
||||
* @param args
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
RxRatpack.initialize();
|
||||
RatpackServer.start(def -> def.registryOf(regSpec -> regSpec.add(ServerErrorHandler.class, (ctx, throwable) -> {
|
||||
ctx.render("Error caught by handler : " + throwable.getMessage());
|
||||
}))
|
||||
.handlers(chain -> chain.get("error", ctx -> {
|
||||
Observable.<String> error(new Exception("Error from observable"))
|
||||
.subscribe(s -> {
|
||||
});
|
||||
})));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.rxjava;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.model.Movie;
|
||||
import com.baeldung.rxjava.service.MoviePromiseService;
|
||||
import com.baeldung.rxjava.service.impl.MoviePromiseServiceImpl;
|
||||
|
||||
import ratpack.exec.Promise;
|
||||
import ratpack.handling.Handler;
|
||||
import ratpack.jackson.Jackson;
|
||||
import ratpack.rx.RxRatpack;
|
||||
import ratpack.server.RatpackServer;
|
||||
|
||||
public class RatpackObserveApp {
|
||||
/**
|
||||
* Try hitting http://localhost:5050/movies or http://localhost:5050/movie to see the application in action.
|
||||
*
|
||||
* @param args
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
RxRatpack.initialize();
|
||||
|
||||
Handler moviePromiseHandler = ctx -> {
|
||||
MoviePromiseService promiseSvc = ctx.get(MoviePromiseService.class);
|
||||
Promise<Movie> moviePromise = promiseSvc.getMovie();
|
||||
RxRatpack.observe(moviePromise)
|
||||
.subscribe(movie -> ctx.render(Jackson.json(movie)));
|
||||
};
|
||||
|
||||
Handler moviesPromiseHandler = ctx -> {
|
||||
MoviePromiseService promiseSvc = ctx.get(MoviePromiseService.class);
|
||||
Promise<List<Movie>> moviePromises = promiseSvc.getMovies();
|
||||
RxRatpack.observeEach(moviePromises)
|
||||
.toList()
|
||||
.subscribe(movie -> ctx.render(Jackson.json(movie)));
|
||||
};
|
||||
|
||||
RatpackServer.start(def -> def.registryOf(regSpec -> regSpec.add(MoviePromiseService.class, new MoviePromiseServiceImpl()))
|
||||
.handlers(chain -> chain.get("movie", moviePromiseHandler)
|
||||
.get("movies", moviesPromiseHandler)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.rxjava;
|
||||
|
||||
import com.baeldung.model.Movie;
|
||||
import com.baeldung.rxjava.service.MovieObservableService;
|
||||
import com.baeldung.rxjava.service.impl.MovieObservableServiceImpl;
|
||||
|
||||
import ratpack.jackson.Jackson;
|
||||
import ratpack.rx.RxRatpack;
|
||||
import ratpack.server.RatpackServer;
|
||||
import rx.Observable;
|
||||
|
||||
public class RatpackParallelismApp {
|
||||
|
||||
/**
|
||||
* Try hitting http://localhost:5050/movies to see the application in action.
|
||||
*
|
||||
* @param args
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
RxRatpack.initialize();
|
||||
RatpackServer.start(def -> def.registryOf(regSpec -> regSpec.add(MovieObservableService.class, new MovieObservableServiceImpl()))
|
||||
.handlers(chain -> chain.get("movies", ctx -> {
|
||||
MovieObservableService movieSvc = ctx.get(MovieObservableService.class);
|
||||
Observable<Movie> movieObs = movieSvc.getMovies();
|
||||
Observable<String> upperCasedNames = movieObs.compose(RxRatpack::forkEach)
|
||||
.map(movie -> movie.getName()
|
||||
.toUpperCase())
|
||||
.serialize();
|
||||
RxRatpack.promise(upperCasedNames)
|
||||
.then(movie -> {
|
||||
ctx.render(Jackson.json(movie));
|
||||
});
|
||||
})));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.rxjava;
|
||||
|
||||
import com.baeldung.model.Movie;
|
||||
import com.baeldung.rxjava.service.MovieObservableService;
|
||||
import com.baeldung.rxjava.service.impl.MovieObservableServiceImpl;
|
||||
|
||||
import ratpack.handling.Handler;
|
||||
import ratpack.jackson.Jackson;
|
||||
import ratpack.rx.RxRatpack;
|
||||
import ratpack.server.RatpackServer;
|
||||
import rx.Observable;
|
||||
|
||||
public class RatpackPromiseApp {
|
||||
|
||||
/**
|
||||
* Try hitting http://localhost:5050/movies or http://localhost:5050/movie to see the application in action.
|
||||
*
|
||||
* @param args
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
RxRatpack.initialize();
|
||||
|
||||
Handler movieHandler = (ctx) -> {
|
||||
MovieObservableService movieSvc = ctx.get(MovieObservableService.class);
|
||||
Observable<Movie> movieObs = movieSvc.getMovie();
|
||||
RxRatpack.promiseSingle(movieObs)
|
||||
.then(movie -> ctx.render(Jackson.json(movie)));
|
||||
};
|
||||
|
||||
Handler moviesHandler = (ctx) -> {
|
||||
MovieObservableService movieSvc = ctx.get(MovieObservableService.class);
|
||||
Observable<Movie> movieObs = movieSvc.getMovies();
|
||||
RxRatpack.promise(movieObs)
|
||||
.then(movie -> ctx.render(Jackson.json(movie)));
|
||||
};
|
||||
|
||||
RatpackServer.start(def -> def.registryOf(rSpec -> rSpec.add(MovieObservableService.class, new MovieObservableServiceImpl()))
|
||||
.handlers(chain -> chain.get("movie", movieHandler)
|
||||
.get("movies", moviesHandler)));
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.rxjava.service;
|
||||
|
||||
import com.baeldung.model.Movie;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
public interface MovieObservableService {
|
||||
|
||||
Observable<Movie> getMovies();
|
||||
|
||||
Observable<Movie> getMovie();
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user