Skip to content

Commit

Permalink
Replace generated classes usages by the ProxyHelper.
Browse files Browse the repository at this point in the history
Update the documentation to mention the two ways,
  • Loading branch information
cescoffier committed Jun 20, 2015
1 parent daf7196 commit f41e5e6
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 4 deletions.
12 changes: 10 additions & 2 deletions service-proxy-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ In the verticle, the handler registration is made using:

[source, java]
----
new ProcessorServiceVertxProxyHandler(vertx, service, "vertx.processor").registerHandler();
ProxyHelper.registerService(ProcessorService.class, vertx, service, "vertx.processor");
----

`service` is the service object we register (so an instance of the `ProcessorServiceImpl`. The last parameter is the address on which the service can be invoked.

Alternatively, you can instantiate the generated handler:
[source, java]
----
new ProcessorServiceVertxProxyHandler(vertx, service).registerHandler("vertx.processor");
----

In this case, classes need to be generated beforehand, and so, requires a manual `mvn compile`.


=== Running the service provider

To run the service provider, you first need to package it with:
Expand Down
18 changes: 18 additions & 0 deletions service-proxy-examples/service-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-groovy</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-js</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-ruby</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-codegen</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.groovy.examples.service;
import groovy.transform.CompileStatic
import io.vertx.lang.groovy.InternalHelper
import io.vertx.groovy.core.Vertx
import io.vertx.core.json.JsonObject
import io.vertx.core.AsyncResult
import io.vertx.core.Handler
/**
* The service interface.
*/
@CompileStatic
public class ProcessorService {
final def io.vertx.examples.service.ProcessorService delegate;
public ProcessorService(io.vertx.examples.service.ProcessorService delegate) {
this.delegate = delegate;
}
public Object getDelegate() {
return delegate;
}
public static ProcessorService create(Vertx vertx) {
def ret= InternalHelper.safeCreate(io.vertx.examples.service.ProcessorService.create((io.vertx.core.Vertx)vertx.getDelegate()), io.vertx.examples.service.ProcessorService.class, io.vertx.groovy.examples.service.ProcessorService.class);
return ret;
}
public static ProcessorService createProxy(Vertx vertx, String address) {
def ret= InternalHelper.safeCreate(io.vertx.examples.service.ProcessorService.createProxy((io.vertx.core.Vertx)vertx.getDelegate(), address), io.vertx.examples.service.ProcessorService.class, io.vertx.groovy.examples.service.ProcessorService.class);
return ret;
}
public void process(Map<String, Object> document, Handler<AsyncResult<Map<String, Object>>> resultHandler) {
this.delegate.process(document != null ? new io.vertx.core.json.JsonObject(document) : null, new Handler<AsyncResult<io.vertx.core.json.JsonObject>>() {
public void handle(AsyncResult<io.vertx.core.json.JsonObject> event) {
AsyncResult<Map<String, Object>> f
if (event.succeeded()) {
f = InternalHelper.<Map<String, Object>>result((Map<String, Object>)InternalHelper.wrapObject(event.result()))
} else {
f = InternalHelper.<Map<String, Object>>failure(event.cause())
}
resultHandler.handle(f)
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.vertx.core.AbstractVerticle;
import io.vertx.examples.service.impl.ProcessorServiceImpl;
import io.vertx.serviceproxy.ProxyHelper;

/**
* The verticle publishing the service.
Expand All @@ -15,8 +16,7 @@ public void start() throws Exception {
// Create the client object
service = new ProcessorServiceImpl();
// Register the handler
new ProcessorServiceVertxProxyHandler(vertx, service)
.registerHandler("vertx.processor");
ProxyHelper.registerService(ProcessorService.class, vertx, service, "vertx.processor");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.
*/

/** @module vertx-processor-sample-js/processor_service */
var utils = require('vertx-js/util/utils');

var io = Packages.io;
var JsonObject = io.vertx.core.json.JsonObject;
var JProcessorService = io.vertx.examples.service.ProcessorService;

/**
The service interface.
@class
*/
var ProcessorService = function(j_val) {

var j_processorService = j_val;
var that = this;

/**
@public
@param document {Object}
@param resultHandler {function}
*/
this.process = function(document, resultHandler) {
var __args = arguments;
if (__args.length === 2 && typeof __args[0] === 'object' && typeof __args[1] === 'function') {
j_processorService["process(io.vertx.core.json.JsonObject,io.vertx.core.Handler)"](utils.convParamJsonObject(document), function(ar) {
if (ar.succeeded()) {
resultHandler(utils.convReturnJson(ar.result()), null);
} else {
resultHandler(null, ar.cause());
}
});
} else utils.invalidArgs();
};

// A reference to the underlying Java delegate
// NOTE! This is an internal API and must not be used in user code.
// If you rely on this property your code is likely to break if we change it / remove it without warning.
this._jdel = j_processorService;
};

/**
@memberof module:vertx-processor-sample-js/processor_service
@param vertx {Vertx}
@return {ProcessorService}
*/
ProcessorService.create = function(vertx) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'object' && __args[0]._jdel) {
return utils.convReturnVertxGen(JProcessorService["create(io.vertx.core.Vertx)"](vertx._jdel), ProcessorService);
} else utils.invalidArgs();
};

/**
@memberof module:vertx-processor-sample-js/processor_service
@param vertx {Vertx}
@param address {string}
@return {ProcessorService}
*/
ProcessorService.createProxy = function(vertx, address) {
var __args = arguments;
if (__args.length === 2 && typeof __args[0] === 'object' && __args[0]._jdel && typeof __args[1] === 'string') {
return utils.convReturnVertxGen(JProcessorService["createProxy(io.vertx.core.Vertx,java.lang.String)"](vertx._jdel, address), ProcessorService);
} else utils.invalidArgs();
};

// We export the Constructor function
module.exports = ProcessorService;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'vertx/util/utils.rb'
# Generated from io.vertx.examples.service.ProcessorService
module VertxProcessorSample
# The service interface.
class ProcessorService
# @private
# @param j_del [::VertxProcessorSample::ProcessorService] the java delegate
def initialize(j_del)
@j_del = j_del
end
# @private
# @return [::VertxProcessorSample::ProcessorService] the underlying java delegate
def j_del
@j_del
end
# @param [::Vertx::Vertx] vertx
# @return [::VertxProcessorSample::ProcessorService]
def self.create(vertx=nil)
if vertx.class.method_defined?(:j_del) && !block_given?
return ::Vertx::Util::Utils.safe_create(Java::IoVertxExamplesService::ProcessorService.java_method(:create, [Java::IoVertxCore::Vertx.java_class]).call(vertx.j_del),::VertxProcessorSample::ProcessorService)
end
raise ArgumentError, "Invalid arguments when calling create(vertx)"
end
# @param [::Vertx::Vertx] vertx
# @param [String] address
# @return [::VertxProcessorSample::ProcessorService]
def self.create_proxy(vertx=nil,address=nil)
if vertx.class.method_defined?(:j_del) && address.class == String && !block_given?
return ::Vertx::Util::Utils.safe_create(Java::IoVertxExamplesService::ProcessorService.java_method(:createProxy, [Java::IoVertxCore::Vertx.java_class,Java::java.lang.String.java_class]).call(vertx.j_del,address),::VertxProcessorSample::ProcessorService)
end
raise ArgumentError, "Invalid arguments when calling create_proxy(vertx,address)"
end
# @param [Hash{String => Object}] document
# @yield
# @return [void]
def process(document=nil)
if document.class == Hash && block_given?
return @j_del.java_method(:process, [Java::IoVertxCoreJson::JsonObject.java_class,Java::IoVertxCore::Handler.java_class]).call(::Vertx::Util::Utils.to_json_object(document),(Proc.new { |ar| yield(ar.failed ? ar.cause : nil, ar.succeeded ? ar.result != nil ? JSON.parse(ar.result.encode) : nil : nil) }))
end
raise ArgumentError, "Invalid arguments when calling process(document)"
end
end
end

0 comments on commit f41e5e6

Please sign in to comment.