Wednesday, February 26, 2014

Announcing Hammock

I'd like to introduce the world to Hammock

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:


  ws.ament.hammock
  hammock-core
  0.0.1


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 .

5 comments:

  1. 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

    ReplyDelete
  2. Interesting. 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.

    There 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.

    ReplyDelete
  3. I've been searching for a while for a simple, embedded CDI/REST setup and found Hammock last week. Very nice!

    What would be a simple way to add a regular http servlet?

    Thanks,
    Allen

    ReplyDelete
  4. I think I figured it out. Not sure if this is the best approach:

    ServletInfo 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))

    ReplyDelete
  5. 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