Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering resources in Dropwizard

Tags:

dropwizard

I am unable to register multiple resources in the run() method of my Dropwizard application. When I do,I am getting the following exception:

Exception in thread "main" MultiException[java.lang.IllegalArgumentException: A metric named io.dropwizard.db.ManagedPooledDataSource.postgresql.active already exists, java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@27f74733]
at org.eclipse.jetty.server.Server.doStart(Server.java:329)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:43)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76)
at io.dropwizard.cli.Cli.run(Cli.java:70)
at io.dropwizard.Application.run(Application.java:73)
at com.xxx.xxx.yyy.GobblerHTTPApplication.main(GobblerHTTPApplication.java:19)
like image 357
Amith Gopal Avatar asked Apr 22 '15 05:04

Amith Gopal


People also ask

What is resource in Dropwizard?

A resource is what REST services are all about. It is nothing but an endpoint URI for accessing the resource on the server. In this example, we'll have a resource class with few annotations for request URI mapping. Since Dropwizard uses the JAX-RS implementation, we will define the URI path using the @Path annotation.

Which is better spring boot vs Dropwizard?

The bare bone spring boot app starts in 1.64 seconds whereas the bare bone Dropwizard app took 1.526 seconds to startup. Spring Boot consumes much more memory. This was true. Spring Boot loaded 7591 classes whereas Dropwizard loaded 6255 classes.

Does Dropwizard use Log4j?

Dropwizard uses Logback for its logging backend. It provides an slf4j implementation, and even routes all java. util. logging , Log4j, and Apache Commons Logging usage through Logback.


1 Answers

It seems you are initializing instances of Postgres twice for each resource which is not required. You can simply first initialize all your stores, etc. and then register your resources. Do it something like as follows:-

    /* Service manager */
    environment.lifecycle().manage(new XYZServiceManager());

    /* Adding Resources */
    environment.jersey().register(new FirstResource());
    environment.jersey().register(new SecondResource());

Hope this will solve your problem.

like image 163
hatellla Avatar answered Oct 28 '22 19:10

hatellla