forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
606 additions
and
0 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
...ted/io/vertx/example/reactivex/services/serviceproxy/SomeDatabaseServiceVertxEBProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright 2014 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version 2.0 | ||
* (the "License"); you may not use this file except in compliance with the | ||
* License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.vertx.example.reactivex.services.serviceproxy; | ||
|
||
import io.vertx.example.reactivex.services.serviceproxy.SomeDatabaseService; | ||
import io.vertx.core.eventbus.DeliveryOptions; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.core.json.JsonArray; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.function.Function; | ||
import io.vertx.serviceproxy.ProxyHelper; | ||
import io.vertx.serviceproxy.ServiceException; | ||
import io.vertx.serviceproxy.ServiceExceptionMessageCodec; | ||
import io.vertx.example.reactivex.services.serviceproxy.SomeDatabaseService; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Handler; | ||
|
||
/* | ||
Generated Proxy code - DO NOT EDIT | ||
@author Roger the Robot | ||
*/ | ||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public class SomeDatabaseServiceVertxEBProxy implements SomeDatabaseService { | ||
|
||
private Vertx _vertx; | ||
private String _address; | ||
private DeliveryOptions _options; | ||
private boolean closed; | ||
|
||
public SomeDatabaseServiceVertxEBProxy(Vertx vertx, String address) { | ||
this(vertx, address, null); | ||
} | ||
|
||
public SomeDatabaseServiceVertxEBProxy(Vertx vertx, String address, DeliveryOptions options) { | ||
this._vertx = vertx; | ||
this._address = address; | ||
this._options = options; | ||
try { | ||
this._vertx.eventBus().registerDefaultCodec(ServiceException.class, | ||
new ServiceExceptionMessageCodec()); | ||
} catch (IllegalStateException ex) {} | ||
} | ||
|
||
public SomeDatabaseService getDataById(int id, Handler<AsyncResult<JsonObject>> resultHandler) { | ||
if (closed) { | ||
resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed"))); | ||
return this; | ||
} | ||
JsonObject _json = new JsonObject(); | ||
_json.put("id", id); | ||
DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions(); | ||
_deliveryOptions.addHeader("action", "getDataById"); | ||
_vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> { | ||
if (res.failed()) { | ||
resultHandler.handle(Future.failedFuture(res.cause())); | ||
} else { | ||
resultHandler.handle(Future.succeededFuture(res.result().body())); | ||
} | ||
}); | ||
return this; | ||
} | ||
|
||
|
||
private List<Character> convertToListChar(JsonArray arr) { | ||
List<Character> list = new ArrayList<>(); | ||
for (Object obj: arr) { | ||
Integer jobj = (Integer)obj; | ||
list.add((char)(int)jobj); | ||
} | ||
return list; | ||
} | ||
|
||
private Set<Character> convertToSetChar(JsonArray arr) { | ||
Set<Character> set = new HashSet<>(); | ||
for (Object obj: arr) { | ||
Integer jobj = (Integer)obj; | ||
set.add((char)(int)jobj); | ||
} | ||
return set; | ||
} | ||
|
||
private <T> Map<String, T> convertMap(Map map) { | ||
if (map.isEmpty()) { | ||
return (Map<String, T>) map; | ||
} | ||
|
||
Object elem = map.values().stream().findFirst().get(); | ||
if (!(elem instanceof Map) && !(elem instanceof List)) { | ||
return (Map<String, T>) map; | ||
} else { | ||
Function<Object, T> converter; | ||
if (elem instanceof List) { | ||
converter = object -> (T) new JsonArray((List) object); | ||
} else { | ||
converter = object -> (T) new JsonObject((Map) object); | ||
} | ||
return ((Map<String, T>) map).entrySet() | ||
.stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, converter::apply)); | ||
} | ||
} | ||
private <T> List<T> convertList(List list) { | ||
if (list.isEmpty()) { | ||
return (List<T>) list; | ||
} | ||
|
||
Object elem = list.get(0); | ||
if (!(elem instanceof Map) && !(elem instanceof List)) { | ||
return (List<T>) list; | ||
} else { | ||
Function<Object, T> converter; | ||
if (elem instanceof List) { | ||
converter = object -> (T) new JsonArray((List) object); | ||
} else { | ||
converter = object -> (T) new JsonObject((Map) object); | ||
} | ||
return (List<T>) list.stream().map(converter).collect(Collectors.toList()); | ||
} | ||
} | ||
private <T> Set<T> convertSet(List list) { | ||
return new HashSet<T>(convertList(list)); | ||
} | ||
} |
225 changes: 225 additions & 0 deletions
225
...o/vertx/example/reactivex/services/serviceproxy/SomeDatabaseServiceVertxProxyHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/* | ||
* Copyright 2014 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version 2.0 | ||
* (the "License"); you may not use this file except in compliance with the | ||
* License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.vertx.example.reactivex.services.serviceproxy; | ||
|
||
import io.vertx.example.reactivex.services.serviceproxy.SomeDatabaseService; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.eventbus.EventBus; | ||
import io.vertx.core.eventbus.Message; | ||
import io.vertx.core.eventbus.MessageConsumer; | ||
import io.vertx.core.eventbus.DeliveryOptions; | ||
import io.vertx.core.eventbus.ReplyException; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.core.json.JsonArray; | ||
import java.util.Collection; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import io.vertx.serviceproxy.ProxyHelper; | ||
import io.vertx.serviceproxy.ProxyHandler; | ||
import io.vertx.serviceproxy.ServiceException; | ||
import io.vertx.serviceproxy.ServiceExceptionMessageCodec; | ||
import io.vertx.example.reactivex.services.serviceproxy.SomeDatabaseService; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Handler; | ||
|
||
/* | ||
Generated Proxy code - DO NOT EDIT | ||
@author Roger the Robot | ||
*/ | ||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public class SomeDatabaseServiceVertxProxyHandler extends ProxyHandler { | ||
|
||
public static final long DEFAULT_CONNECTION_TIMEOUT = 5 * 60; // 5 minutes | ||
|
||
private final Vertx vertx; | ||
private final SomeDatabaseService service; | ||
private final long timerID; | ||
private long lastAccessed; | ||
private final long timeoutSeconds; | ||
|
||
public SomeDatabaseServiceVertxProxyHandler(Vertx vertx, SomeDatabaseService service) { | ||
this(vertx, service, DEFAULT_CONNECTION_TIMEOUT); | ||
} | ||
|
||
public SomeDatabaseServiceVertxProxyHandler(Vertx vertx, SomeDatabaseService service, long timeoutInSecond) { | ||
this(vertx, service, true, timeoutInSecond); | ||
} | ||
|
||
public SomeDatabaseServiceVertxProxyHandler(Vertx vertx, SomeDatabaseService service, boolean topLevel, long timeoutSeconds) { | ||
this.vertx = vertx; | ||
this.service = service; | ||
this.timeoutSeconds = timeoutSeconds; | ||
try { | ||
this.vertx.eventBus().registerDefaultCodec(ServiceException.class, | ||
new ServiceExceptionMessageCodec()); | ||
} catch (IllegalStateException ex) {} | ||
if (timeoutSeconds != -1 && !topLevel) { | ||
long period = timeoutSeconds * 1000 / 2; | ||
if (period > 10000) { | ||
period = 10000; | ||
} | ||
this.timerID = vertx.setPeriodic(period, this::checkTimedOut); | ||
} else { | ||
this.timerID = -1; | ||
} | ||
accessed(); | ||
} | ||
|
||
private void checkTimedOut(long id) { | ||
long now = System.nanoTime(); | ||
if (now - lastAccessed > timeoutSeconds * 1000000000) { | ||
close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (timerID != -1) { | ||
vertx.cancelTimer(timerID); | ||
} | ||
super.close(); | ||
} | ||
|
||
private void accessed() { | ||
this.lastAccessed = System.nanoTime(); | ||
} | ||
|
||
public void handle(Message<JsonObject> msg) { | ||
try { | ||
JsonObject json = msg.body(); | ||
String action = msg.headers().get("action"); | ||
if (action == null) { | ||
throw new IllegalStateException("action not specified"); | ||
} | ||
accessed(); | ||
switch (action) { | ||
case "getDataById": { | ||
service.getDataById(json.getValue("id") == null ? null : (json.getLong("id").intValue()), createHandler(msg)); | ||
break; | ||
} | ||
default: { | ||
throw new IllegalStateException("Invalid action: " + action); | ||
} | ||
} | ||
} catch (Throwable t) { | ||
msg.reply(new ServiceException(500, t.getMessage())); | ||
throw t; | ||
} | ||
} | ||
|
||
private <T> Handler<AsyncResult<T>> createHandler(Message msg) { | ||
return res -> { | ||
if (res.failed()) { | ||
if (res.cause() instanceof ServiceException) { | ||
msg.reply(res.cause()); | ||
} else { | ||
msg.reply(new ServiceException(-1, res.cause().getMessage())); | ||
} | ||
} else { | ||
if (res.result() != null && res.result().getClass().isEnum()) { | ||
msg.reply(((Enum) res.result()).name()); | ||
} else { | ||
msg.reply(res.result()); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) { | ||
return res -> { | ||
if (res.failed()) { | ||
if (res.cause() instanceof ServiceException) { | ||
msg.reply(res.cause()); | ||
} else { | ||
msg.reply(new ServiceException(-1, res.cause().getMessage())); | ||
} | ||
} else { | ||
msg.reply(new JsonArray(res.result())); | ||
} | ||
}; | ||
} | ||
|
||
private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) { | ||
return res -> { | ||
if (res.failed()) { | ||
if (res.cause() instanceof ServiceException) { | ||
msg.reply(res.cause()); | ||
} else { | ||
msg.reply(new ServiceException(-1, res.cause().getMessage())); | ||
} | ||
} else { | ||
msg.reply(new JsonArray(new ArrayList<>(res.result()))); | ||
} | ||
}; | ||
} | ||
|
||
private Handler<AsyncResult<List<Character>>> createListCharHandler(Message msg) { | ||
return res -> { | ||
if (res.failed()) { | ||
if (res.cause() instanceof ServiceException) { | ||
msg.reply(res.cause()); | ||
} else { | ||
msg.reply(new ServiceException(-1, res.cause().getMessage())); | ||
} | ||
} else { | ||
JsonArray arr = new JsonArray(); | ||
for (Character chr: res.result()) { | ||
arr.add((int) chr); | ||
} | ||
msg.reply(arr); | ||
} | ||
}; | ||
} | ||
|
||
private Handler<AsyncResult<Set<Character>>> createSetCharHandler(Message msg) { | ||
return res -> { | ||
if (res.failed()) { | ||
if (res.cause() instanceof ServiceException) { | ||
msg.reply(res.cause()); | ||
} else { | ||
msg.reply(new ServiceException(-1, res.cause().getMessage())); | ||
} | ||
} else { | ||
JsonArray arr = new JsonArray(); | ||
for (Character chr: res.result()) { | ||
arr.add((int) chr); | ||
} | ||
msg.reply(arr); | ||
} | ||
}; | ||
} | ||
|
||
private <T> Map<String, T> convertMap(Map map) { | ||
return (Map<String, T>)map; | ||
} | ||
|
||
private <T> List<T> convertList(List list) { | ||
return (List<T>)list; | ||
} | ||
|
||
private <T> Set<T> convertSet(List list) { | ||
return new HashSet<T>((List<T>)list); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...x/example/reactivex/services/serviceproxy/groovy/SomeDatabaseService_GroovyExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.vertx.example.reactivex.services.serviceproxy.groovy; | ||
public class SomeDatabaseService_GroovyExtension { | ||
public static io.vertx.example.reactivex.services.serviceproxy.SomeDatabaseService getDataById(io.vertx.example.reactivex.services.serviceproxy.SomeDatabaseService j_receiver, int id, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.Map<String, Object>>> resultHandler) { | ||
io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.getDataById(id, | ||
resultHandler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.core.json.JsonObject>>() { | ||
public void handle(io.vertx.core.AsyncResult<io.vertx.core.json.JsonObject> ar) { | ||
resultHandler.handle(ar.map(event -> io.vertx.core.impl.ConversionHelper.fromJsonObject(event))); | ||
} | ||
} : null)); | ||
return j_receiver; | ||
} | ||
} |
Oops, something went wrong.