挂起MDB消息处理,直到@StartupBean完成初始化 [英] Suspend MDB message processing until @StartupBean has finished initialization
问题描述
将JBoss 5应用程序迁移到JBoss AS 7(7.1.1.FINAL)时,我遇到了新的JMS消息驱动的EJB问题。在消息处理中,必须检查一些主数据字段。为了提高性能,应使用 @Singleton @Startup
EJB将主数据预加载到缓存结构中,加载该数据大约需要30秒。
While migrating a JBoss 5 application to JBoss AS 7 (7.1.1.FINAL) I have a problem with a new JMS message driven EJB. Within message processing, some master data fields have to be checked. To enhance performance, this master data shall be preloaded into a cache structure using a @Singleton @Startup
EJB, which needs about 30 seconds to load the data.
我的问题是,即使尚未完全初始化高速缓存,队列消息处理也会开始,从而导致消息验证错误。
My problem is that the queue message processing starts even if the cache has not been fully initialized, causing message validation errors.
I试图定义MDB和启动EJB之间的依赖关系,但据我了解, @DependsOn
注释仅适用于 @Singleton
EJB。因此很明显,我的解决方案无效;-)
I tried to define a dependency between the MDB and the startup EJB, but as far as I understood the @DependsOn
annotation works only with @Singleton
EJBs. So it's clear that my solution does not work ;-)
启动bean代码:
@Singleton
@Startup
public class StartupBean {
@PostConstruct
void atStartup() {
// TODO load master data cache (takes about 30 seconds)
}
@PreDestroy()
void atShutdown() {
// TODO free master data cache
}
}
注意:我从示例中剥离了真实代码,以使其更易于阅读: -)
Note: I stripped the real code from the example to make it easier to read :-)
消息驱动bean代码:
Message driven bean code:
@MessageDriven(name="SampleMessagingBean", activationConfig = {
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="jms/SampleQueue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
})
@DependsOn("StartupBean")
public class SampleMessagingBean implements MessageListener {
public void onMessage(Message message) {
// TODO validate message using master data cache
}
}
问题:我可以在启动bean完成加载缓存之前暂停消息处理吗?
Question: How can I suspend message processing until the startup bean has finished loading the cache?
任何建议,请多谢:-)!
Any suggestions greatly appreciated :-)!
推荐答案
首先,我认为在mdb中注入单例EJB足以延迟消息的消耗
,但是不,有时候,它会在Singleton-ejb的@PostConstruct完成之前开始消耗消息。因此,还添加了一个方法调用,它开始起作用
First i thought inject singleton EJB in mdb would be enough to delay message consumption But no, sometimes it would start consuming the message before @PostConstruct of Singleton-ejb completed. So added a method invocation also and it started working
这对玻璃鱼有效,但是我看不出它不应该在jboss上工作的原因
This worked on glassfish, but i dont see a reason why it shouldnt work on jboss
Singleton-Ejb:
Singleton-Ejb:
@Singleton
@Startup
public class SingletonBean {
private Logger logger = Logger.getLogger(getClass().getName());
private boolean init = false;
public boolean isInit() {
return init;
}
@PostConstruct
public void init() {
logger.error("singleton init start");
//Do something that takes time here
init = true;
logger.error("singleton init end ");
}
}
和mdb:
@MessageDriven(...)
public class SomeMdb implements MessageListener {
private Logger logger = Logger.getLogger(getClass().getName());
@EJB
SingletonBean sb;
@PostConstruct
public void init() {
logger.error("mdb init start");
if (!sb.isInit()) {
logger.error("never happens");
}
logger.error("mdb init complete");
}
public void onMessage(Message message) {
logger.error("onMessage start");
}
}
现在,它总是等待SingletonBean在mdb之前完成初始化完成初始化(如日志所示)
Now it always waits for SingletonBean to complete init before mdb completes init (as seen in log)
19:51:51,980 [ad-pool-1; w: 3] ERROR SomeMdb - mdb init start
19:51:52,122 [ad-pool-4848(4)] ERROR SingletonBean - singleton init start
19:51:56,316 [ad-pool-4848(4)] ERROR SingletonBean - singleton init end
19:51:56,317 [ad-pool-1; w: 3] ERROR SomeMdb - mdb init complete
19:51:56,317 [ad-pool-1; w: 3] ERROR SomeMdb - onMessage start
这篇关于挂起MDB消息处理,直到@StartupBean完成初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!