问题描述
如果我的Web应用程序和ejb应用程序在同一台机器上(在同一个JVM上),所有的ejb调用都是本地调用,那么将使用 ThreadLocal 创建任何问题将信息从网络传递到ejb?
If my web application and ejb application are on the same machine (on same JVM) and all the ejb calls are local calls , will the use of ThreadLocal create any issue while passing information from web to ejb?
如果ejb调用是远程的,任何解决方法?将 ThreadLocal 信息可从Web应用程序到ejb应用程序?在这种情况下使用 ThreadLocal
Any workaround if the ejb calls are remote? Will ThreadLocal information be available from web application to ejb application? Is use of ThreadLocal advisable in such scenario?
推荐答案
问题,只要您在每次调用结束时删除ThreadLocal变量就没有问题。这很重要,因为容器(servlet或ejb)通常是用户线程池,因此重用线程,这有两个效果:一个调用可能会看到来自先前调用的threadlocal信息,并且如果从容器中删除应用程序而不停止JVM某些类可能不会被垃圾回收,因为它们仍然被容器线程引用。所以将数据放在一个try / finally块中的一个threadlocal中,并在finally部分删除。
For the first question, there is no problem as long as you remove the ThreadLocal variables at the end of every call. This is important because containers (servlet or ejb) typically user threadpools and therefore reuse threads, this has two effects: one "call" may see threadlocal info coming from a previous call, and if you remove an app from the container without stopping the JVM some classes may not be garbage collected because they are still referenced by a container thread. So put data in a threadlocal in a try / finally block and remove in the finally part.
这是一个显示一个方法来解决问题的帖子:
Here is a post showing one way to handle the problem: ThreadLocal in web applications
对于第二个问题,由于数据是threadlocal,它不会带有远程调用,您必须向接口添加参数,在一侧提取threadlocal数据,并在另一侧重新创建它...
For the second question as data is threadlocal it will not come with a remote call, you have to add a parameter to your interfaces, extract threadlocal data on one side and recreate it on the other side...
这篇关于ThreadLocal在企业应用中的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!