@Inject
private SomeManager someManager ;
| @Inject | |
| public MainVerticle(SomeManager someManager ) { | |
| this.someManager = SomeManager; | |
| } |
import javax.inject.Inject;
import com.google.inject.Inject;
<dependency>
<groupId>com.englishtown.vertx</groupId>
<artifactId>vertx-guice</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
</dependency>
package io.vos.vertx;
import io.vos.service.blob.BlobModule;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.google.inject.Guice;
import com.google.inject.Injector;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Verticle;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.inject.Inject;
/**
* For Vertx Docs:
* @see http://vertx.io/docs/vertx-core/java
* @see http://vertx.io/docs/apidocs/io/vertx/core/package-summary.html
* @see http://vertx.io/docs/apidocs/io/vertx/core/Vertx.html
*
* For Vertx Examples:
* @see https://github.com/vert-x3/vertx-examples/tree/master/core-examples
*/
public class HelloWorld {
// Using the JCommander command line parameter parser
// see http://jcommander.org/#_overview
@Parameter(names = { "--some-int" }, description = "Some random int")
private Integer someInt = 0;
@Parameter(names = { "--some-string", "-S"}, description = "Some string")
private String someString = "default";
private static final Map<String, Object> VRTCL_CONFIG =
Stream.of(
new SimpleEntry<>("storage.cassandra.host", "localhost"),
new SimpleEntry<>("storage.cassandra.port", 9042),
new SimpleEntry<>("grpc.port", 50051))
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
private final Set<Verticle> ijVerticles;
private final Vertx ijVertx;
@Inject
HelloWorld(
Vertx vertx,
Set<Verticle> verticles) {
ijVertx = vertx;
ijVerticles = verticles;
}
private void deployVerticles() {
DeploymentOptions options =
new DeploymentOptions()
.setConfig(new JsonObject(VRTCL_CONFIG))
.setWorker(true);
for (Verticle v : ijVerticles) {
LOG.info("deploying --> " + v.getClass().getName());
ijVertx.deployVerticle(v, options);
}
}
// Use runtime Java system properties to select a LogDelegateFactory
// @see http://vertx.io/docs/vertx-core/java/#_using_another_logging_framework
// @see http://vertx.io/docs/apidocs/io/vertx/core/spi/logging/LogDelegateFactory.html
//
// For slf4j, make sure to include //external:slf4fj and run with:
// $ bazel run \
// --jvmopt='-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory' \
// //java/io/vos/vertx:HelloWorld
private static final Logger LOG = LoggerFactory.getLogger(HelloWorld.class);
public static void main(String[] args) {
final Injector i = Guice.createInjector(
new VertxModule(),
// For more examples see:
// https://github.com/vert-x3/vertx-examples/tree/master/core-examples
new BlobModule());
HelloWorld hwServer = i.getInstance(HelloWorld.class);
JCommander.newBuilder()
.addObject(hwServer)
.build()
.parse(args);
LOG.info(String.format("HelloWorld: parameter --some-int: %d", hwServer.someInt));
LOG.info(String.format("HelloWorld: parameter --some-string: %s", hwServer.someString));
hwServer.deployVerticles();
final Vertx v = hwServer.ijVertx;
// Create an HTTP server which simply returns "Hello World!" to each request.
LOG.info("creating webserver");
v.createHttpServer().requestHandler(req -> req.response().end("Hello World!")).listen(8080);
// http://vertx.io/docs/vertx-core/java/#_writing_verticles
// http://vertx.io/docs/apidocs/io/vertx/core/Vertx.html#deployVerticle-io.vertx.core.Verticle-
// See deploy examples:
// https://github.com/vert-x3/vertx-examples/blob/master/core-examples/src/main/java/io/vertx/example/core/verticle/deploy/DeployExample.java
LOG.info("deploying HttpClientVerticle");
v.deployVerticle(new HttpClientVerticle());
LOG.info("deploying hazelcast instance member");
v.deployVerticle(new HazelcastVerticle());
LOG.info("deploying ruby verticle");
v.deployVerticle("ruby/io/vos/vertx/hello_verticle.rb");
}
}
package io.vos.service.blob;
import com.google.inject.AbstractModule;
import com.google.inject.multibindings.Multibinder;
import io.grpc.BindableService;
import io.vertx.core.Verticle;
public final class BlobModule extends AbstractModule {
@Override
protected void configure() {
Multibinder<Verticle> verticleBinder = Multibinder.newSetBinder(binder(), Verticle.class);
verticleBinder.addBinding().to(BlobVerticle.class);
Multibinder<BindableService> serviceBinder =
Multibinder.newSetBinder(binder(), BindableService.class);
serviceBinder.addBinding().to(BlobService.class);
}
}
package io.vos.service.blob;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import javax.inject.Inject;
public class BlobVerticle extends AbstractVerticle {
private static final Logger LOG = LoggerFactory.getLogger(BlobVerticle.class);
private final BlobServer ijServer;
@Inject
BlobVerticle(BlobServer server) {
ijServer = server;
}
@Override
public void start()
throws Exception {
LOG.info("starting blob RPC service");
int port = config().getInteger("grpc.port");
ijServer.start(port);
}
}
| @Override | |
| public void setUp() throws Exception { | |
| super.setUp(); | |
| CompletableFuture<Void> future = new CompletableFuture<>(); | |
| JsonObject config = new JsonObject().put("guice_binder", Binder.class.getName()); | |
| vertx.deployVerticle("java-guice:" + SimpleVerticle.class.getName(), new DeploymentOptions().setConfig(config), result -> { | |
| if (result.succeeded()) { | |
| future.complete(null); | |
| } else { | |
| future.completeExceptionally(result.cause()); | |
| } | |
| }); | |
| future.get(2, TimeUnit.SECONDS); | |
}
-------
one would assume that "setup" is done in the MainVerticle, but apparently thats not the case
all i want is for the MainVerticle (or whatever the main bootstrap class is) to have the start method call some other utility type classes to bootstrap the app w/
routes, a web server and other environment setup stuff.
Most examples have all this dome in the MainVerticle, but i think thats best organized in diff utility classes
-----------
public class MainVerticle extends AbstractVerticle { |
re: "Your also using guice Injector, but none of the github examples i've seen require that class for guice DI"
I'd very much encourage you to take a look at using Guice without any additional abstraction around it. I promise that any examples you've seen using guice are using an injector at some point to build the object graph.
Try checking out the Guice getting started guide here:
https://github.com/google/guice/wiki/GettingStarted
Re: "Confusingly, some DI vertx github examples use guice direct and some use englishtown.
I have'nt been able to find info on which is best/newest or what the diff are between them."
When I first started looking at Guice+Vertx I also came across that project. IIRC though, I abandoned that route because I had no interest in deploying verticles on the command line, and dropping the extra dependency was just simpler than managing the Guice dependency transitively. Without the command line deployment requirement, it seemed like there was no benefit to using it.
Someone please correct me if I'm wrong
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/77d3955c-f951-4360-9c16-22f23a164a07%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
i went over the "getting started w/ guice" guide as per your instructions:
Injector injector = Guice.createInjector();
Roof roof= injector.getInstance(Roof.class);
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/300529ee-8873-4a68-9250-750f7ea77a0c%40googlegroups.com.