我无法在Dropwizard应用程序的run()方法中注册多个资源。当我这样做时,我得到了以下异常:
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)发布于 2015-04-30 14:24:54
似乎您正在为每个资源初始化两次Postgres实例,这并不是必需的。你可以简单地先初始化所有的存储,然后注册你的资源。如下所示:
/* Service manager */
environment.lifecycle().manage(new XYZServiceManager());
/* Adding Resources */
environment.jersey().register(new FirstResource());
environment.jersey().register(new SecondResource());希望这能解决你的问题。
发布于 2015-08-09 03:18:24
如果你碰巧使用Hibernate初始化数据库,例如。通过在主应用程序中使用以下代码段(两次):
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";
}
};https://stackoverflow.com/questions/29788929
复制相似问题