Skip to content

Commit

Permalink
Option Name is optinal for Vertx instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaol committed Oct 14, 2024
1 parent 46f1376 commit 63c7d74
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
public class NamedVertxOptions {

public static final NamedVertxOptions DEFAULT = new NamedVertxOptions("", new VertxOptions());

/** The name of the configured VertxOptions **/
private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.jboss.msc.service.StopContext;
import org.wildfly.extension.vertx.logging.VertxLogger;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand All @@ -36,10 +35,14 @@ public class VertxProxyService implements Service, VertxConstants {
final Consumer<VertxProxy> vertxProxytConsumer;

static void installService(OperationContext context, String optionName) {
Objects.requireNonNull(optionName, "optionName cannot be null.");
CapabilityServiceBuilder<?> vertxServiceBuilder = context.getCapabilityServiceTarget().addService();
Consumer<VertxProxy> vertxProxytConsumer = vertxServiceBuilder.provides(VERTX_RUNTIME_CAPABILITY);
Supplier<NamedVertxOptions> optionsSupplier = vertxServiceBuilder.requiresCapability(VERTX_OPTIONS_CAPABILITY.getName(), NamedVertxOptions.class, optionName);
final Supplier<NamedVertxOptions> optionsSupplier;
if (optionName == null) {
optionsSupplier = () -> NamedVertxOptions.DEFAULT;
} else {
optionsSupplier = vertxServiceBuilder.requiresCapability(VERTX_OPTIONS_CAPABILITY.getName(), NamedVertxOptions.class, optionName);
}
VertxProxyService vertxProxyService = new VertxProxyService(optionName, optionsSupplier, vertxProxytConsumer);
vertxServiceBuilder.setInstance(vertxProxyService);
vertxServiceBuilder.setInitialMode(ServiceController.Mode.ACTIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ static class VertxResourceAdd extends AbstractAddStepHandler {

@Override
protected void performRuntime(OperationContext context, ModelNode operation, Resource resource) throws OperationFailedException {
String optionName = operation.hasDefined(ATTR_OPTION_NAME) ? VertxAttributes.OPTION_NAME.resolveModelAttribute(context, operation).asString() : null;
if (optionName != null) {
VertxProxyService.installService(context, optionName);
}
final String optionName = operation.hasDefined(ATTR_OPTION_NAME) ? VertxAttributes.OPTION_NAME.resolveModelAttribute(context, operation).asString() : null;
VertxProxyService.installService(context, optionName);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.extension.vertx.test.basic;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.api.ContainerResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.as.test.integration.common.HttpRequest;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.extension.vertx.test.shared.ejb.EchoService;
import org.wildfly.extension.vertx.test.shared.rest.RestApp;
import org.wildfly.extension.vertx.test.shared.rest.ServiceEndpoint;

import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
* Test injection in servlet, ejb, REST resource endpoints.
*
* @author <a href="mailto:[email protected]">Lin Gao</a>
*/
@RunWith(Arquillian.class)
@RunAsClient
@Ignore("io.netty.netty-transport needs to be added as dependency to module: io.smallrye.reactive.mutiny.vertx-core")
public class VertxInjectionTestCase {
@ArquillianResource
private URL url;

@ContainerResource
private ManagementClient managementClient;

@Deployment
public static WebArchive createDeployment() {
final WebArchive war = ShrinkWrap.create(WebArchive.class, "test-injection.war");
war.addClasses(EchoService.class, RestApp.class, ServiceEndpoint.class);
war.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return war;
}

@Test
public void testInjection() throws Exception {
String res = HttpRequest.get(url.toExternalForm() + "rest/echo/Hello", 4, TimeUnit.SECONDS);
Assert.assertEquals("Hello", res);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static org.wildfly.extension.vertx.test.shared.ManagementClientUtils.vertxOptionOperation;

/**
* Test vertx eventbus message in async ejb on basic set up.
* Test vertx subsystem management operations.
*
* @author <a href="mailto:[email protected]">Lin Gao</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

import io.smallrye.common.annotation.Identifier;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.MessageConsumer;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.ejb.Asynchronous;
import jakarta.ejb.Stateless;
import jakarta.enterprise.inject.Any;
import jakarta.inject.Inject;

import static org.wildfly.extension.vertx.VertxConstants.CDI_QUALIFIER;

@Stateless
public class EchoService {

@Any
@Identifier(CDI_QUALIFIER)
@Inject
private Vertx vertx;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@

import java.io.IOException;

import io.smallrye.common.annotation.Identifier;
import io.vertx.core.Vertx;
import jakarta.enterprise.inject.Any;
import jakarta.inject.Inject;
import jakarta.servlet.AsyncContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import static org.wildfly.extension.vertx.VertxConstants.CDI_QUALIFIER;

/**
* An abstract servlet class which communicates with a Vert.x eventbus address.
*
* @author <a href="mailto:[email protected]">Lin Gao</a>
*/
public abstract class AbstractVertxServlet<S, R> extends HttpServlet {

@Any
@Identifier(CDI_QUALIFIER)
@Inject
private Vertx vertx;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
*/
package org.wildfly.extension.vertx.test.shared.servlet;

import io.smallrye.common.annotation.Identifier;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.MessageConsumer;

import jakarta.enterprise.inject.Any;
import jakarta.inject.Inject;
import jakarta.servlet.AsyncContext;
import jakarta.servlet.ServletException;
Expand All @@ -17,6 +19,8 @@
import java.io.IOException;
import java.io.PrintWriter;

import static org.wildfly.extension.vertx.VertxConstants.CDI_QUALIFIER;

/**
* AsyncServlet which requests a response from `echo` Vert.x EventBus address.
*
Expand All @@ -25,6 +29,8 @@
@WebServlet(value = "/async", asyncSupported = true)
public class AsyncServlet extends HttpServlet {

@Any
@Identifier(CDI_QUALIFIER)
@Inject
private Vertx vertx;

Expand Down

0 comments on commit 63c7d74

Please sign in to comment.