Dropwizard – The single greatest framework ever created! – Approach



Dropwizard – The single greatest framework ever created! – Approach

0 0


1devday-slides


On Github challendy / 1devday-slides

Dropwizard

The single greatest framework ever created!

(at least thats what they say ;))

Chris Hallendy

Who am I?

technologist @ MadDog Technology

challendy@maddogtechnology.com

https://github.com/challendy

Approach

  • Service-oriented architecture (SOA)
  • Micro Services
martinfowler.com/articles/microservices.html

Conway's Law implies that micro-services lead to small independent teams

Frameworks for micro-services

  • Dropwizard (Java)
  • Spring Boot (Java)
  • Finegle (Scala)
  • Sinatra (Ruby)
  • Scalatra (Scala)
  • Flask (Python)

What is Dropwizard

Dropwizard is a heavily opinionated framework for building web services on the JVM.  It is mostly glue around mature java libraries like Jetty, Jersey, Jackson and Guava.
Dropwizard has out-of-the-box support for sophisticated configuration, application metrics, logging, operational tools, and much more, allowing you and your team to ship a production-quality HTTP+JSON web service in the shortest time possible.

Who Created it?

@coda
As I've said before, the only reason Dropwizard exists at all is to provide opinions on what a service should be. I think they work better.It embeds Jetty because I think that works better.It uses Jackson because I think that works better.It uses Jersey because I think that works better.It has a single YAML configuration file because I think that works better. It wraps Logback because I think that works better.

Why use it?

Benchmarks
dropwizard.io

How does it work?

Deployed as a fat jar and starts jetty. No need for a container.
                public static void main(String[] args) throws Exception {
                  new StoreApplication().run(args);
                }
            
Start the server from the command line by running:
java -jar target/store-dropwizard-0.0.1-SNAPSHOT.jar server example.yml

The Service

Services are a collection of bundles, commands, healthchecks, tasks and resources. The service class defines all of the abilities of your application.
@Override
public void initialize(Bootstrap bootstrap) {
    bootstrap.addBundle migrationsBundle
    bootstrap.addBundle hibernateBundle
}
@Override
public void run(StoreConfiguration configuration,
                    Environment environment) throws ClassNotFoundException {

  final VisitDAO dao = new VisitDAO(hibernateBundle.getSessionFactory());
  final Template template = configuration.buildTemplate();

  environment.healthChecks().register("template", new TemplateHealthCheck(template));
  environment.jersey().register(new VisitResource(dao));
}
            

The Resource

Resources model what is exposed via your RESTful API. Dropwizard uses Jersey for this so these classes are mostly jersey annotations.
@Path("/visits")
@Produces(MediaType.APPLICATION_JSON)
public class VisitResource  {

    private final VisitDAO visitDAO;

    public VisitResource(VisitDAO visitDAO) {
        this.visitDAO = visitDAO;
    }
    
    @Timed(name = "createVisit")
    @POST
    @UnitOfWork
    public Visit createVist(Visit visit) {
        return visitDAO.create(visit);
    }

    @GET
    @UnitOfWork
    public List<visit> listVisits() {
        return visitDAO.findAll();
    }
}            </visit>

The Representation

Your POGOs will be turned into JSON via Jackson.
@Entity
@Table(name = "visits")
@NamedQueries({
        @NamedQuery(
                name = "com.mystore.core.Visit.findAll",
                query = "SELECT v FROM Visit v"
        )
})
public class Visit {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @Column(name = "user_id", nullable = false)
    private long userId;
    
    @Column(name = "beacon_id", nullable = false)
    private String beaconId;
}
            

Metrics

Yammer Metrics is built in and provides metrics over an administration port (8081). Resources can be annotated with @Timed or @Metered, and @ExceptionMetered. Dropwizard augments Jersey to automatically record runtime information about your resource methods.
metrics

Health Checks

Health Checks are a method to make sure the infrastructure your service depends on are all running. They are accessible on the administration port.
public class MySQLHealthCheck extends HealthCheck {
    private final SessionFactory sessionFactory

    public MySQLHealthCheck(SessionFactory sessionFactory) {
        super("MySQL")
        this.sessionFactory = sessionFactory
    }

    @Override
    protected com.yammer.metrics.core.HealthCheck.Result check() {
        if (!sessionFactory.closed) {
            return healthy()
        } else {
            return unhealthy('Session Factory is Closed!')
        }
    }
}
    
    

Bundles

Bundles are reusable blocks of behaviour designed to be reused across services. Assets, Hibernate and Liquibase are all implemented as Dropwizard Bundles.

Commands

Commands add options to the command line interface of your service. For example the server starts based on the 'server' command. Migrations run based on the 'db migrate' command. You might add your own command for running functional tests or seeding the database.

Tasks

Tasks are run time actions available over the administration port. Dropwizard ships with a garbage collection task. You might want to right a task to clean a cache by key.

Other Stuff

  • Configuration
  • Logging
  • Hibernate/JDBI
  • Clients
  • Authentication
  • Views

Code Time

What problem are we trying to solve?

Tracking iBeacon Visits from a device.

Thanks To:

Hakim El Hattab (reveal.js)

Kevin Boon (Grails and Dropwizard)