openfire 插件中使用javamail 发送邮件时有如下问题发生:
Access to default session denied
对这个问题的详细解释如下:
http://stackoverflow.com/questions/4184204/what-is-the-difference-between-getdefaultinstance-and-getinstance-in-session
What is the difference between Session.getDefaultInstance(props,
authenticator)
and getInstance(props,
authenticator)
? In general, when will you choose one over the other?
I also read Java doc on getDefaultInstance(props, authenticator), but still couldn't able to make out the difference distinctly/clearly.
Hope experts can help me in understanding this better.
UPDATE: Actual reason that triggered to ask this question is: We've used Session.getDefaultInstance()
method
in some places within our web-based application. Sometimes, it throws java.lang.SecurityException:
Access to default session denied
, on quick googling, it suggested to use Session.getInstance()
method
instead. Hence, when one would choose one over the other?
If you read the documentation, you will see that
getDefaultInstance Get the default Session object. If a default has not yet been setup, a new Session object is created and installed as the default.
Therefore, if one does not already exist, it call getInstance()
getInstance Get a new Session object.
So, a new session object is created, regardless of whether one already exists.
The
only I reason I can see to use defaultinstance, is that it prevents the need to create multiple instances needlessly of the session, but other than that there is no difference. If you are having trouble with permissions, then just use getInstance(), it shouldn't
cause you a problem. Your application however, should store this at a global level however, rather than create a new instance each time you need it.
Per
doc,
getDefaultInstance():
Get the default Session object. If a default has not yet been setup, a new Session object is created and installed as the default. , will subsequent calls to getDefaultInstance() just
not make use of the session that was created before?
@Gnanam,
yes, but it uses a shared instance across JVM's (reading between the lines in the documentation that says a session can be shared across the desktop). So, that is possibly why he is getting his error, that it is using a shared instance and accessing it with
the wrong credentials. So, to prevent the error, it is safer not to use getDefaultInstance().
A
very important side affect of using getDefaultInstance is that if the session is already created your properties you passed in will be ignored. The existing sessions properties will not be updated in any way, so if you're changing any properties you want to
be using getInstance.
Cause This error is raised in the getDefaultInstance method in javax.mail.Session.java. According to this source code, this error occures when the default session object is already initialized, but authenticator instance is renewed or changed, or the class loader of the default session object is different from the argument authentificator's. Maybe the java source code using the default session instance of the java mail is recompiled and reloaded, or duplicate javamail class libraries are included into the Classpath of the environment. it gives proper solution
与jvm、classloader有关,学艺不精,暂时不能解释
结论:
使用getInstance()替换getDefaultInstance()即可,不会有问题。
openfire插件在使用一些类似功能时都会有这种问题发生。
使用morphia也有,解决方案暂时定为:(1)自己实现一套mongo(2)不使用morphia。。待测试
|
Session.getDefaultInstance()
method in some places within our web-based application. Sometimes, it throwsjava.lang.SecurityException: Access to default session denied
, on quick googling, it suggested to useSession.getInstance()
method instead. And that's the reason I've asked here when one would choose one over the other?