`
chainhou
  • 浏览: 172403 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

resource-ref与resource-env-ref的区别

阅读更多

最近工作中处理一个对于资源引用的问题,主要问题是在resource-ref引用资源可以成功,换成resource-env-ref引用资源就会导致Naming Not Found 。。。 于是就开始Google 这两者的不同,到底是什么原因导致问题出现呢?跟了几遍代码,发现是在处理过程中对于resource-env-ref的定义是

 

Element 

Required 

Description 

resource-env-ref-name

only one 

Specifies the res-ref-name in the corresponding Java EE deployment descriptor fileresource-env-ref entry.

jndi-name

only one 

Specifies the absolute jndi-name of a resource.

 

 

但在Runtime Descriptor(sun-web.xml)中写成了res-env-ref-name,相应的这个标签是没有定义的,所以保存在服务器的动态属性中的是以res-env-ref-name为key来保存,

 

Dynamic Attribute

 

  Property name = JndiName value = myjndi

 

  Property name = res-env-ref-name value = aaa

<==End]

 

而不是定义的RESENVREFNAME,所以调用其相应的get方法会返回空,导致相应的关联没有创建。

问题分析完了,通过更正相应配置文件中的属性名来解决。

但是这两者之间的区别到底是什么呢,还是不清楚。于是又Google了几次,大概明白了。

 

区别主要是:

 

- Resource reference, which is typically to an object factory for resources such as a JDBC DataSource, a JavaMail Session, or custom object factories configured into Tomcat. -

 

Resource environment reference, a new variation of resource-ref added in Servlet 2.4 that is simpler to configure for resources that do not require authentication information.

resource-ref 连接工厂管理对象:表示连接工厂的资源。该连接通常在驱动程序和池提供的容器管理的实施。目前,有5个标准的Java EE运行时连接工厂:

 

Resource Manager Connection Factory Type JNDI Contextpfad
JDBC javax.sql.DataSource java:comp/env/jdbc/[pfad]
JMS javax.jms.TopicConnectionFactory, javax.jms.QueueConnectionFactory java:comp/env/jms/[pfad]
JavaMail javax.mail.Session java:comp/env/mail/[pfad]
URL java.net.URL java:comp/env/url/[pfad]
JAXR ResourceAdapter javax.xml.registry.ConnectionFactory java:comp/env/eis/JAXR/[pfad]
JCA Outbound Resource Adapter a.b.c.MyConnectionFactory

java:comp/env/eis/[pfad]

 

 

 

 

 

 

web.xml样例:

<resource-ref>
  <res-ref-name>jdbc/petShopDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

 

应用服务器Runtime Descriptor样例:
<resource-ref>
        <res-ref-name>jdbc/DefaultDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <jndi-name>java:/DefaultDS</jndi-name>
    </resource-ref>
    <resource-ref>
        <res-ref-name>mail/DefaultMail</res-ref-name>
        <res-type>javax.mail.Session</res-type>
        <jndi-name>java:/Mail</jndi-name>
    </resource-ref>

  这两个文件中的res-ref-name要一致

 

 

resource-env-ref 目标管理的对象:这些资源是直接寻址(无状态)。每个容器必须JMS队列和JMS主题作为管理对象提供的,默认情况下,他们下​​javacomp / env/ JMS提供。完成在Deploymentdescriptor声明resource-env-ref元素

web.xml中的内容把上面的resource-ref换成resource-env-ref即可,

<resource-env-ref>
  <resource-env-ref-name>jms/pets/stockQueue</resource-env-ref-name>
  <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
</resource-env-ref>

 

应用服务器描述文件:
<resource-env-ref>
        <resource-env-ref-name>jms/StockInfo</resource-env-ref-name>
        <jndi-name>queue/StockInfoQueue</jndi-name>
    </resource-env-ref>

 

以上是两者的区别与样例。

在StackOverFlow中对于每种资源应该使用哪种引用还有个列表:

Java Type                                              Equivalent Resource type

java.lang.String                                              env-entry
java.lang.Character                                           env-entry
java.lang.Integer                                             env-entry
java.lang.Boolean                                             env-entry
java.lang.Double                                              env-entry
java.lang.Byte                                                env-entry
java.lang.Short                                               env-entry
java.lang.Long                                                env-entry
java.lang.Float                                               env-entry
javax.xml.rpc.Service                                         service-ref
javax.xml.ws.Service                                          service-ref
javax.jws.WebService                                          service-ref
javax.sql.DataSource                                          resource-ref
javax.jms.ConnectionFactory                                   resource-ref
javax.jms.QueueConnectionFactory                              resource-ref
javax.jms.TopicConnectionFactory                              resource-ref
javax.mail.Session                                            resource-ref
java.net.URL                                                  resource-ref
javax.resource.cci.ConnectionFactory                          resource-ref
org.omg.CORBA_2_3.ORB                                         resource-ref
any other connection factory defined by a resource adapter    resource-ref
javax.jms.Queue                                               message-destination-ref
javax.jms.Topic                                               message-destination-ref
javax.resource.cci.InteractionSpec                            resource-env-ref
javax.transaction.UserTransaction                             resource-env-ref
Everything else                                               resource-env-ref

 

 以下是参考的几篇文件,都很不错:

http://stackoverflow.com/questions/7254277/jee6-what-can-be-injected-with-resource

https://access.redhat.com/knowledge/docs/en-US/JBoss_Enterprise_Web_Platform/5/html/Administration_And_Configuration_Guide/Naming_on_JBoss-J2EE_and_JNDI___The_Application_Component_Environment.html

http://www.prozesse-und-systeme.de/jndiResourcen.html

http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics