Skip to content

Instantly share code, notes, and snippets.

@AgileSpirit
Last active December 14, 2015 16:48
Show Gist options
  • Save AgileSpirit/5117407 to your computer and use it in GitHub Desktop.
Save AgileSpirit/5117407 to your computer and use it in GitHub Desktop.
Launch an embedded Grizzly Server for Heroku cloud provider AND local environment
package com.agile.spirit.openapi;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.grizzly.http.server.HttpServer;
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
public class Main {
private static URI getBaseURI(String hostname, int port) {
return UriBuilder.fromUri("http://0.0.0.0/").port(port).build();
}
protected static HttpServer startServer(URI uri) throws IOException {
System.out.println("Starting grizzly...");
ResourceConfig rc = new PackagesResourceConfig("com.agile.spirit.openapi");
return GrizzlyServerFactory.createHttpServer(uri, rc);
}
public static void main(String[] args) throws IOException {
String hostname = System.getenv("HOSTNAME");
if (hostname == null) {
hostname = "localhost";
}
boolean isOnLocal = false;
String port = System.getenv("PORT");
if (port == null) {
isOnLocal = true;
port = "9998";
}
URI uri = getBaseURI(hostname, Integer.valueOf(port));
HttpServer httpServer = startServer(uri);
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", uri, uri));
if (isOnLocal) {
System.in.read();
httpServer.stop();
} else {
while (true) {
System.in.read();
}
}
}
}
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-framework</artifactId>
<version>2.2.19</version>
</dependency>
<repository>
<id>glassfish-maven2-repository.dev.java.net</id>
<name>Java.net Maven 2 Repository for GlassFish</name>
<url>https://maven.java.net/content/repositories/releases/</url>
</repository>
web: java -cp target/classes:target/dependency/* com.agile.spirit.openapi.Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment