Skip to content

Instantly share code, notes, and snippets.

@nlinker
Last active March 16, 2020 10:05
Show Gist options
  • Save nlinker/9cac639e8c3b87e3d029 to your computer and use it in GitHub Desktop.
Save nlinker/9cac639e8c3b87e3d029 to your computer and use it in GitHub Desktop.
Embedded Jetty + Jersey + Guice
package com.vertigo.service.rest.search;
import java.util.EnumSet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import com.vertigo.service.rest.search.filter.GuiceFilter;
import com.vertigo.service.rest.search.listener.GuiceServletContextListener;
/**
* The class runs the search service inside the embedded Jetty container
* Jetty + Guice + Jersey.
* Make sure dependencies are updated (servlet-api must be not provided)
* <pre>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.3.5.v20151012</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.3.5.v20151012</version>
</dependency>
* </pre>
* @author nlinker (28.10.2015 14:37)
*/
public class App {
public static void main(String[] args) throws Exception {
Server jettyServer = new Server(8080);
// tell jetty to ask Guice about what servlet should handle the requests
ServletContextHandler context = new ServletContextHandler(
jettyServer, "/rest", ServletContextHandler.SESSIONS);
// attach listener who instantiate our injector
context.addEventListener(new GuiceServletContextListener());
context.addFilter(GuiceFilter.class, "/*",
EnumSet.of(javax.servlet.DispatcherType.REQUEST,
javax.servlet.DispatcherType.ASYNC));
// DefaultServlet is at the end of the pipeline to handle what Guice didn't
context.addServlet(DefaultServlet.class, "/");
try {
jettyServer.start();
jettyServer.join();
} finally {
jettyServer.destroy();
}
}
}
package com.vertigo.service.rest.search.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
@WebFilter("/*")
public class GuiceFilter extends com.google.inject.servlet.GuiceFilter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
super.doFilter(request, response, chain);
}
}
package com.vertigo.service.rest.search.listener;
import javax.servlet.annotation.WebListener;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.vertigo.rest.module.SwaggerModule;
import com.vertigo.service.rest.search.module.ConfigurationModule;
import com.vertigo.service.rest.search.module.JerseyServletModule;
import com.vertigo.service.rest.search.module.ServicesModule;
import org.apache.bval.guice.ValidationModule;
@WebListener
public class GuiceServletContextListener extends com.google.inject.servlet.GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(
new ConfigurationModule(),
new SwaggerModule(getClass().getPackage().getImplementationVersion()),
new JerseyServletModule(),
new ValidationModule(),
new ServicesModule()
);
}
}
@dhruvpsaru
Copy link

How can we add idle Time Out and other properties like that?

@nlinker
Copy link
Author

nlinker commented Mar 13, 2020

@dhruvpsaru we might pass some Config object to GuiceServletContextListener and then bind it inside the injector.

@dhruvpsaru
Copy link

Ok I did it like this, defined my own connector,

Server server = new Server()
 Connector connector = new SelectChannelConnector();
            connector.setPort(port);
            connector.setHost(host);
            connector.setMaxIdleTime(1000 * 60 * 20);
            server.setConnectors(new Connector[]{connector});

@nlinker
Copy link
Author

nlinker commented Mar 14, 2020

Nice!

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