gpt4 book ai didi

java - Jetty 9 邮件 session 资源 JNDI 查找时出现 NameNotfoundException

转载 作者:行者123 更新时间:2023-12-01 12:00:02 24 4
gpt4 key购买 nike

我正在使用 Jersey 编写 REST API 后端 servlet,并且在使用 jndi 配置邮件发送时遇到一些问题。

这是我的 WEB-INF/jetty-env.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
<New id="mail" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid="wac"/></Arg>
<Arg>mail/Session</Arg>
<Arg>
<New class="org.eclipse.jetty.jndi.factories.MailSessionReference">
<Set name="user">MY_USER</Set>
<Set name="password">MY_PASS</Set>
<Set name="properties">
<New class="java.util.Properties">
<Put name="mail.smtp.host">MY_HOST</Put>
<Put name="mail.from">MY_SENDER</Put>
<Put name="mail.debug">true</Put>
</New>
</Set>
</New>
</Arg>
</New>
</Configure>

这是我尝试查找它的方法:

Session session = (Session) new InitialContext().lookup("java:comp/env/mail/Session");

但是,我得到:

 javax.naming.NameNotFoundException; remaining name 'mail/Session'
at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:487)

来自“service jetty restart”后的 DEBUG 日志:

20-01-2015 10:10:39 DEBUG jndi:183 - SAVE mail/Session in o.e.j.w.WebAppContext@4b9e13df{/finalcountdown.backend,file:/tmp/jetty-0.0.0.0-8085-finalcountdown.backend.war-_finalcountdown.backend-any-2063340985085206547.dir/webapp/,STARTING}{/finalcountdown.backend.war}
20-01-2015 10:10:39 DEBUG jndi:79 - InitialContextFactory.getInitialContext()
20-01-2015 10:10:39 DEBUG jndi:82 - Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@6b9651f3
20-01-2015 10:10:39 DEBUG jndi:79 - InitialContextFactory.getInitialContext()
20-01-2015 10:10:39 DEBUG jndi:82 - Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@23d2a7e8
20-01-2015 10:10:39 DEBUG jndi:411 - Looking up name="org.eclipse.jetty.webapp.WebAppContext@4b9e13df"
20-01-2015 10:10:39 DEBUG jndi:137 - supportDeepBinding=false
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=org.eclipse.jetty.webapp.WebAppContext@4b9e13df obj=org.eclipse.jetty.jndi.NamingContext@4abdb505[name=org.eclipse.jetty.webapp.WebAppContext@4b9e13df,parent=org.eclipse.jetty.jndi.local.localContextRoot$NamingRoot@7ce6a65d,bindings.size=0] for context=null as org.eclipse.jetty.webapp.WebAppContext@4b9e13df: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@4abdb505[name=org.eclipse.jetty.webapp.WebAppContext@4b9e13df,parent=org.eclipse.jetty.jndi.local.localContextRoot$NamingRoot@7ce6a65d,bindings.size=0]
20-01-2015 10:10:39 DEBUG jndi:82 - Subcontext org.eclipse.jetty.webapp.WebAppContext@4b9e13df created
20-01-2015 10:10:39 DEBUG jndi:464 - Looking up name="__"
20-01-2015 10:10:39 DEBUG jndi:137 - supportDeepBinding=false
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=__ obj=org.eclipse.jetty.jndi.NamingContext@1500955a[name=__,parent=org.eclipse.jetty.jndi.NamingContext@4abdb505,bindings.size=0] for context=org.eclipse.jetty.webapp.WebAppContext@4b9e13df as __: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@1500955a[name=__,parent=org.eclipse.jetty.jndi.NamingContext@4abdb505,bindings.size=0]
20-01-2015 10:10:39 DEBUG jndi:82 - Subcontext __ created
20-01-2015 10:10:39 DEBUG jndi:464 - Looking up name="mail"
20-01-2015 10:10:39 DEBUG jndi:137 - supportDeepBinding=false
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=mail obj=org.eclipse.jetty.jndi.NamingContext@e874448[name=mail,parent=org.eclipse.jetty.jndi.NamingContext@1500955a,bindings.size=0] for context=__ as mail: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@e874448[name=mail,parent=org.eclipse.jetty.jndi.NamingContext@1500955a,bindings.size=0]
20-01-2015 10:10:39 DEBUG jndi:82 - Subcontext mail created
20-01-2015 10:10:39 DEBUG jndi:1327 - Removing binding with key=Session
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=Session obj=mail/Session for context=mail as Session: org.eclipse.jetty.plus.jndi.Resource:mail/Session
20-01-2015 10:10:39 DEBUG jndi:88 - Bound object to Session
20-01-2015 10:10:39 DEBUG jndi:411 - Looking up name="org.eclipse.jetty.webapp.WebAppContext@4b9e13df"
20-01-2015 10:10:39 DEBUG jndi:76 - Subcontext org.eclipse.jetty.webapp.WebAppContext@4b9e13df already exists
20-01-2015 10:10:39 DEBUG jndi:464 - Looking up name="mail"
20-01-2015 10:10:39 DEBUG jndi:137 - supportDeepBinding=false
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=mail obj=org.eclipse.jetty.jndi.NamingContext@29b5cd00[name=mail,parent=org.eclipse.jetty.jndi.NamingContext@4abdb505,bindings.size=0] for context=org.eclipse.jetty.webapp.WebAppContext@4b9e13df as mail: org.eclipse.jetty.jndi.NamingContext:org.eclipse.jetty.jndi.NamingContext@29b5cd00[name=mail,parent=org.eclipse.jetty.jndi.NamingContext@4abdb505,bindings.size=0]
20-01-2015 10:10:39 DEBUG jndi:82 - Subcontext mail created
20-01-2015 10:10:39 DEBUG jndi:1327 - Removing binding with key=Session
20-01-2015 10:10:39 DEBUG jndi:1280 - Adding binding with key=Session obj=Reference Class Name: javax.mail.Session

所以应该绑定(bind)。我在这里缺少什么?

最佳答案

有 2 件事需要您解决。

首先,您的 DOCTYPE 不好。

应该是

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

其次,您似乎缺少对 JNDI 对象的强制 WEB-INF/web.xml 引用。

添加此:

<resource-ref>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>

这应该会让你的环境变得愉快

另请注意,要在 Jetty 9.2+ 上运行,您需要在 ${jetty.base 中启用 annotationsjndi 模块}/start.ini

关于java - Jetty 9 邮件 session 资源 JNDI 查找时出现 NameNotfoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com