Skip to content

Commit 053e7c4

Browse files
the-java-guyjzheaux
authored andcommitted
BAEL-774 (eugenp#6234)
* BAEL-774 * BAEL 774 : Refactored methods * BAEL-774 Refactored to new module. * BAEL 774 - CRs * BAEL 774 - Moved code back to ratpack module * Removed modules reactive-modules and ratpack-rxjava * Refactored packages
1 parent 2634bdc commit 053e7c4

10 files changed

+301
-1
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@
570570
<module>rxjava</module>
571571
<module>rxjava-2</module>
572572
<module>software-security/sql-injection-samples</module>
573-
574573
</modules>
575574

576575
</profile>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.model;
2+
/**
3+
*
4+
*POJO class for Movie object
5+
*/
6+
public class Movie {
7+
8+
private String name;
9+
10+
private String year;
11+
12+
private String director;
13+
14+
private Double rating;
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getYear() {
25+
return year;
26+
}
27+
28+
public void setYear(String year) {
29+
this.year = year;
30+
}
31+
32+
public String getDirector() {
33+
return director;
34+
}
35+
36+
public void setDirector(String director) {
37+
this.director = director;
38+
}
39+
40+
public Double getRating() {
41+
return rating;
42+
}
43+
44+
public void setRating(Double rating) {
45+
this.rating = rating;
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.rxjava;
2+
3+
import ratpack.error.ServerErrorHandler;
4+
import ratpack.rx.RxRatpack;
5+
import ratpack.server.RatpackServer;
6+
import rx.Observable;
7+
8+
public class RatpackErrorHandlingApp {
9+
10+
/**
11+
* Try hitting http://localhost:5050/error to see the error handler in action
12+
* @param args
13+
* @throws Exception
14+
*/
15+
16+
public static void main(String[] args) throws Exception {
17+
RxRatpack.initialize();
18+
RatpackServer.start(def -> def.registryOf(regSpec -> regSpec.add(ServerErrorHandler.class, (ctx, throwable) -> {
19+
ctx.render("Error caught by handler : " + throwable.getMessage());
20+
}))
21+
.handlers(chain -> chain.get("error", ctx -> {
22+
Observable.<String> error(new Exception("Error from observable"))
23+
.subscribe(s -> {
24+
});
25+
})));
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.rxjava;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.model.Movie;
6+
import com.baeldung.rxjava.service.MoviePromiseService;
7+
import com.baeldung.rxjava.service.impl.MoviePromiseServiceImpl;
8+
9+
import ratpack.exec.Promise;
10+
import ratpack.handling.Handler;
11+
import ratpack.jackson.Jackson;
12+
import ratpack.rx.RxRatpack;
13+
import ratpack.server.RatpackServer;
14+
15+
public class RatpackObserveApp {
16+
/**
17+
* Try hitting http://localhost:5050/movies or http://localhost:5050/movie to see the application in action.
18+
*
19+
* @param args
20+
* @throws Exception
21+
*/
22+
public static void main(String[] args) throws Exception {
23+
RxRatpack.initialize();
24+
25+
Handler moviePromiseHandler = ctx -> {
26+
MoviePromiseService promiseSvc = ctx.get(MoviePromiseService.class);
27+
Promise<Movie> moviePromise = promiseSvc.getMovie();
28+
RxRatpack.observe(moviePromise)
29+
.subscribe(movie -> ctx.render(Jackson.json(movie)));
30+
};
31+
32+
Handler moviesPromiseHandler = ctx -> {
33+
MoviePromiseService promiseSvc = ctx.get(MoviePromiseService.class);
34+
Promise<List<Movie>> moviePromises = promiseSvc.getMovies();
35+
RxRatpack.observeEach(moviePromises)
36+
.toList()
37+
.subscribe(movie -> ctx.render(Jackson.json(movie)));
38+
};
39+
40+
RatpackServer.start(def -> def.registryOf(regSpec -> regSpec.add(MoviePromiseService.class, new MoviePromiseServiceImpl()))
41+
.handlers(chain -> chain.get("movie", moviePromiseHandler)
42+
.get("movies", moviesPromiseHandler)));
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.rxjava;
2+
3+
import com.baeldung.model.Movie;
4+
import com.baeldung.rxjava.service.MovieObservableService;
5+
import com.baeldung.rxjava.service.impl.MovieObservableServiceImpl;
6+
7+
import ratpack.jackson.Jackson;
8+
import ratpack.rx.RxRatpack;
9+
import ratpack.server.RatpackServer;
10+
import rx.Observable;
11+
12+
public class RatpackParallelismApp {
13+
14+
/**
15+
* Try hitting http://localhost:5050/movies to see the application in action.
16+
*
17+
* @param args
18+
* @throws Exception
19+
*/
20+
public static void main(String[] args) throws Exception {
21+
RxRatpack.initialize();
22+
RatpackServer.start(def -> def.registryOf(regSpec -> regSpec.add(MovieObservableService.class, new MovieObservableServiceImpl()))
23+
.handlers(chain -> chain.get("movies", ctx -> {
24+
MovieObservableService movieSvc = ctx.get(MovieObservableService.class);
25+
Observable<Movie> movieObs = movieSvc.getMovies();
26+
Observable<String> upperCasedNames = movieObs.compose(RxRatpack::forkEach)
27+
.map(movie -> movie.getName()
28+
.toUpperCase())
29+
.serialize();
30+
RxRatpack.promise(upperCasedNames)
31+
.then(movie -> {
32+
ctx.render(Jackson.json(movie));
33+
});
34+
})));
35+
}
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.rxjava;
2+
3+
import com.baeldung.model.Movie;
4+
import com.baeldung.rxjava.service.MovieObservableService;
5+
import com.baeldung.rxjava.service.impl.MovieObservableServiceImpl;
6+
7+
import ratpack.handling.Handler;
8+
import ratpack.jackson.Jackson;
9+
import ratpack.rx.RxRatpack;
10+
import ratpack.server.RatpackServer;
11+
import rx.Observable;
12+
13+
public class RatpackPromiseApp {
14+
15+
/**
16+
* Try hitting http://localhost:5050/movies or http://localhost:5050/movie to see the application in action.
17+
*
18+
* @param args
19+
* @throws Exception
20+
*/
21+
public static void main(String[] args) throws Exception {
22+
RxRatpack.initialize();
23+
24+
Handler movieHandler = (ctx) -> {
25+
MovieObservableService movieSvc = ctx.get(MovieObservableService.class);
26+
Observable<Movie> movieObs = movieSvc.getMovie();
27+
RxRatpack.promiseSingle(movieObs)
28+
.then(movie -> ctx.render(Jackson.json(movie)));
29+
};
30+
31+
Handler moviesHandler = (ctx) -> {
32+
MovieObservableService movieSvc = ctx.get(MovieObservableService.class);
33+
Observable<Movie> movieObs = movieSvc.getMovies();
34+
RxRatpack.promise(movieObs)
35+
.then(movie -> ctx.render(Jackson.json(movie)));
36+
};
37+
38+
RatpackServer.start(def -> def.registryOf(rSpec -> rSpec.add(MovieObservableService.class, new MovieObservableServiceImpl()))
39+
.handlers(chain -> chain.get("movie", movieHandler)
40+
.get("movies", moviesHandler)));
41+
}
42+
43+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.rxjava.service;
2+
3+
import com.baeldung.model.Movie;
4+
5+
import rx.Observable;
6+
7+
public interface MovieObservableService {
8+
9+
Observable<Movie> getMovies();
10+
11+
Observable<Movie> getMovie();
12+
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.rxjava.service;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.model.Movie;
6+
7+
import ratpack.exec.Promise;
8+
9+
public interface MoviePromiseService {
10+
11+
Promise<List<Movie>> getMovies();
12+
13+
Promise<Movie> getMovie();
14+
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.rxjava.service.impl;
2+
3+
import com.baeldung.model.Movie;
4+
import com.baeldung.rxjava.service.MovieObservableService;
5+
6+
import rx.Observable;
7+
8+
public class MovieObservableServiceImpl implements MovieObservableService {
9+
10+
@Override
11+
public Observable<Movie> getMovie() {
12+
Movie movie = new Movie();
13+
movie.setName("The Godfather");
14+
movie.setYear("1972");
15+
movie.setDirector("Coppola");
16+
movie.setRating(9.2);
17+
return Observable.just(movie);
18+
}
19+
20+
@Override
21+
public Observable<Movie> getMovies() {
22+
Movie movie = new Movie();
23+
movie.setName("The Godfather");
24+
movie.setYear("1972");
25+
movie.setDirector("Coppola");
26+
movie.setRating(9.2);
27+
Movie movie2 = new Movie();
28+
movie2.setName("The Godfather Part 2");
29+
movie2.setYear("1974");
30+
movie2.setDirector("Coppola");
31+
movie2.setRating(9.0);
32+
return Observable.just(movie, movie2);
33+
34+
}
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.rxjava.service.impl;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.baeldung.model.Movie;
7+
import com.baeldung.rxjava.service.MoviePromiseService;
8+
9+
import ratpack.exec.Promise;
10+
11+
public class MoviePromiseServiceImpl implements MoviePromiseService {
12+
13+
@Override
14+
public Promise<Movie> getMovie() {
15+
Movie movie = new Movie();
16+
movie.setName("The Godfather");
17+
movie.setYear("1972");
18+
movie.setDirector("Coppola");
19+
movie.setRating(9.2);
20+
return Promise.value(movie);
21+
}
22+
23+
@Override
24+
public Promise<List<Movie>> getMovies() {
25+
Movie movie = new Movie();
26+
movie.setName("The Godfather");
27+
movie.setYear("1972");
28+
movie.setDirector("Coppola");
29+
movie.setRating(9.2);
30+
Movie movie2 = new Movie();
31+
movie2.setName("The Godfather Part 2");
32+
movie2.setYear("1974");
33+
movie2.setDirector("Coppola");
34+
movie2.setRating(9.0);
35+
List<Movie> movies = new ArrayList<>();
36+
movies.add(movie);
37+
movies.add(movie2);
38+
return Promise.value(movies);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)