A lightweight replacement for vertx-service-proxy. This library highly optimizes for vertx java.
- Support generic
- Service methods return
Future
to avoid callback hell - Highly optimizes for java
- More supported data type
<dependency>
<groupId>com.github.longdt</groupId>
<artifactId>vertx-service4j</artifactId>
<version>1.1.0</version>
</dependency>
pojo classess need to implements Shareable
class to be used in Service
public class UserCreateRequest implements Shareable {
String username;
String password;
String email;
LocalDate dob;
UserCreateRequest() {
}
@Override
public UserCreateRequest copy() { //if this is immutable class, no need to override this method
var copied = new UserCreateRequest();
...
return copied;
}
...
setter/getter methods
...
}
public class UserUpdateRequest implements Shareable {
...
}
public class User implements Shareable {
...
}
@Service
public interface UserService {
static UserService create(UserRepository userRepository) {
return new UserServiceImpl(userRepository);
}
static UserService createProxy(Vertx vertx, String address) {
return new UserServiceProxy(vertx, address);
}
Future<Long> countUsers();
Future<List<Long>> getUserIds();
Future<List<Long>> aMethod(List<String> buffer);
Future<Map<Integer, User>> getUserMapping(List<Integer> ids);
Future<User> createUser(UserCreateRequest createRequest);
Future<User> updateUser(UserUpdateRequest updateRequest);
Future<User> getUser(long id);
Future<List<User>> getUserList();
Future<Page<User>> getUsers(JsonObject filter, int page, int size);
}
mvn clean compile
After compilation, vertx-service4j
creates UserServiceProxy
, UserServiceProxyHandler
.
var userService = new UserServiceImpl();
var proxyHandler = new UserServiceProxyHandler(vertx, userService, true);
vertx.eventBus().consumer(ADDRESS, proxyHandler);
this library uses kryo
to serialize objects to Buffer
when sending over network, so you can register pojo class to improve
de/serialization performance
Kryos.initialize(kryo -> {
kryo.setRegistrationRequired(false);
kryo.register(Page.class, new FieldSerializer<>(kryo, Page.class) {
@Override
protected Object create(Kryo kryo, Input input, Class type) {
return new Page<>(1, 1, 0, Collections.emptyList());
}
});
kryo.register(UserCreateRequest.class);
kryo.register(UserUpdateRequest.class);
kryo.register(User.class);
});