Hammock is based on my last blog post, creating a light weight service to run JAX-RS resources over a minimalistic configuration.
Binaries are currently up on Sonatype OSS (I hope they sync to MVN central shortly): https://oss.sonatype.org/index.html#nexus-search;quick~ws.ament.hammock
Github: https://github.com/johnament/hammock
What is Hammock?
Hammock is a light weight integration between JBoss RestEasy, Undertow and Weld. Leveraging Weld SE, it provides automatic resource scanning and minimal binding code to launch a web container (Undertow) running the basic services to launch a full JAX-RS application.
Getting Started
Getting started with Hammock is simple. Add a reference to the project in your pom.xml:
Add your REST Resource class:
@Path("/echo")
@RequestScoped
public class EchoResource {
@GET
@Produces("text/plain")
public String greet() {
return "hello";
}
}
Implement the configuration, for application (via @ApplicationConfig)
@ApplicationConfig
@ApplicationScoped
public class ApplicationConfigBean implements WebServerConfiguration {
@Override
public int getPort() {
return 8080;
}
@Override
public String getContextRoot() {
return "/api";
}
@Override
public Collection getProviderClasses() {
return Collections.EMPTY_LIST;
}
@Override
public Collection getResourceClasses() {
return Collections.singleton(EchoResource.class);
}
@Override
public String getBindAddress() {
return "0.0.0.0";
}
}
You can optionally also do this for a management interface as well (via @ManagementConfig). The resources tied to each of these configurations would then be launched when you start your application.
Starting your app can be done manually via Weld SE, or by using their built in class, org.jboss.weld.environment.se.StartMain .
Cool stuff John. Glad to see you taking your code/ideas we discussed on the resteasy list and making it a reality. Let me know of any changes you need in resteasy to make this work
ReplyDeleteInteresting. Would be cool to see how this performs vs Wildfly to get an idea of the additional overhead introduced since they both use the same components.
ReplyDeleteThere was some discussion of adding Wildfly to the TechEmpower benchmark project, but it doesn’t look like it has been done yet: http://lists.jboss.org/pipermail/wildfly-dev/2014-February/001729.html
Would be awesome to see them both up there.
I've been searching for a while for a simple, embedded CDI/REST setup and found Hammock last week. Very nice!
ReplyDeleteWhat would be a simple way to add a regular http servlet?
Thanks,
Allen
I think I figured it out. Not sure if this is the best approach:
ReplyDeleteServletInfo defaultServlet = Servlets.servlet("DefaultServlet", DefaultServlet.class)
.setLoadOnStartup(1)
.addMapping("/web/*");
... .addServlet(resteasyServlet).setDeploymentName("ResteasyUndertow")
.addServlet(defaultServlet).setDeploymentName("DefaultUndertow")
.setResourceManager(new FileResourceManager(new File("web"), 1000))
Hi John, the idea is brilliant and hammock works like a charm for creating standalone rest services. But I still wonder, what the ContextRoot configuration property is used for. How does it relate to the @ApplicationPath annotation used for Jax-RS Application? It seems to be internally added UriInfo request path but when I enter it in the browser it seems to be added again, ending up with the error: Could not find resource for full path: http://localhost:80/contextRoot/contextRoot/resource
ReplyDelete