Skip to content

Commit

Permalink
Merge pull request #1587 from mkouba/vertx-web-2
Browse files Browse the repository at this point in the history
Vertx web - make hot replacement work
  • Loading branch information
cescoffier authored Mar 20, 2019
2 parents 9dbe2d1 + 918973d commit 7c26e51
Show file tree
Hide file tree
Showing 12 changed files with 572 additions and 494 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ public interface HotReplacementContext {

Throwable getDeploymentProblem();

void doScan() throws Exception;
/**
*
* @return {@code true} if a restart was performed, {@code false} otherwise
* @throws Exception
*/
boolean doScan() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ public Throwable getDeploymentProblem() {
return DevModeMain.deploymentProblem;
}

public void doScan() throws IOException {
public boolean doScan() throws IOException {
final long startNanoseconds = System.nanoTime();
final ConcurrentMap<String, byte[]> changedClasses = scanForChangedClasses();
if (changedClasses == null)
return;
return false;

DevModeMain.restartApp();
log.infof("Hot replace total time: %ss ", Timing.convertToBigDecimalSeconds(System.nanoTime() - startNanoseconds));
return true;
}

ConcurrentMap<String, byte[]> scanForChangedClasses() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.deployment.builditem.substrate.ReflectiveClassBuildItem;
import io.quarkus.deployment.util.HashUtil;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.gizmo.MethodCreator;
import io.quarkus.gizmo.MethodDescriptor;
import io.quarkus.gizmo.ResultHandle;
import io.quarkus.vertx.deployment.VertxBuildItem;
import io.quarkus.vertx.web.Route;
import io.quarkus.vertx.web.RoutingExchange;
import io.quarkus.vertx.web.runtime.HttpServerInitializer;
import io.quarkus.vertx.web.runtime.RouterProducer;
import io.quarkus.vertx.web.runtime.RoutingExchangeImpl;
import io.quarkus.vertx.web.runtime.VertxHttpConfiguration;
import io.quarkus.vertx.web.runtime.VertxWebTemplate;
Expand All @@ -73,7 +75,7 @@ BeanDeploymentValidatorBuildItem initialize(BuildProducer<FeatureBuildItem> feat
BuildProducer<RouteHandlerBuildItem> routeHandlerBusinessMethods,
BuildProducer<UnremovableBeanBuildItem> unremovableBeans) {

additionalBean.produce(new AdditionalBeanBuildItem(false, HttpServerInitializer.class));
additionalBean.produce(new AdditionalBeanBuildItem(false, RouterProducer.class));
feature.produce(new FeatureBuildItem(FeatureBuildItem.VERTX_WEB));
unremovableBeans.produce(new UnremovableBeanBuildItem(new BeanClassAnnotationExclusion(ROUTE)));
unremovableBeans.produce(new UnremovableBeanBuildItem(new BeanClassAnnotationExclusion(ROUTES)));
Expand Down Expand Up @@ -118,7 +120,9 @@ void build(VertxWebTemplate template, BeanContainerBuildItem beanContainer,
List<RouteHandlerBuildItem> routeHandlerBusinessMethods,
BuildProducer<GeneratedClassBuildItem> generatedClass, AnnotationProxyBuildItem annotationProxy,
LaunchModeBuildItem launchMode,
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses,
ShutdownContextBuildItem shutdown,
VertxBuildItem vertx) {

ClassOutput classOutput = new ClassOutput() {
@Override
Expand All @@ -135,7 +139,9 @@ public void write(String name, byte[] data) {
routeConfigs.put(handlerClass, routes);
reflectiveClasses.produce(new ReflectiveClassBuildItem(false, false, handlerClass));
}
template.configureRouter(beanContainer.getValue(), routeConfigs, vertxHttpConfiguration, launchMode.getLaunchMode());
template.configureRouter(vertx.getVertx(), beanContainer.getValue(), routeConfigs, vertxHttpConfiguration,
launchMode.getLaunchMode(),
shutdown);
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.quarkus.deployment.devmode.HotReplacementContext;
import io.quarkus.deployment.devmode.HotReplacementSetup;
import io.quarkus.deployment.devmode.ReplacementDebugPage;
import io.quarkus.vertx.web.runtime.HttpServerInitializer;
import io.quarkus.vertx.web.runtime.VertxWebTemplate;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;

Expand All @@ -17,7 +17,7 @@ public class VertxHotReplacementSetup implements HotReplacementSetup {
@Override
public void setupHotDeployment(HotReplacementContext context) {
this.hotReplacementContext = context;
HttpServerInitializer.setHotReplacement(this::handleHotReplacementRequest);
VertxWebTemplate.setHotReplacement(this::handleHotReplacementRequest);
}

void handleHotReplacementRequest(RoutingContext routingContext) {
Expand All @@ -30,10 +30,11 @@ void handleHotReplacementRequest(RoutingContext routingContext) {
routingContext.next();
return;
}
boolean restart = false;
synchronized (this) {
if (nextUpdate < System.currentTimeMillis()) {
try {
hotReplacementContext.doScan();
restart = hotReplacementContext.doScan();
} catch (Exception e) {
throw new IllegalStateException("Unable to perform hot replacement scanning", e);
}
Expand All @@ -44,7 +45,11 @@ void handleHotReplacementRequest(RoutingContext routingContext) {
handleDeploymentProblem(routingContext, hotReplacementContext.getDeploymentProblem());
return;
}
routingContext.next();
if (restart) {
routingContext.reroute(routingContext.request().path());
} else {
routingContext.next();
}
}

public static void handleDeploymentProblem(RoutingContext routingContext, final Throwable exception) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.vertx.web.runtime;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Singleton;

import io.vertx.ext.web.Router;

@Singleton
public class RouterProducer {

private volatile Router router;

void initialize(Router router) {
this.router = router;
}

// Note that we need a client proxy because if a bean also @Observes Router a null value would be injected
@ApplicationScoped
@Produces
Router produceRouter() {
return router;
}

}
Loading

0 comments on commit 7c26e51

Please sign in to comment.