本文介紹了Play Framework:為 Spring RabbitMQ 監聽器手動打開 JPA 上下文的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我正在使用 Spring-AMQP 來監控 Play 應用程序中的 RabbitMQ 消息隊列.
I am using Spring-AMQP to monitor a RabbitMQ message queue in a Play application.
問題是我無法從偵聽器代碼訪問我的數據庫,因為 JPA 上下文未在此范圍內打開.
The problem is I can not access my database from the listener code since the JPA context is not open in this scope.
我了解 Play Framework 管理 JPA 上下文,以便在處理 HTTP 請求時打開它,但是有沒有辦法可以從 Play 控制器/作業外部使用 JPA?
I understand Play Framework manages the JPA context so that it is open when processing HTTP requests, but is there a way I can use JPA from outside Play controllers/jobs?
推薦答案
剛剛找到答案是使用JPAPlugin!
Just found the answer was to use JPAPlugin!
監聽方法示例:
public void process(Message message) {
JPAPlugin.startTx(false);
boolean rollBack = false;
try {
// work with your models
JPA.em().flush();
} catch (RuntimeException e) {
rollBack = true;
// throw exception to prevent msg ACK, need to refine error handling :)
throw e;
} finally {
JPAPlugin.closeTx(rollBack);
}
}
這篇關于Play Framework:為 Spring RabbitMQ 監聽器手動打開 JPA 上下文的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!