Skip to content

Instantly share code, notes, and snippets.

@sleberknight
Last active October 17, 2024 15:30
Show Gist options
  • Save sleberknight/87cbeb0bc699713f80cbc645e0e91154 to your computer and use it in GitHub Desktop.
Save sleberknight/87cbeb0bc699713f80cbc645e0e91154 to your computer and use it in GitHub Desktop.
Changes to dropwizard-service-utils to make it a Dropwizard App for testing FreePortFinder implementations
package org.kiwiproject.dropwizard.util;
import lombok.extern.slf4j.Slf4j;
import org.kiwiproject.dropwizard.util.bundle.DynamicPortsBundle;
import org.kiwiproject.dropwizard.util.bundle.DynamicPortsConfiguration;
import org.kiwiproject.dropwizard.util.server.DropwizardConnectors;
import java.util.Map;
import java.time.Instant;
import io.dropwizard.core.Application;
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
@Slf4j
public class App extends Application<AppConfig> {
public static void main(String[] args) throws Exception {
new App().run(args);
}
@Override
public void initialize(Bootstrap<AppConfig> bootstrap) {
var portsBundle = new DynamicPortsBundle<AppConfig>() {
@Override
public DynamicPortsConfiguration getDynamicPortsConfiguration(AppConfig configuration) {
return configuration.getDynamicPorts();
}
};
bootstrap.addBundle(portsBundle);
}
@Override
public void run(AppConfig configuration, Environment environment) throws Exception {
var applicationPorts = DropwizardConnectors.getApplicationPorts(configuration);
var adminPorts = DropwizardConnectors.getAdminPorts(configuration);
LOG.info("Application ports:");
applicationPorts.forEach(port -> LOG.info("\t{}", port));
LOG.info("Admin ports:");
adminPorts.forEach(port -> LOG.info("\t{}", port));
environment.jersey().register(new HelloResource());
}
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public static class HelloResource {
@GET
public Response sayHelloAndTime() {
var entity = Map.of(
"greeting", "Good morning",
"time", Instant.now().toString()
);
return Response.ok(entity).build();
}
}
}
package org.kiwiproject.dropwizard.util;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import org.kiwiproject.dropwizard.util.bundle.DynamicPortsConfiguration;
import io.dropwizard.core.Configuration;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
@Getter
@Setter
public class AppConfig extends Configuration {
@NotNull
@Valid
@JsonProperty("dynamicPorts")
private DynamicPortsConfiguration dynamicPorts;
}
---
dynamicPorts:
minDynamicPort: 15000
maxDynamicPort: 15100
freePortFinder:
type: adjacent
useSecureDynamicPorts: false
logging:
level: INFO
loggers:
org.kiwiproject.dropwizard.util.startup: TRACE
appenders:
- type: console
---
dynamicPorts:
minDynamicPort: 15000
maxDynamicPort: 15100
freePortFinder:
type: incrementing
useSecureDynamicPorts: false
---
dynamicPorts:
minDynamicPort: 15000
maxDynamicPort: 15100
freePortFinder:
type: random
useSecureDynamicPorts: false
<!-- These are the CHANGES to make -->
<!-- 1. Change logback-classic to a required dependency by commenting out the test scope -->
<!-- 2. Add Maven Shade plugin -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.kiwiproject.dropwizard.util.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
@sleberknight
Copy link
Author

sleberknight commented Oct 17, 2024

Steps to run the apps:

  1. Build: mvn package -DskipTests
  2. Make sure netcat (nc) is installed
  3. In a terminal (tab), run: nc -l -p 15001
  4. In a terminal (tab), run: nc -l -p 15003
  5. In a terminal (tab), run: java -jar target/dropwizard-service-utilities-x.y.z-SNAPSHOT.jar server config-adjacent.yml (adjust x.y.z as necessary)
  6. In a terminal (tab), run: java -jar target/dropwizard-service-utilities-x.y.z-SNAPSHOT.jar server config-incrementing.yml (adjust x.y.z as necessary)
  7. In a terminal (tab), run: java -jar target/dropwizard-service-utilities-x.y.z-SNAPSHOT.jar server config-random.yml (adjust x.y.z as necessary)

The nc commands open a "server" listening on ports 15001 and 15003, which will not allow the adjacent port finder to use ports 15000 through 15003. So, the first (adjacent) app will take ports 15004 and 15005. The second (incrementing) app will use ports 15000 and 15002, since it is not limited to adjacent ports. And the third (random) app will search for two open ports at random between 15000 and 15100.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment