Skip to content

Instantly share code, notes, and snippets.

@AppLoidx
Last active August 10, 2019 23:58
Show Gist options
  • Save AppLoidx/cf6e5628fd085e7e5befbb12efdde1ce to your computer and use it in GitHub Desktop.
Save AppLoidx/cf6e5628fd085e7e5befbb12efdde1ce to your computer and use it in GitHub Desktop.
Run Grizzly Http Server on Heroku via Maven

GitHub Repository: github.com/AppLoidx/helios-rest-api

package com.apploidxxx;
import org.glassfish.grizzly.http.server.CLStaticHttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Main class.
*
*/
public class Main {
// Base URI the Grizzly HTTP server for tests will listen on
public static final String BASE_URI = "http://localhost:3000/";
// Getting port from JAVA_OPTS
private static String port = System.getenv("PORT");
// HEROKU URI Setting up
private static final String BASE_URI_HEROKU = "http://0.0.0.0:" + port + "/";
/**
* Starts local Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer() {
setUpLogger();
// create a resource config that scans for JAX-RS resources and providers
// in com.apploidxxx package
final ResourceConfig rc = new ResourceConfig().packages("com.apploidxxx");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
private static URI getBaseURI(int port) {
return UriBuilder.fromUri("http://0.0.0.0/").port(port).build();
}
/**
* Starts Heroku Grizzly HTTP server
* @return Grizzly HTTP server
*/
private static HttpServer startServerHeroku(URI uri) {
setUpLogger();
// create a resource config that scans for JAX-RS resources and providers
// in com.apploidxxx package
final ResourceConfig rc = new ResourceConfig().packages("com.apploidxxx");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(uri, rc);
}
private static void setUpLogger(){
// Setup Hibernate Logger level
Logger hibernateLog = Logger.getLogger("org.hibernate");
hibernateLog.setLevel(Level.SEVERE);
// Setup Handlers Logger level
Logger l = Logger.getLogger("org.glassfish.grizzly.http.server.HttpHandler");
l.setLevel(Level.FINE);
l.setUseParentHandlers(false);
// Adding console handler and setting level
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.ALL);
l.addHandler(ch);
}
public static void main(String[] args) throws IOException {
final HttpServer server = startServerHeroku(getBaseURI(Integer.parseInt(System.getenv("PORT"))));
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI_HEROKU));
while(true) {
System.in.read();
}
}
}
<plugins>
.
.
.
<plugin>
<groupId>com.heroku.sdk</groupId>
<artifactId>heroku-maven-plugin</artifactId>
<version>2.0.8</version>
<configuration>
<appName>helios-service</appName>
<processTypes>
<web>java $JAVA_OPTS -cp target/classes:target/dependency/* com.apploidxxx.Main</web>
</processTypes>
</configuration>
</plugin>
.
.
.
</plugins>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment