代码之家  ›  专栏  ›  技术社区  ›  Vinnie

glassfish限制为使用jpa/ejb/web服务同时运行两个线程

  •  1
  • Vinnie  · 技术社区  · 16 年前

    我将通过Web服务公开下面的EJB3无状态会话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;
        }
    
    }
    

    正如你所看到的,它并没有做太多的事情——事实上,它只是在收到请求后睡上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

    我想这些还可以。有什么地方可以安排神秘经理吗?我还应该看什么?

    2 回复  |  直到 16 年前
        1
  •  4
  •   Pascal Thivent    16 年前

    检查 配置线程池线程池线程池最小线程池大小 在管理控制台中(应该是2)。但那只是一分钟,当然不是最大值。

        2
  •  2
  •   Bozho    16 年前

    我怀疑有这样的限制。可能作业完成得很快,加上连接延迟,因此实体管理器被重用。

    为了获得更好的基准,请使用 JMeter -触发15个请求,每个请求以x秒为单位,并共享输出。( this article 开始使用JMeter。但这很容易)