Registering Resources in Dropwizard

I cannot register multiple resources in the method of run()my Dropwizard application. When I do this, I get 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)
+4
source share
2 answers

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

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

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

Hope this solves your problem.

+4
source

if you want to start databases using Hibernate, for example. using the following code snippet in the main application (twice):

private final HibernateBundle<Cfg> hib1 = new HibernateBundle<Cfg>(ImmutableList.of(SomeHibernateModel.class), new SessionFactoryFactory()) {
    @Override
    public DataSourceFactory getDataSourceFactory(Cfg configuration) {
        return configuration.getDatabaseFactory();
    }
};

: 'name(). ( ".postgresql.active" ). :

private final HibernateBundle<Cfg> hib1 = new HibernateBundle<Cfg>(ImmutableList.of(SomeHibernateModel.class), new SessionFactoryFactory()) {
    @Override
    public DataSourceFactory getDataSourceFactory(Cfg configuration) {
        return configuration.getDatabaseFactory();
    }
    @Override
    protected String name() {
        return "hibernate.accesslog";
    }

};
+3

Source: https://habr.com/ru/post/1583932/


All Articles