Chapter 7. Changes in protocols This section explains the changes in networking protocols. 7.1. Changes in Eclipse Vert.x gRPC The following section describes the changes in Eclipse Vert.x gRPC. 7.1.1. New gRPC compiler plugin In Eclipse Vert.x 4, the module protoc-gen-grpc-java is no longer available. This module was a fork of the official gRPC compiler. In earlier releases of Eclipse Vert.x, you had to work with this fork. This fork is maintained by the Eclipse project. Working with the fork was complex. In releases, to work with gRPC, the following details were added to pom.xml file. <!-- Vert.x 3.x --> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <configuration> <protocArtifact>com.google.protobuf:protoc:3.2.0:exe:USD{os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <!-- NOTE: the gav coordinates point to the 3.x only compiler fork --> <pluginArtifact>io.vertx:protoc-gen-grpc-java:USD{vertx.grpc.version}:exe:USD{os.detected.classifier}</pluginArtifact> </configuration> ... </plugin> In Eclipse Vert.x 4, a new gRPC compiler plugin is available. This plugin uses the official gRPC compiler instead of the fork. To work with the new gRPC plugin, add the following details to pom.xml file. <!-- Vert.x 4.x --> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <configuration> <protocArtifact>com.google.protobuf:protoc:3.2.0:exe:USD{os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <!-- NOTE: the gav coordinates point to the official compiler --> <pluginArtifact>io.grpc:protoc-gen-grpc-java:USD{vertx.grpc.version}:exe:USD{os.detected.classifier}</pluginArtifact> <protocPlugins> <!-- NEW: a plugin is added to generate the Vert.x specific code --> <protocPlugin> <id>vertx-grpc-protoc-plugin</id> <groupId>io.vertx</groupId> <artifactId>vertx-grpc-protoc-plugin</artifactId> <version>USD{vertx.version}</version> <mainClass>io.vertx.grpc.protoc.plugin.VertxGrpcGenerator</mainClass> </protocPlugin> </protocPlugins> </configuration> ... </plugin> 7.1.2. Migrating the generated code In Eclipse Vert.x 4, the new compiler is used. When the new gRPC plugin is used, the generated code is not written in the same source file. This is because the compiler does not allow custom code generation on its base class. The plugins must generate a new class with a different name to save the code. In earlier releases of Eclipse Vert.x, the older gRPC plugin would write the generated code in the same source file. For example, if you have the following descriptor: service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } In Eclipse Vert.x 3.x, the code would be generated in the GreeterGrpc class. // 3.x GreeterGrpc.GreeterVertxImplBase service = new GreeterGrpc.GreeterVertxImplBase() { ... } In Eclipse Vert.x 4, the code is generated in the VertxGreeterGrpc class. // 4.x VertxGreeterGrpc.GreeterVertxImplBase service = new VertxGreeterGrpc.GreeterVertxImplBase() { ... } 7.1.3. gRPC APIs support futures In Eclipse Vert.x 4, the gRPC APIs support futures. The gRPC plugin generates promisified APIs. These APIs use the standard Eclipse Vert.x input and output arguments, which makes it easier to create standard Eclipse Vert.x applications. The following example shows the use of promise in Eclipse Vert.x 3.x. // 3.x GreeterGrpc.GreeterVertxImplBase service = new GreeterGrpc.GreeterVertxImplBase() { @Override public void sayHello(HelloRequest request, Promise<HelloReply> future) { future.complete( HelloReply.newBuilder().setMessage(request.getName()).build()); } } The following example shows the use of futures in Eclipse Vert.x 4. // 4.x VertxGreeterGrpc.GreeterVertxImplBase service = new VertxGreeterGrpc.GreeterVertxImplBase() { @Override public Future<HelloReply> sayHello(HelloRequest request) { return Future.succeededFuture( HelloReply.newBuilder() .setMessage(request.getName()) .build()); } } 7.2. Changes in Eclipse Vert.x MQTT The following section describes the changes in Eclipse Vert.x MQTT. 7.2.1. Some fluent methods in MQTT clients return future Some fluent methods in MqttClient class return Future instead of being fluent. For example, methods such as, MqttClient.connect() , MqttClient.disconnect() , MqttClient.publish() return future in Eclipse Vert.x 4. The following example shows the use of publish() method in Eclipse Vert.x 3.x releases. client .publish("hello", Buffer.buffer("hello"), MqttQoS.EXACTLY_ONCE, false, false) .publish("hello", Buffer.buffer("hello"), MqttQoS.AT_LEAST_ONCE, false, false); The following example shows the use of publish() method in Eclipse Vert.x 4 release. client.publish("hello", Buffer.buffer("hello"), MqttQoS.EXACTLY_ONCE, false, false); client.publish("hello", Buffer.buffer("hello"), MqttQoS.AT_LEAST_ONCE, false, false); 7.2.2. MqttWill messages return buffer The MqttWill data object wraps a string message as an Eclipse Vert.x buffer instead of a byte array. 7.2.3. Removed the deprecated MqttWill and authorization methods from MQTT The following MQTT methods have been removed: Removed methods Replacing methods MqttWill.willMessage() MqttWill.getWillMessage() MqttWill.willTopic() MqttWill.getWillTopic() MqttWill.willQos() MqttWill.getWillQos() MqttAuth.username() MqttAuth.getUsername() MqttAuth.password() MqttAuth.getPassword() MqttClientOptions.setKeepAliveTimeSeconds() MqttClientOptions.setKeepAliveInterval() 7.3. Changes in Eclipse Vert.x Service Proxy The following section describes the changes in service proxy. 7.3.1. Using service proxy code generator The ServiceProxyProcessor class has been removed. To use the service proxy code generator, you must import vertx-codegen with processor classifier in your classpath: <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-codegen</artifactId> <classifier>processor</classifier> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-service-proxy</artifactId> </dependency> </dependencies> Service proxy reuses io.vertx.codegen.CodeGenProcessor from vertx-codegen to start the code generation of service proxy and handler.

Commands:
<!-- Vert.x 3.x --> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <configuration> <protocArtifact>com.google.protobuf:protoc:3.2.0:exe:USD{os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <!-- NOTE: the gav coordinates point to the 3.x only compiler fork --> <pluginArtifact>io.vertx:protoc-gen-grpc-java:USD{vertx.grpc.version}:exe:USD{os.detected.classifier}</pluginArtifact> </configuration> </plugin>
<!-- Vert.x 4.x --> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <configuration> <protocArtifact>com.google.protobuf:protoc:3.2.0:exe:USD{os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <!-- NOTE: the gav coordinates point to the official compiler --> <pluginArtifact>io.grpc:protoc-gen-grpc-java:USD{vertx.grpc.version}:exe:USD{os.detected.classifier}</pluginArtifact> <protocPlugins> <!-- NEW: a plugin is added to generate the Vert.x specific code --> <protocPlugin> <id>vertx-grpc-protoc-plugin</id> <groupId>io.vertx</groupId> <artifactId>vertx-grpc-protoc-plugin</artifactId> <version>USD{vertx.version}</version> <mainClass>io.vertx.grpc.protoc.plugin.VertxGrpcGenerator</mainClass> </protocPlugin> </protocPlugins> </configuration> </plugin>
service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} }
// 3.x GreeterGrpc.GreeterVertxImplBase service = new GreeterGrpc.GreeterVertxImplBase() { }
// 4.x VertxGreeterGrpc.GreeterVertxImplBase service = new VertxGreeterGrpc.GreeterVertxImplBase() { }
// 3.x GreeterGrpc.GreeterVertxImplBase service = new GreeterGrpc.GreeterVertxImplBase() { @Override public void sayHello(HelloRequest request, Promise<HelloReply> future) { future.complete( HelloReply.newBuilder().setMessage(request.getName()).build()); } }
// 4.x VertxGreeterGrpc.GreeterVertxImplBase service = new VertxGreeterGrpc.GreeterVertxImplBase() { @Override public Future<HelloReply> sayHello(HelloRequest request) { return Future.succeededFuture( HelloReply.newBuilder() .setMessage(request.getName()) .build()); } }
client .publish("hello", Buffer.buffer("hello"), MqttQoS.EXACTLY_ONCE, false, false) .publish("hello", Buffer.buffer("hello"), MqttQoS.AT_LEAST_ONCE, false, false);
client.publish("hello", Buffer.buffer("hello"), MqttQoS.EXACTLY_ONCE, false, false); client.publish("hello", Buffer.buffer("hello"), MqttQoS.AT_LEAST_ONCE, false, false);
<dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-codegen</artifactId> <classifier>processor</classifier> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-service-proxy</artifactId> </dependency> </dependencies>