Skip to content

Commit 972c2c8

Browse files
committed
Merge branch 'BillyYccc-rxified-service-proxy'
2 parents 174c8ba + 1c1c91e commit 972c2c8

File tree

15 files changed

+758
-1
lines changed

15 files changed

+758
-1
lines changed

rxjava-2-examples/README.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,15 @@ Names and greetings are line-separated.
181181

182182
link:src/main/java/io/vertx/example/reactivex/net/greeter/Client.java[Greeting client]
183183
link:src/main/java/io/vertx/example/reactivex/net/greeter/Server.java[Greeting Server]
184+
185+
== Services examples
186+
187+
Rxified Vert.x Services examples
188+
189+
=== Service Proxy example
190+
191+
This example shows you how to make your service proxy Rxified with RxJava2.
192+
193+
link:src/main/java/io/vertx/example/reactivex/services/serviceproxy/SomeDatabaseService.java[Service Proxy interface]
194+
link:src/main/java/io/vertx/example/reactivex/services/serviceproxy/SomeDatabaseServiceVerticle.java[Service Provider Verticle]
195+
link:src/main/java/io/vertx/example/reactivex/services/serviceproxy/ServiceConsumerVerticle.java[Service Consumer Verticle]

rxjava-2-examples/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@
7171
<version>${project.version}</version>
7272
</dependency>
7373

74+
<dependency>
75+
<groupId>io.vertx</groupId>
76+
<artifactId>vertx-service-proxy</artifactId>
77+
<version>${project.version}</version>
78+
</dependency>
79+
80+
<dependency>
81+
<groupId>io.vertx</groupId>
82+
<artifactId>vertx-codegen</artifactId>
83+
<version>${project.version}</version>
84+
</dependency>
85+
7486
<dependency>
7587
<groupId>io.vertx</groupId>
7688
<artifactId>examples-utils</artifactId>
@@ -90,6 +102,15 @@
90102
<configuration>
91103
<source>1.8</source>
92104
<target>1.8</target>
105+
<annotationProcessors>
106+
<annotationProcessor>io.vertx.codegen.CodeGenProcessor</annotationProcessor>
107+
</annotationProcessors>
108+
<generatedSourcesDirectory>
109+
${project.basedir}/src/main/generated
110+
</generatedSourcesDirectory>
111+
<compilerArgs>
112+
<arg>-AoutputDirectory=${project.basedir}/src/main</arg>
113+
</compilerArgs>
93114
</configuration>
94115
</plugin>
95116
</plugins>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 2014 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.example.reactivex.services.serviceproxy;
18+
19+
import io.vertx.example.reactivex.services.serviceproxy.SomeDatabaseService;
20+
import io.vertx.core.eventbus.DeliveryOptions;
21+
import io.vertx.core.Vertx;
22+
import io.vertx.core.Future;
23+
import io.vertx.core.json.JsonObject;
24+
import io.vertx.core.json.JsonArray;
25+
import java.util.ArrayList;
26+
import java.util.HashSet;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Set;
30+
import java.util.stream.Collectors;
31+
import java.util.function.Function;
32+
import io.vertx.serviceproxy.ProxyHelper;
33+
import io.vertx.serviceproxy.ServiceException;
34+
import io.vertx.serviceproxy.ServiceExceptionMessageCodec;
35+
import io.vertx.example.reactivex.services.serviceproxy.SomeDatabaseService;
36+
import io.vertx.core.json.JsonObject;
37+
import io.vertx.core.AsyncResult;
38+
import io.vertx.core.Handler;
39+
40+
/*
41+
Generated Proxy code - DO NOT EDIT
42+
@author Roger the Robot
43+
*/
44+
@SuppressWarnings({"unchecked", "rawtypes"})
45+
public class SomeDatabaseServiceVertxEBProxy implements SomeDatabaseService {
46+
47+
private Vertx _vertx;
48+
private String _address;
49+
private DeliveryOptions _options;
50+
private boolean closed;
51+
52+
public SomeDatabaseServiceVertxEBProxy(Vertx vertx, String address) {
53+
this(vertx, address, null);
54+
}
55+
56+
public SomeDatabaseServiceVertxEBProxy(Vertx vertx, String address, DeliveryOptions options) {
57+
this._vertx = vertx;
58+
this._address = address;
59+
this._options = options;
60+
try {
61+
this._vertx.eventBus().registerDefaultCodec(ServiceException.class,
62+
new ServiceExceptionMessageCodec());
63+
} catch (IllegalStateException ex) {}
64+
}
65+
66+
public SomeDatabaseService getDataById(int id, Handler<AsyncResult<JsonObject>> resultHandler) {
67+
if (closed) {
68+
resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
69+
return this;
70+
}
71+
JsonObject _json = new JsonObject();
72+
_json.put("id", id);
73+
DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions();
74+
_deliveryOptions.addHeader("action", "getDataById");
75+
_vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
76+
if (res.failed()) {
77+
resultHandler.handle(Future.failedFuture(res.cause()));
78+
} else {
79+
resultHandler.handle(Future.succeededFuture(res.result().body()));
80+
}
81+
});
82+
return this;
83+
}
84+
85+
86+
private List<Character> convertToListChar(JsonArray arr) {
87+
List<Character> list = new ArrayList<>();
88+
for (Object obj: arr) {
89+
Integer jobj = (Integer)obj;
90+
list.add((char)(int)jobj);
91+
}
92+
return list;
93+
}
94+
95+
private Set<Character> convertToSetChar(JsonArray arr) {
96+
Set<Character> set = new HashSet<>();
97+
for (Object obj: arr) {
98+
Integer jobj = (Integer)obj;
99+
set.add((char)(int)jobj);
100+
}
101+
return set;
102+
}
103+
104+
private <T> Map<String, T> convertMap(Map map) {
105+
if (map.isEmpty()) {
106+
return (Map<String, T>) map;
107+
}
108+
109+
Object elem = map.values().stream().findFirst().get();
110+
if (!(elem instanceof Map) && !(elem instanceof List)) {
111+
return (Map<String, T>) map;
112+
} else {
113+
Function<Object, T> converter;
114+
if (elem instanceof List) {
115+
converter = object -> (T) new JsonArray((List) object);
116+
} else {
117+
converter = object -> (T) new JsonObject((Map) object);
118+
}
119+
return ((Map<String, T>) map).entrySet()
120+
.stream()
121+
.collect(Collectors.toMap(Map.Entry::getKey, converter::apply));
122+
}
123+
}
124+
private <T> List<T> convertList(List list) {
125+
if (list.isEmpty()) {
126+
return (List<T>) list;
127+
}
128+
129+
Object elem = list.get(0);
130+
if (!(elem instanceof Map) && !(elem instanceof List)) {
131+
return (List<T>) list;
132+
} else {
133+
Function<Object, T> converter;
134+
if (elem instanceof List) {
135+
converter = object -> (T) new JsonArray((List) object);
136+
} else {
137+
converter = object -> (T) new JsonObject((Map) object);
138+
}
139+
return (List<T>) list.stream().map(converter).collect(Collectors.toList());
140+
}
141+
}
142+
private <T> Set<T> convertSet(List list) {
143+
return new HashSet<T>(convertList(list));
144+
}
145+
}

0 commit comments

Comments
 (0)