【发布时间】:2014-10-14 08:07:15
【问题描述】:
我有一个应用程序,其中一些请求允许修改某些实体,而一个请求会阻塞直到执行修改(通知客户端的更改)
我做了以下事情:
@Entity
class TheEntity extends Model {
public static Collection<TheEntity> getLastEntities(after) {
//finds the entities where the `lastUpdateTimestamp` is greater than `after`
return TheEntity.find("byLastUpdateTimestampGreaterThan", after).fetch();
}
@PostUpdate
@PostPersist
private void notifyChannel() {
lastUpdateTimestamp = System.currentTime();
Job asyncNotifier = new Job() {
@Override
public void doJob() throws Exception {
//we manage the updates. the message channel trigs the channel
MyChannel.publish(AcEvent.this);
}
};
asyncNotifier.now();
}
}
现在,我有一个监听频道的控制器
public static void listenMessageUpdate(Long after,Long blockDuration) {
Collection<TheEntity> evts = MyChannel.getLast(after);
if (evts.isEmpty()) {
Timeout timeout = F.Timeout(blockDuration*1000);
Promise<TheEntity> nextFlt = MyChannel.nextEvent();
Either<Timeout, TheEntity> await = await(F.Promise.waitEither(
timeout, nextFlt));
evts = TheEntity.getLastEntities(after);
}
renderJSON(TheEntity.toJson(evts));
}
事实上,TheEntity.getLastEntities(after) 在 PROD 模式下不会返回最近修改或创建的元素。
经过调查,通知事件流时未提交事务,因此,实体在数据库中不是最新的
在每次实体修改时收到通知的最佳做法是什么?
【问题讨论】:
-
您能否将
asyncNotifier.now();更改为asyncNotifier.in(5),以便延迟作业初始化 -
@hzog 实际上这是一个非常糟糕的主意。在生产模式下,我在使用 postgresql 时遇到了这个非确定性错误:2014-11-29 18:46:04,239 错误 ~ 发生断言失败(这可能表明 Hibernate 中存在错误,但更可能是由于使用不安全造成的会话的)我最终丢弃了 PostUpdate 注释注释并取代了 save 方法来调用 notifyChannel()
标签: jpa playframework playframework-1.x