Forbidden technique: Multi-threading inside J2EE server
It is an old post based on old idea, it is not relevant anymore.
It was in 2009, I just wrote a multi-threaded code to fetch data from two DB2 servers. They are both physically separated. My intension was to get multiple data simultaneously, so JavaBeans will work quickly. I uploaded my multi-threaded J2EE application in the production server, after one or two months later the whole production server was terribly slowed down. I thought there is memory leak problem inside the server. However, I also checked which application is taking two much CPU and memory. I found that my implemented multi-threaded application consuming vast amount of computing resources.
I was surprised and I researched on the multi-threading in J2EE. I found from several tech-forums that multi-threading inside J2EE server is forbidden technique. Hmmmm, WHY?
To understand why J2EE does not support multi-threading [7], we need to understand fundamental theory of J2EE server at first. J2EE server is not single component. J2EE server is composed of multiple components and services. The main component is called Container. In plain English container holds server-side Java application or simply J2EE application. Container gives such environment where server-side Java application can survive and run according to the client HTTP request. Without container server side Java application cannot be launched [1]. Container provides various build-in services such as security, transactions, resources management, database related tasks and many more [1].
There is two kind of container inside the J2EE server:
- Web Container
- EJB Container
Web Container provides few services such as security of the running J2EE applications. It contains JSP and Servlet file. Therefore, Web Container nothing but a servlet engine [2]. Web Container is enough for a project where resource management and data integrity are not important concerns. For example Tomcat is one of the most notable Web Container. Technically, Tomcat is not server though most of the people mistakenly consider it as server.
EJB Container:
Enterprise JavaBeans (EJB) container provides to run EJB related code. It also provides three main services such as resource management, data integrity, and security [3]. EJB container is not responsible to send or get HTTP request. It does not know anything about web. So web container will get the request from client and bypass the request to EJB container to do the business process. From this little theory we can conclude that EJB is necessary to do scalable business process. On the other hand a servlet oriented application is failed to do scalable business process.
Now, we have got the very basic discussion of the J2EE server. So from the above discussion, it is clear that J2EE application cannot directly talk with JVM without container. A programmer cannot do anything without following Container specifications. J2EE server provides managed environment via container. Therefore, J2EE server is also designed to manage all kind of threads inside JVM [4]. So a custom thread cannot be managed by application which is running inside J2EE server. And container cannot kill or stop specific custom thread. The unmanaged thread impacts the J2EE server very badly [4]. From my own experience, custom threads fail to access other resources also because J2EE does not know about the existence of the custom thread [4].
So what is the solution to overcome multi-threading problem inside J2EE server?
At present there is two solutions:
- Work Manager API which is vendor specific. According to the my best knowledge only IBM and BEA provides the Work Manager API functionality with their server [5].
- EJB and JMS. However, using EJB with JMS we can achieve multi-threading functionality without locking with the Vendor [6].
References:
[1] S. Haugland, M. Cade, A. Orapallo, "Component of a J2EE Application Server" in J2EE 1.4 The Big Picture, New Jersey, USA: Prentice Hall, 2004, pp 27.
[2] S. Haugland, M. Cade, A. Orapallo, "There's Web Container Too" in J2EE 1.4 The Big Picture, New Jersey, USA: Prentice Hall, 2004, pp 28.
[3] S. Haugland, M. Cade, A. Orapallo, "The Container That EJBs Run Inside Is an EJB Container" in J2EE 1.4 The Big Picture, New Jersey, USA: Prentice Hall, 2004, pp 28
[4] Java Developer Journal (JDJ) (2010, April 02) "Designing Custom Multi-threading Frameworks in J2EE Containers" [Online]. Available at http://java.sys-con.com/node/83057
[5] DevX.com (2010, April, 03) The Work Manager API: Parallel Processing Withing a J2EE Container [Online]. Available at: http://www.devx.com/Java/Article/28815
[6] S. Haugland, M. Cade, A. Orapallo, "Message Driven Beans" in J2EE 1.4 The Big Picture, New Jersey, USA: Prentice Hall, 2004, pp 142.
[7] S. Haugland, M. Cade, A. Orapallo, "Introduction to JSP, Servlet and EJB" in J2EE 1.4 The Big Picture, New Jersey, USA: Prentice Hall, 2004, pp 50.
[1] http://stackoverflow.com/questions/533783/why-spawning-threads-in-j2ee-container-is-discouraged
Comments
Post a Comment