Java 通知观察者事件为空时失败-尝试在事务失败时触发事件时失败
我需要在交易失败时得到通知。对于这种情况,我创建了事件观察者:Java 通知观察者事件为空时失败-尝试在事务失败时触发事件时失败,java,jakarta-ee,jboss,wildfly,java-ee-8,Java,Jakarta Ee,Jboss,Wildfly,Java Ee 8,我需要在交易失败时得到通知。对于这种情况,我创建了事件观察者: public class TransactionObserver { private static final Logger log = Logger.getLogger(TransactionObserver.class); @PersistenceContext(unitName = PERSISTENCE_NAME) private EntityManager em;
public class TransactionObserver {
private static final Logger log = Logger.getLogger(TransactionObserver.class);
@PersistenceContext(unitName = PERSISTENCE_NAME)
private EntityManager em;
//remove it to work correctly
@Inject
private SomeService someService;
public void observeAfterTransactionFailed(@Observes(during = TransactionPhase.AFTER_FAILURE) @Transaction Long id) {
log.error("Message from within transaction received after failure: " + id);
CustomEntity obj= em.find(CustomEntity.class, id);
someService.onTimeoutMarkAsFinished(id);
}
除非我删除注入的服务,否则这是不起作用的。我遇到以下错误:
ERROR [org.jboss.weld.Event] (Transaction Reaper Worker 0) WELD-000401: Failure while notifying an observer of event null
事件从如下方法激发:
@Asynchronous
@TransactionTimeout(value = 1, unit = TimeUnit.SECONDS)
public void doStuff() {
simpleMessageEvent.fire(stepId);
.
.
}
知道哪里出了问题,或者我怎样才能获得更多信息/日志
编辑:
交易代码:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
public @interface Transaction {
}
删除此批注没有帮助请为自定义的
@事务添加代码
批注好吗?您是否尝试了没有此批注的代码?@rieckpil我添加了代码。是的,我确实尝试在没有注释的情况下运行,但没有帮助。可能是我注射的服务有问题,有些东西不起作用