gpt4 book ai didi

java - Glassfish 仅限于使用 JPA/EJB/Web 服务的两个同时线程

转载 作者:行者123 更新时间:2023-11-29 07:22:29 26 4
gpt4 key购买 nike

我正在通过 Web 服务公开以下 EJB3 无状态 session bean。

@Stateless
public class UserRoleFacade implements UserRoleFacadeLocal {
@PersistenceContext(unitName = "SimpleEA-ejbPU")
private EntityManager em;

public int count() {
System.out.println("thisClass=" + this.getClass().getSimpleName() + "@" + this.hashCode() + ", em=" + em);
try {
Thread.sleep(10000); // 10 sec
} catch (InterruptedException ex) {
Logger.getLogger(UserFacade.class.getName()).log(Level.SEVERE, null, ex);
}
return 0;
}

}

如您所见,它并没有做太多事情——事实上它除了在收到请求后 hibernate 10 秒外什么也没做。我创建此代码作为实验,以了解有关 EntityManager 如何在多线程环境中工作的更多信息。

Web 服务如下所示:

@WebService(serviceName="UserRole")
public class UserRoleWebService {
@EJB
private UserRoleFacadeLocal ejbRef;// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Web Service Operation")

@WebMethod(operationName = "count")
public int count() {
return ejbRef.count();
}

}

为了进行测试,我启动了 5 个浏览器,指向 Web 服务测试器,然后将它们全部启动。这是打印出来的结果:

INFO: thisClass=UserRoleFacade@11511572, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@23961
INFO: thisClass=UserRoleFacade@32513964, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@1af5c5a
INFO: thisClass=UserRoleFacade@11511572, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@23961
INFO: thisClass=UserRoleFacade@32513964, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@1af5c5a
INFO: thisClass=UserRoleFacade@11511572, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@23961

<罢工>

为了进行测试,我采纳了 Bozho 的建议并使用 15 个线程设置 JMeter。这是打印出来的结果:

INFO: thisClass=UserRoleFacade@3869465, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@55a5d0, thread=Thread[http-thread-pool-17025-(2),10,Grizzly]
INFO: thisClass=UserRoleFacade@5558947, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@d0e940, thread=Thread[http-thread-pool-17025-(1),10,Grizzly]
INFO: thisClass=UserRoleFacade@20208512, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@635f47, thread=Thread[http-thread-pool-17025-(39),10,Grizzly]
INFO: thisClass=UserRoleFacade@23924919, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@16478c1, thread=Thread[http-thread-pool-17025-(41),10,Grizzly]
INFO: thisClass=UserRoleFacade@6172173, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@121691b, thread=Thread[http-thread-pool-17025-(40),10,Grizzly]
INFO: thisClass=UserRoleFacade@3869465, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@55a5d0, thread=Thread[http-thread-pool-17025-(2),10,Grizzly]
INFO: thisClass=UserRoleFacade@5558947, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@d0e940, thread=Thread[http-thread-pool-17025-(1),10,Grizzly]
INFO: thisClass=UserRoleFacade@23924919, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@16478c1, thread=Thread[http-thread-pool-17025-(39),10,Grizzly]
INFO: thisClass=UserRoleFacade@20208512, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@635f47, thread=Thread[http-thread-pool-17025-(40),10,Grizzly]
INFO: thisClass=UserRoleFacade@6172173, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@121691b, thread=Thread[http-thread-pool-17025-(41),10,Grizzly]
INFO: thisClass=UserRoleFacade@3869465, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@55a5d0, thread=Thread[http-thread-pool-17025-(2),10,Grizzly]
INFO: thisClass=UserRoleFacade@5558947, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@d0e940, thread=Thread[http-thread-pool-17025-(1),10,Grizzly]
INFO: thisClass=UserRoleFacade@23924919, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@16478c1, thread=Thread[http-thread-pool-17025-(39),10,Grizzly]
INFO: thisClass=UserRoleFacade@6172173, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@121691b, thread=Thread[http-thread-pool-17025-(41),10,Grizzly]
INFO: thisClass=UserRoleFacade@20208512, em=com.sun.enterprise.container.common.impl.EntityManagerWrapper@635f47, thread=Thread[http-thread-pool-17025-(40),10,Grizzly]

如您所见, (以及我从浏览器中看到的)是只有 2 只有 5 个线程同时运行。 EJB 和 EntityManager 的实例看起来都可能在某处受到限制。那是哪里?

在 Glassfish 应用服务器上,我的设置看起来是:

  • 连接池:8个初始连接,最多32个
  • ejb 容器:初始 0 个,最大 32 个

我猜这些没问题。 EnitiyManager 在某处有设置吗?我还应该看什么?

最佳答案

在管理控制台中检查 configuration>thread-pools>thread-pool#min-thread-pool-size(应该是 2)。但这只是一分钟,当然不是最大值。

关于java - Glassfish 仅限于使用 JPA/EJB/Web 服务的两个同时线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2292439/

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