gpt4 book ai didi

java - JBOSS EAP 6 在异步方法之后阻止调用 ejb 方法

转载 作者:行者123 更新时间:2023-12-02 03:17:11 24 4
gpt4 key购买 nike

我有一个无状态 bean,它使用其他 bean 的异步方法(本地注入(inject))插入一些数据。此数据插入需要时间,因此我不等待完成此操作。插入数据后,我将调用同一 bean 的另一个方法。当我将调试点放入方法时,服务器等待大约 90 秒才能到达该点。可能是 Jboss 等待异步方法的事务完成。我不知道发生了什么事。 。

   @Stateless
public class SimulationNodePersistenceBean implements SimulationNodePersistenceRemote, SimulationNodePersistenceLocal {
@Resource
SessionContext context;

@EJB
private SimulationResultGraphPersitenceBean graphPersistenceBean;

@Asynchronous
@TransactionAttribute(TransactionAttributeType.REQUIRED)
private void addResultGraphsToDatabase(long id, Graph[] graphList) {

ResultGraph paramGraph;
ResultGraphPoint dataPoint;
Graph graph;
for (int i = 0; i < graphList.length; i++) {
graph = graphList[i];
paramGraph = new ResultGraph();

try {
graphPersistenceBean.persistGraph(paramGraph);
} catch (Exception databaseException) {
// TODO add error message to the contingency simulation messages
// list
logger.error("Error saving ResultGraph:" + paramGraph);
}
}
long duration = System.nanoTime() - startTime;
logger.debug("Graphs inserted to DB in (sec) :" + (duration / NANO_SECOND_CONVERSION_FACTOR));
}

// @Asynchronous
public void persistSimulationResults(long contingencySimulationId, Graph[] graphList,
List<AB> reiList) {
if (graphList != null) {
addResultGraphsToDatabase(contingencySimulationId, graphList);
}
if (reiList != null) {
//another method
}
calculateContSimStability(contingencySimulationId);
}

@Override
public void calculateSimIndex(long id) {

}

这是从主 bean 调用的其他 bean

 @Stateless
public class SimulationResultGraphPersitenceBean {
@PersistenceContext(unitName = "DBService")
private EntityManager em;

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Asynchronous
public void persistGraph(ResultGraph graph) throws SiGuardPersistenceException {
try {
ResultGraphService service = new ResultGraphService(em);
service.create(graph);
em.flush();
} catch (Exception ex) {
throw new PersistenceException("Error persisting graph", ex);
}
}

这是客户端调用主 bean。这是异步工作的。

    getSimulationEJB().persistSimulationResults(id, tsaParser.getLstFile().getGraphArray());

调用该方法后,我调用了SimulationNodePersistenceBean的另一个方法。该方法会等待几分钟。

getSimulationEJB().calculateSimIndex(contSimId);

我使用 jstack 创建了一个线程转储。实际上我在 Jboss As 6 中没有这个问题。我将我的应用程序迁移到 Jboss EAP 6。4。可能我需要在配置中进行一些配置更改。但我不知道我该怎么办。

我检查了线程转储。我没有发现任何处于 BLOCKING 状态的线程。我应该寻找其他关键字吗?

最佳答案

正如我在评论中已经指出的,您正在混合调用异步和同步方法。在您的示例中,您正在从 persistSimulationResults 方法(这是一个同步方法 - 因为您已注释掉其上的异步注释)调用 addResultGraphsToDatabase 方法(这是一个异步方法)。因此,尽管有异步注释,现在 addResultGraphsToDatabase 方法的行为类似于同步方法。

我不确定您是否查看了我在评论中发布的链接,但您需要使用 SessionContext 调用异步方法。像这样的事情:

在类(class)层面:

@Inject
SessionContext ctx;

在 persistSimulationResults 方法中:

ctx.addResultGraphsToDatabase

有关更详细的示例,请查看我在评论中发布的链接。

关于java - JBOSS EAP 6 在异步方法之后阻止调用 ejb 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40111525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com