DeploymentUnitResource { @POST @Path("/unload") @Produces(MediaType.APPLICATION_JSON) public Response unloadDeploymentUnit(@QueryParam("name") String name, @QueryParam("version") Integer version) { if(name == null || name.length() == 0 || version == null || version < 0) { throw new WebApplicationException(buildResponse(Response.Status.BAD_REQUEST, "deploymentUnit name or version is invalid")); } DeploymentUnit unitToDelete = deploymentUnitManager.getAllDeploymentUnits().stream() .filter(d -> d.getName().equals(name) && d.getVersion() == version).findFirst().orElse(null); if(unitToDelete == null) { throw new WebApplicationException((buildResponse(Response.Status.NOT_FOUND, "deploymentUnit not found"))); } deploymentUnitManager.unload(name, version); Set<String> routersToDelete = taskRouterUtil.getRouterNames(unitToDelete); Set<String> routersInUse = deploymentUnitManager.getAllDeploymentUnits().stream() .filter(d -> d.getName().equals(name) && d.getVersion() != version) .map(d -> taskRouterUtil.getRouterNames(d)) .flatMap(Set::stream) .distinct() .collect(Collectors.toSet()); routersToDelete.removeAll(routersInUse); for(String routerName: routersToDelete) { routerRegistry.createOrResize(routerName, 0); } return buildResponse(Response.Status.OK, "successfully unloaded deploymentUnit: " + name + "/" + version); } @Inject  DeploymentUnitResource(DeploymentUnitsManager deploymentUnitsManager, RouterRegistry routerRegistry,
                                  TaskRouterUtil taskRouterUtil); @POST @Path("/load") @Produces(MediaType.APPLICATION_JSON) Response loadDeploymentUnit(@QueryParam("name") String name, @QueryParam("version") Integer version,
                                       @QueryParam("replace") @DefaultValue("false") Boolean replaceOld); @POST @Path("/unload") @Produces(MediaType.APPLICATION_JSON) Response unloadDeploymentUnit(@QueryParam("name") String name, @QueryParam("version") Integer version);  }
@Test public void testUnloadDeploymentUnit_shouldCallUnloadAndRemoveUnusedRouter() throws Exception { DeploymentUnit unit1 = getMockedDUnit("unit1", 1, "task1", getMethod1()); DeploymentUnit unit2 = getMockedDUnit("unit2", 1, "task2", getMethod2()); when(deploymentUnitsManager.getAllDeploymentUnits()).thenReturn(Arrays.asList(unit1, unit2)); Response response = deploymentUnitResource.unloadDeploymentUnit("unit1", 1); Assertions.assertThat(response.getStatus()).isEqualTo(200); verify(deploymentUnitsManager).unload("unit1", 1); verify(routerRegistry).createOrResize("com.flipkart.flux.resource.DeploymentUnitResourceTest_testMethod", 0); verifyNoMoreInteractions(routerRegistry); }