【发布时间】:2018-06-19 11:30:19
【问题描述】:
我想使用 Dropwizard 后端发出定期 REST 请求。更具体地说,我想每分钟向外部 REST API 发出 GET 请求并处理结果。
我使用了quartz here,现在我尝试使用jersey 客户端发出REST 请求。我使用 guice 作为我的依赖注入。
我的应用类有以下方法
@Override
public void initialize(final Bootstrap<DockerwizardConfiguration> bootstrap) {
Job everyJob = new EveryTestJob();
bootstrap.addBundle(new JobsBundle(everyJob));
}
@Override
public void run(final DockerwizardConfiguration configuration,
final Environment environment) {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(HelloWorldParameter.class)
.annotatedWith(Names.named("helloWorldParameter"))
.toInstance(configuration.getHelloWorldParameter());
}
});
JerseyClientConfiguration conf = configuration.getJerseyClientConfiguration();
conf.setChunkedEncodingEnabled(false);
final Client client = new JerseyClientBuilder(environment).using(conf).build(getName());
environment.jersey().register(new ExternalServiceResource(client)); // How should that be implented with guice
environment.jersey().register(injector.getInstance(HelloWorldResource.class));
}
而我的 EveryTestJob 类实现如下
@Every("1s")
public class EveryTestJob extends Job {
@Override
public void doJob(JobExecutionContext context) throws JobExecutionException {
// logic run every time and time again
}
}
我不确定如何组织它。
【问题讨论】:
-
代码中的一切看起来都很好,您遇到什么问题了吗?
-
我不知道如何将客户端对象注入到资源中,还是应该注入到
EveryTestJob -
所有进行rest调用所需的代码都应该在EveryTestJob中注入和处理
-
我认为
EveryTestJob必须在initialize方法中用bootstrapJobbundle实例化
标签: java jersey quartz-scheduler guice dropwizard