- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将现有的 spring 应用程序与自定义的 elasticsearch river 集成,ES rivers 使用 Google Guice 管理它们的依赖关系并在它们自己的线程集中运行。
我创建了一个简单的类,它返回对 spring 上下文的静态引用,并配置了一个从 spring 上下文返回对象的 Guice 模块。为了确保 guice 线程和 spring 线程之间的正确同步,我使用了在上下文完全初始化后释放的 CountDownLatch
。这是一些代码
public class GuiceSpringIntegrator implements ApplicationListener<ContextRefreshedEvent> {
private static ApplicationContext context;
private static final CountDownLatch contextLatch = new CountDownLatch(1);
@Override public void onApplicationEvent(ContextRefreshedEvent event) {
try {
// check some stuff in context
} finally {
log.debug("Setting application context as static field of {}", getClass().getSimpleName());
GuiceSpringIntegrator.context = event.getApplicationContext();
log.info("Releasing latch for application context");
contextLatch.countDown();
}
}
public static ApplicationContext getApplicationContext() {
if (null == context) {
log.info("ApplicationContext not yet initialized, wait for it in thread {}", Thread.currentThread().getName());
Uninterruptibles.awaitUninterruptibly(contextLatch); <-- !!! SPRING INITIALIZATION CODE HANGS HERE
log.debug("Returning application context since now context is initialized");
Preconditions.checkState(context != null, "ApplicationContext should have been initialized properly");
}
return context;
}
}
这是使用上面类的guice模块
/**
* Guice module configuration
*/
public class ElasticSearchModule extends AbstractModule {
@Override
protected void configure() {}
@Provides @Singleton TaskScheduler getSchedulerInstance() {
return GuiceSpringIntegrator.getApplicationContext().getBean(TaskScheduler.class);
}
// and so on...
}
但是,当我启动应用程序(尤其是在快速服务器上)时,应用程序偶尔会在上面代码中标记的行处挂起。我仔细检查了导致 #getApplicationContext()
方法调用的每个代码路径,它们(应该)由 guice 调用,因此最终应该释放闩锁,代码应该继续。
#getApplicationContext
?#onApplicationEvent
似乎在上下文初始化实际完成之前被调用?最佳答案
除了第 4 点之外,这并没有真正回答您的问题,但我想编写代码,所以我将其作为答案而不是评论。
我会尝试改变这个:
Uninterruptibles.awaitUninterruptibly(contextLatch); <-- !!! SPRING INITIALIZATION CODE HANGS HERE
为此:
boolean awaitResult = Uninterruptibles.awaitUninterruptibly(contextLatch, 2, TimeUnit.MINUTES);
if(awaitResult) {
throw new IllegalStateException("Unable to load Spring Context");
}
然后您可以在某处捕获该异常并将其堆栈跟踪写入日志;这不会解决您的问题,但它可能会让您更清楚地了解死锁的位置?
关于java - google guice 和 spring 的线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23501967/
我有很多 TabularResultSet 类的带注释的命名实例,我想将它们传递给多个类并传递给静态最终属性。我该如何注入(inject)?我认为它不喜欢该属性的最终和静态性质。必须在属性声明中直接调
我是 Guice 的新手,我想知道我能走多远。 我有一个接口(interface)UserInfo具有多个实现类 GoogleUserInfo , FacebookUserInfo , Twitter
我的应用程序中绑定(bind)了两个类型的命名实例: bind(Foo.class).toProvider(FooProvider.class); bind(Foo.class).annotatedW
我有一个 Guice 模块,它有一个 @Provides 方法,它接受 2 个参数并返回接口(interface)的实现: public class **ClientModule** extends
请解释在以下使用工厂的场景中使用 Google-Guice 自动连接的正确方法。我正在使用 XML 工厂进行 XSLT 处理。 StringWriter strWriter = new StringW
我有一个 Guice Module提供 List使用 @Provides - 带注释的方法。 class TestModule() : Module { override fun configur
为什么Guice 3.0针对错误的配置组件(例如,缺少@Inject)抛出此异常,而不是格式化消息? Exception in thread "main" com.google.inject.inte
我有以下供应商: public class GuiceResourceProvider implements Provider { @Inject private Configur
使用 Guice-servlet,我们可以轻松地进行 servlet 映射,如下所示: filter(*.jsp).through(MyFilter.class) 但是,谁能告诉我如何将过滤器映射到
我正在尝试向我的项目添加 Swagger 。我们的设置与示例项目略有不同。我们使用 guice 和 guice-servlet 来注入(inject)并启动我们的 JerseyServletModul
假设我有一个 Guice 模块 ProdModule,我想依赖其他 GuiceModule、ProdDbModule 和 ProdPubSubModule。我将如何实现 ProdModule 的 co
我正在考虑在我的应用程序中使用 Guice for DI,我应该能够在运行时交换实现。下面提供了一个示例来说明要求: class ValidationEngine { public void v
我想设置 Guice 绑定(bind),所以我用 Java 创建了一个完美运行的模块: public class CrashLoggerModule extends AbstractModule {
所以我有一个模块将接口(interface)与实现类绑定(bind)。 bind(ILocalStore.class).to(LocalStore.class); 此绑定(bind)是否还会注入(in
guice 4.0 是否向后兼容 3.x?无法从发行说明或常见问题解答中弄清楚... 如果没有,是否有兼容性问题列表? 最佳答案 我没有任何官方来源但根据我的经验,没有任何兼容性问题。 我使用了几个
我正在尝试使用 Guice,并且我来自 Spring。 我想知道 @Inject 是否相当于 Spring 中的 @Autowired 以及我是否可以在 Web 应用程序中使用它,就像在 Spring
我是 Guice DI 的新手。我想弄清楚我的情况。 简单来说,有没有通过Guice @annotations来替代MapBinder的? 我的场景: Interface A{} Class A1 i
我的项目正在使用 Guice作为负责为大型对象图(主要是单例)提供依赖项(服务类)的 IOC 容器。有时,如果在构造过程中依赖项失败,并且许多对象都需要此依赖项,则失败将一遍又一遍地发生,并将异常添加
我有一个类 (CustomConnectionProvider),它将由第三方库 (hibernate) 使用 class.forName().newInstance() 实例化。我需要注入(inje
删除 guice servlet 后,我需要进行一些清理。使用 guice servlet 时是否可以 Hook 到 servlet 破坏?我需要使用喷油器进行清理工作。 我可以覆盖 contex
我是一名优秀的程序员,十分优秀!