在执行 jndi 查找时,如何找出应该使用什么 SerialContextProvider?
今天我遇到了这个异常,
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext [Root exception is java.lang.NullPointerException]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
... 23 more
我试图远程访问一个 ejb,有问题的代码是
Context c = new InitialContext();
我之前见过这个异常并修复了它,但不记得我是如何做到的。我知道我必须为初始、上下文 url 和服务提供商或类似的东西设置一些环境变量。
事实上,我设法找到了上次用来解决这个问题的代码,如下所示。
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
Context c = new InitialContext(env);
我的问题是,如何找出要使用的初始上下文工厂?我已经为在 glassfish v3 上运行的数据库编写了一个 ejb 模块,但我没有得到任何提示,我当然应该使用 com.sun.enterprise.naming.SerialInitContextFactory,我的意思是就像它如此明显一样。谁制造了这些上下文工厂?谁决定我必须使用哪一个以及为什么?为什么没有一个列表来显示不同目的需要哪一个?似乎有人不遗余力地创造了最难以理解和最神秘的方法来访问人类可能实现的资源。或者我完全误解了这里的某些内容或者缺乏大量知识。
我非常感谢有关这个主题的一些启发。
谢谢大家。
Today I was presented with this exception
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext [Root exception is java.lang.NullPointerException]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
... 23 more
I was trying to remotely access an ejb, the offending code was
Context c = new InitialContext();
Ive seen this exception before and fixed it but couldn't remember exactly how I did it. I knew that I had to set some environment variables for the initial ,context url and service provider or some such stuff.
In fact I managed to find the code I used to fix this issue last time i had it, it is as follows.
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
Context c = new InitialContext(env);
My question is this how can you find out what initial context factory to use? I've written an ejb module for our database that runs on glassfish v3, at no point did I get any hint that of course I should use the com.sun.enterprise.naming.SerialInitContextFactory
, I mean like its so obvious. Who makes these context factories? Who decides which one I have to use and why? Why isn't there a list showing which one is required for different purposes? It seems like someone's gone out of their way to make the most impenetrable and cryptic method of accessing a resource that is humanly possible. Or I have completely misunderstood something here or am lacking a huge chunk of knowledge.
I would very much appreciate some enlightenment on this subject.
Thank you all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jndi.properties
的 glassfish 设置供参考:1.) 当调用默认的
InitialContext
构造函数时,会加载jndi.properties
文件。在我看来,这比对这些值进行硬编码、创建Properties
对象等更可取。2.) 这些连接参数适用于本地运行的 Glassfish。我从各种来源拼凑了这个。
3.) 我同意以下问题的精神:这些内容在精美手册中的何处指定?我在手册中看到过一些关于它们的提及,但它们并不都集中在一个位置——至少对于玻璃鱼来说是这样。 4.x glassfish 手册仅以 PDF 形式提供,这也无济于事。
glassfish settings for
jndi.properties
for reference:1.) The
jndi.properties
file is loaded when the defaultInitialContext
constructor is invoked. To my mind, this is preferable from hard-coding these values, creating aProperties
object, etc.2.) These connection parameters work for me with Glassfish running locally. I cobbled this together from various sources.
3.) I concur with the spirit the question: where in the fine manual are these specified? I've seen some mention of them in the manual, but they're not all in one location -- at least for glassfish. It doesn't help that the 4.x glassfish manual is only available via PDF.
通常,JNDI 在类路径中的 jndi.properties 文件中查找其配置。
也许有一个流氓 jndi.properties 文件误导了您。
有关更多详细信息,请参阅 https://glassfish.dev.java.net/ javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB
Normally JNDI looks for its configuration in a jndi.properties file in the class path.
Meybe there is a rogue jndi.properties file misdirecting you.
For more details see https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB
检查 jndiContext.lookup(???) 是否指定正确。
Chk if the jndiContext.lookup(???) is specified correctly.