java:comp/env 解释
关于获取数据源的语法,大体有(javax.sql.DataSource) ctx.lookup ("java:comp/env/XXX")和(javax.sql.DataSource) ctx.lookup("XXX")两种写法,好多人以为这两种写法是相同的,以为都是通过JNDI来获取数据源。其实java:comp/env和 JNDI是不同的,java:comp/env 是环境命名上下文(environment naming context(ENC)),是在EJB规范1.1以后引入的,引入这个是为了解决原来JNDI查找所引起的冲突问题,也是为了提高EJB或者J2EE应用的移植性。ENC是一个引用,引用是用于定位企业应用程序的外部资源的逻辑名。引用是在应用程序部署描述符文件中定义的。在部署时,引用被绑定到目标可操作环境中资源的物理位置(JNDI名)。使用ENC是把对其它资源的JNDI查找的硬编码解脱出来,通过配置这个引用可以在不修改代码的情况下,将引用指向不同的EJB(JNDI)。 在J2EE中的引用常用的有:---------JDBC 数据源引用在java:comp/env/jdbc 子上下文中声明
---------JMS 连接工厂在java:comp/env/jms 子上下文中声明
---------JavaMail 连接工厂在java:comp/env/mail 子上下文中声明
---------URL 连接工厂在 java:comp/env/url子上下文中声明
假如你写了一个EJB,获取datasource如:dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/DBPool");
那么在配置文件中进行资源映射时,在ejb-jar.xml中,
<resource-ref>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在weblogic-ejb-jar.xml中,
<reference-descriptor>
<resource-description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<jndi-name>OraDataSource</jndi-name>
</resource-description>
</reference-descriptor>
//转者注:如果是在jboss则在jboss.xml中做如下修改
<resource-managers>
<resource-manager>
<res-name>jdbc/DBPool</res-name>
<res-jndi-name>OraDataSource</res-jndi-name>
</resource-manager>
</resource-managers>
实际服务器中的JNDI名字是OraDataSource,逻辑名jdbc/DBPool只是用来和它作映射的,这样做的好处是为了提高可移植性,移植的时候只需要把配置文件改一下就可以,而应用程序可不用改动。
假如你写了一个一般的应用程序,想直接通过JNDI来获取数据源,那么直接lookup(“mytest”)就可以了(假如服务器上的JNDI为mytest),用第一种写法反而会报错的。
转自:http://www.blogjava.net/javababy/archive/2006/12/06/85881.html
----------------------------------------------------------------------------------------------------------------------------------------------
ENC的概念:
The application component environment is referred to as the ENC, the enterprise naming context.
应用组件的业务逻辑应该是ENC中取得对象。组件提供者使用标准的部署描述符指定必需的ENC入口。这些ENC入口是运行时组件所依赖的资源等信息。
一个应用组件实例使用JNDI定位ENC。ENC的标准JNDI CONTEXT是:java;/comp/env
Java代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]// Obtain the application component's ENC
[*]Context iniCtx =newInitialContext();
[*]Context compEnv = (Context) iniCtx.lookup( "java:comp/env" );
view plaincopy
[*]// Obtain the application component's ENC
[*]Context iniCtx = new InitialContext();
[*]Context compEnv = (Context) iniCtx.lookup("java:comp/env");
env环境是一个私有的环境,只能在该组件内部存取。对其它组件是不可见的。比如,EJB1不能存取EJB2的ENV。同样,任何一个客户端代码,不管它是运行在应用服务器的同一个JVM内或者是远程调用,均不能访问这些JNDI。这样的话,组件之间是相互隔离的,不同组件之间可以定义它自己的环境参数。比如EJB1可以定义自己的环境变量参数:java:comp/env/red
1、JBOSS的命名空间:
比如一个命名: java:comp/env 凡是绑定在java: 名字下面的子环境,均只能在JBOSS 服务器内部才能进行访问。无法进行远程访问,比如DataSource 均被绑定在 java:下面。不能远程访问。而EJB,JTA均被绑定在全局范围之内,能进行远程访问。
一些惯用的ENV:
ENV 用env-entry 进行申明。
ejb引用用 ejb-ref,ejb-local-ref 进行申明。
资源管理连接工厂用resource-ref 进行申明。
资源环境变量引用用resource-env-ref进行申明。
1.1 Environment Entries
例子:
Xml代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]< session >
[*] < ejb-name > ASessionBean </ ejb-name >
[*] <!-- ... -->
[*] < env-entry >
[*] < description > The maximum number of tax exemptions allowed</ description >
[*] < env-entry-name > maxExemptions </ env-entry-name >
[*] < env-entry-type > java.lang.Integer </ env-entry-type >
[*] < env-entry-value > 15 </ env-entry-value >
[*] </ env-entry >
[*] < env-entry >
[*] < description > The tax rate</ description >
[*] < env-entry-name > taxRate </ env-entry-name >
[*] < env-entry-type > java.lang.Float </ env-entry-type >
[*] < env-entry-value > 0.23 </ env-entry-value >
[*] </ env-entry >
[*]</ session >
view plaincopy
[*]<session>
[*] <ejb-name>ASessionBean</ejb-name>
[*] <!-- ... -->
[*] <env-entry>
[*] <description>The maximum number of tax exemptions allowed </description>
[*] <env-entry-name>maxExemptions</env-entry-name>
[*] <env-entry-type>java.lang.Integer</env-entry-type>
[*] <env-entry-value>15</env-entry-value>
[*] </env-entry>
[*] <env-entry>
[*] <description>The tax rate </description>
[*] <env-entry-name>taxRate</env-entry-name>
[*] <env-entry-type>java.lang.Float</env-entry-type>
[*] <env-entry-value>0.23</env-entry-value>
[*] </env-entry>
[*]</session>
ENC env-entry access code fragment
Java代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]InitialContext iniCtx =newInitialContext();
[*]Context envCtx = (Context) iniCtx.lookup( "java:comp/env" );
[*]Integer maxExemptions = (Integer) envCtx.lookup( "maxExemptions" );
[*]Float taxRate = (Float) envCtx.lookup( "taxRate" );
view plaincopy
[*]InitialContext iniCtx = new InitialContext();
[*]Context envCtx = (Context) iniCtx.lookup("java:comp/env");
[*]Integer maxExemptions = (Integer) envCtx.lookup("maxExemptions");
[*]Float taxRate = (Float) envCtx.lookup("taxRate");
1.2 EJB Reference
在开发过程,有时候需在EJB之间进行相互引用。这个引用一般均要根据JNDI名字才能进行,但JNDI名字是在部署时才能决定。因此需要一种手段,引用在部署时的另一个EJB,ejb-reference就是为了满足这种要求。
ejb reference 是一个在应用组件命名环境下指向一个部署时EJB HOME的链接。J2EE规范推荐所有的此种链接应接被组织在java:/comp/env/ejb命名空间下。
Xml代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]< session >
[*] < ejb-name > ShoppingCartBean </ ejb-name >
[*] <!-- ...-->
[*]</ session >
[*]
[*]< session >
[*] < ejb-name > ProductBeanUser </ ejb-name >
[*] <!--...-->
[*] < ejb-ref >
[*] < description > This is a reference to the store products entity</ description >此属性可选
[*] < ejb-ref-name > ejb/ProductHome </ ejb-ref-name >
[*] < ejb-ref-type > Entity </ ejb-ref-type > 只能选Entity与Session
[*] < home > org.jboss.store.ejb.ProductHome </ home >
[*] </ ejb-ref >
[*] < remote >org.jboss.store.ejb.Product </ remote >
[*]</ session >
[*]
[*]< session >
[*] < ejb-name > ShoppingCartUser </ ejb-name >
[*] <!--...-->
[*] < ejb-ref >
[*] < ejb-ref-name > ejb/ShoppingCartHome </ ejb-ref-name >
[*] < ejb-ref-type > Session </ ejb-ref-type >
[*] < home > org.jboss.store.ejb.ShoppingCartHome </ home >
[*] < remote >org.jboss.store.ejb.ShoppingCart </ remote >
[*] < ejb-link > ShoppingCartBean </ ejb-link >
[*] <!--指向同一个EJB JAR 或 同一个 APPLICATION UNIT中的一个EJB,且是该 EJB的名字。-->
[*] </ ejb-ref >
[*]</ session >
[*]
[*]< entity >
[*] < description > The Product entity bean</ description >
[*] < ejb-name > ProductBean </ ejb-name >
[*] <!--...-->
[*]</ entity >
view plaincopy
[*]<session>
[*] <ejb-name>ShoppingCartBean</ejb-name>
[*] <!-- ...-->
[*]</session>
[*]
[*]<session>
[*] <ejb-name>ProductBeanUser</ejb-name>
[*] <!--...-->
[*] <ejb-ref>
[*] <description>This is a reference to the store products entity </description> 此属性可选
[*] <ejb-ref-name>ejb/ProductHome</ejb-ref-name>
[*] <ejb-ref-type>Entity</ejb-ref-type>只能选Entity与Session
[*] <home>org.jboss.store.ejb.ProductHome</home>
[*] </ejb-ref>
[*] <remote> org.jboss.store.ejb.Product</remote>
[*]</session>
[*]
[*]<session>
[*] <ejb-name>ShoppingCartUser</ejb-name>
[*] <!--...-->
[*] <ejb-ref>
[*] <ejb-ref-name>ejb/ShoppingCartHome</ejb-ref-name>
[*] <ejb-ref-type>Session</ejb-ref-type>
[*] <home>org.jboss.store.ejb.ShoppingCartHome</home>
[*] <remote> org.jboss.store.ejb.ShoppingCart</remote>
[*] <ejb-link>ShoppingCartBean</ejb-link>
[*] <!--指向同一个EJB JAR 或 同一个 APPLICATION UNIT中的一个EJB,且是该 EJB的名字。-->
[*] </ejb-ref>
[*]</session>
[*]
[*]<entity>
[*] <description>The Product entity bean </description>
[*] <ejb-name>ProductBean</ejb-name>
[*] <!--...-->
[*]</entity>
ejb-ref的范围是属于申明此段属性的EJB组件。这就意味着其它的应用组件在运行时不能访问这些属性。其它组件定义相同的名字也不会引用名称冲突。
Java代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]InitialContext iniCtx =newInitialContext();
[*]Context ejbCtx = (Context) iniCtx.lookup( "java:comp/env/ejb" );
[*]ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup( "ShoppingCartHome" );
view plaincopy
[*]InitialContext iniCtx = new InitialContext();
[*]Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
[*]ShoppingCartHome home = (ShoppingCartHome) ejbCtx.lookup("ShoppingCartHome");
JBOSS的引用方法:
Xml代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]< session >
[*] < ejb-name > ProductBeanUser </ ejb-name >
[*] < ejb-ref >
[*] < ejb-ref-name > ejb/ProductHome </ ejb-ref-name >对应于ejb-jar.xml文件中的ejb-ref-name元素
[*] < jndi-name > jboss/store/ProductHome </ jndi-name >
[*] </ ejb-ref >
[*]</ session >
[*]
[*]< entity >
[*] < ejb-name > ProductBean </ ejb-name >
[*] < jndi-name > jboss/store/ProductHome </ jndi-name >
[*] <!-- ... -->
[*]</ entity >
view plaincopy
[*]<session>
[*] <ejb-name>ProductBeanUser</ejb-name>
[*] <ejb-ref>
[*] <ejb-ref-name>ejb/ProductHome</ejb-ref-name> 对应于ejb-jar.xml文件中的ejb-ref-name元素
[*] <jndi-name>jboss/store/ProductHome</jndi-name>
[*] </ejb-ref>
[*]</session>
[*]
[*]<entity>
[*] <ejb-name>ProductBean</ejb-name>
[*] <jndi-name>jboss/store/ProductHome</jndi-name>
[*] <!-- ... -->
[*]</entity>
从上面可以看出,如果ejb/ProductHome的引用被重新定向给了jndi-name : jboss/store/ProductHome
1.3 EJB Local Reference
EJB 2.0 增加了 ejb local reference , 从而从语义学的角度保证该引用为本地引用,而不是通过RMI的方式。
Xml代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]< session >
[*] < ejb-name > Probe </ ejb-name >
[*] < home > org.jboss.test.perf.interfaces.ProbeHome </ home >
[*] < remote > org.jboss.test.perf.interfaces.Probe </ remote >
[*] < local-home > org.jboss.test.perf.interfaces.ProbeLocalHome </ local-home >
[*] < local > org.jboss.test.perf.interfaces.ProbeLocal </ local >
[*] < ejb-class > org.jboss.test.perf.ejb.ProbeBean </ ejb-class >
[*] < session-type > Stateless </ session-type >
[*] < transaction-type > Bean </ transaction-type >
[*]</ session >
[*]< session >
[*] < ejb-name > PerfTestSession </ ejb-name >
[*] < home > org.jboss.test.perf.interfaces.PerfTestSessionHome </ home >
[*] < remote > org.jboss.test.perf.interfaces.PerfTestSession </ remote >
[*] < ejb-class > org.jboss.test.perf.ejb.PerfTestSessionBean </ ejb-class >
[*] < session-type > Stateless </ session-type >
[*] < transaction-type > Container </ transaction-type >
[*] < ejb-ref >
[*] < ejb-ref-name > ejb/ProbeHome </ ejb-ref-name >
[*] < ejb-ref-type > Session </ ejb-ref-type >
[*] < home > org.jboss.test.perf.interfaces.SessionHome </ home >
[*] < remote > org.jboss.test.perf.interfaces.Session </ remote >
[*] < ejb-link > Probe </ ejb-link >
[*] </ ejb-ref >
[*] < ejb-local-ref >
[*] < ejb-ref-name > ejb/ProbeLocalHome </ ejb-ref-name >
[*] < ejb-ref-type > Session </ ejb-ref-type >
[*] < local-home > org.jboss.test.perf.interfaces.ProbeLocalHome </ local-home >
[*] < local > org.jboss.test.perf.interfaces.ProbeLocal </ local >
[*] <!--EJB local interface 的全限定名-->
[*] < ejb-link > Probe </ ejb-link >
[*] </ ejb-local-ref >
[*]</ session >
view plaincopy
[*]<session>
[*] <ejb-name>Probe</ejb-name>
[*] <home>org.jboss.test.perf.interfaces.ProbeHome</home>
[*] <remote>org.jboss.test.perf.interfaces.Probe</remote>
[*] <local-home>org.jboss.test.perf.interfaces.ProbeLocalHome</local-home>
[*] <local>org.jboss.test.perf.interfaces.ProbeLocal</local>
[*] <ejb-class>org.jboss.test.perf.ejb.ProbeBean</ejb-class>
[*] <session-type>Stateless</session-type>
[*] <transaction-type>Bean</transaction-type>
[*] </session>
[*] <session>
[*] <ejb-name>PerfTestSession</ejb-name>
[*] <home>org.jboss.test.perf.interfaces.PerfTestSessionHome</home>
[*] <remote>org.jboss.test.perf.interfaces.PerfTestSession</remote>
[*] <ejb-class>org.jboss.test.perf.ejb.PerfTestSessionBean</ejb-class>
[*] <session-type>Stateless</session-type>
[*] <transaction-type>Container</transaction-type>
[*] <ejb-ref>
[*] <ejb-ref-name>ejb/ProbeHome</ejb-ref-name>
[*] <ejb-ref-type>Session</ejb-ref-type>
[*] <home>org.jboss.test.perf.interfaces.SessionHome</home>
[*] <remote>org.jboss.test.perf.interfaces.Session</remote>
[*] <ejb-link>Probe</ejb-link>
[*] </ejb-ref>
[*] <ejb-local-ref>
[*] <ejb-ref-name>ejb/ProbeLocalHome</ejb-ref-name>
[*] <ejb-ref-type>Session</ejb-ref-type>
[*] <local-home>org.jboss.test.perf.interfaces.ProbeLocalHome</local-home>
[*] <local>org.jboss.test.perf.interfaces.ProbeLocal</local>
[*] <!--EJB local interface 的全限定名-->
[*] <ejb-link>Probe</ejb-link>
[*] </ejb-local-ref>
[*] </session>
存取的语法:
Java代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]InitialContext iniCtx =newInitialContext();
[*]Context ejbCtx = (Context) iniCtx.lookup( "java:comp/env/ejb" );
[*]ProbeLocalHome home = (ProbeLocalHome) ejbCtx.lookup( "ProbeLocalHome" );
view plaincopy
[*]InitialContext iniCtx = new InitialContext();
[*]Context ejbCtx = (Context) iniCtx.lookup("java:comp/env/ejb");
[*]ProbeLocalHome home = (ProbeLocalHome) ejbCtx.lookup("ProbeLocalHome");
1.4 Resource manaager connection factory reference
由一系列的resource-ref 组成。每个引用以下几个子节点:
description (optional)
res-ref-name : java:/comp/env 下面的相关的名字
res-type : 资源的类型,资源管理器连接工厂的全限定名称。
res-auth : 资源需要的权限管理。 分两种:Application或 container
res-share-scope (optional) : 目前JBOSS不支持。
J2EE规范推荐所有资源引用都组织在java:/comp/env 名称空间之下,并且每类资源有自己的子命名空间。
比如:JDBC DataSource Reference should be declared in the java:/comp/env/jdbc subContext.
JMS connection factories should be declared int the java:/comp/env/jms subContext.
一个在WEB.XML文件中的引用例子:
Xml代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]< web >
[*] <!-- ... -->
[*] < servlet >
[*] < servlet-name > AServlet </ servlet-name >
[*] <!-- ... -->
[*] </ servlet >
[*] <!-- ... -->
[*] <!-- JDBC DataSources (java:comp/env/jdbc) -->
[*] < resource-ref >
[*] < description > The default DS </ description >
[*] < res-ref-name > jdbc/DefaultDS </ res-ref-name > <!--该名称必须与下面的描述文件中的一致-->
[*] < res-type > javax.sql.DataSource </ res-type >
[*] < res-auth > Container </ res-auth >
[*] </ resource-ref >
[*] <!-- JavaMail Connection Factories (java:comp/env/mail) -->
[*] < resource-ref >
[*] < description > Default Mail </ description >
[*] < res-ref-name > mail/DefaultMail </ res-ref-name >
[*] < res-type > javax.mail.Session </ res-type >
[*] < res-auth > Container </ res-auth >
[*] </ resource-ref >
[*] <!-- JMS Connection Factories (java:comp/env/jms) -->
[*] < resource-ref >
[*] < description > Default QueueFactory </ description >
[*] < res-ref-name > jms/QueueFactory </ res-ref-name >
[*] < res-type > javax.jms.QueueConnectionFactory </ res-type >
[*] < res-auth > Container </ res-auth >
[*] </ resource-re >
view plaincopy
[*]<web>
[*] <!-- ... -->
[*] <servlet>
[*] <servlet-name>AServlet</servlet-name>
[*] <!-- ... -->
[*] </servlet>
[*] <!-- ... -->
[*] <!-- JDBC DataSources (java:comp/env/jdbc) -->
[*] <resource-ref>
[*] <description>The default DS</description>
[*] <res-ref-name>jdbc/DefaultDS</res-ref-name> <!--该名称必须与下面的描述文件中的一致-->
[*] <res-type>javax.sql.DataSource</res-type>
[*] <res-auth>Container</res-auth>
[*] </resource-ref>
[*] <!-- JavaMail Connection Factories (java:comp/env/mail) -->
[*] <resource-ref>
[*] <description>Default Mail</description>
[*] <res-ref-name>mail/DefaultMail</res-ref-name>
[*] <res-type>javax.mail.Session</res-type>
[*] <res-auth>Container</res-auth>
[*] </resource-ref>
[*] <!-- JMS Connection Factories (java:comp/env/jms) -->
[*] <resource-ref>
[*] <description>Default QueueFactory</description>
[*] <res-ref-name>jms/QueueFactory</res-ref-name>
[*] <res-type>javax.jms.QueueConnectionFactory</res-type>
[*] <res-auth>Container</res-auth>
[*] </resource-re>
程序中进行引用:
Java代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]Context initCtx =newInitialContext();
[*]javax.mail.Session s = (javax.mail.Session)
[*]initCtx.lookup( "java:comp/env/mail/DefaultMail" );
view plaincopy
[*]Context initCtx = new InitialContext();
[*]javax.mail.Session s = (javax.mail.Session)
[*]initCtx.lookup("java:comp/env/mail/DefaultMail");
jboss-web.xml(对应于WEB),jboss.xml(对应于ejb) 文件提供res-ref-name对JNDI名字的转换。
这是通过在jboss.xml 或 jboss-web.xml 文件中提供 resource-ref 完成的。
比如:
Xml代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]< jboss-web >
[*] <!-- ... -->
[*] < resource-ref >
[*] < res-ref-name > jdbc/DefaultDS </ res-ref-name >
[*] < res-type > javax.sql.DataSource </ res-type >
[*] < jndi-name > java:/DefaultDS </ jndi-name > <!--将资源引用重定向到JNDI上面-->
[*] </ 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 >
[*] < resource-ref >
[*] < res-ref-name > jms/QueueFactory </ res-ref-name >
[*] < res-type > javax.jms.QueueConnectionFactory </ res-type >
[*] < jndi-name > QueueConnectionFactory </ jndi-name >
[*] </ resource-ref >
[*] <!-- ... -->
[*]</ jboss-web >
view plaincopy
[*]<jboss-web>
[*] <!-- ... -->
[*] <resource-ref>
[*] <res-ref-name>jdbc/DefaultDS</res-ref-name>
[*] <res-type>javax.sql.DataSource</res-type>
[*] <jndi-name>java:/DefaultDS</jndi-name><!--将资源引用重定向到JNDI上面-->
[*] </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>
[*] <resource-ref>
[*] <res-ref-name>jms/QueueFactory</res-ref-name>
[*] <res-type>javax.jms.QueueConnectionFactory</res-type>
[*] <jndi-name>QueueConnectionFactory</jndi-name>
[*] </resource-ref>
[*] <!-- ... -->
[*]</jboss-web>
Resource Environment References 提供了 resource 本身需要使用的资源。
Xml代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]< session >
[*] < ejb-name > MyBean </ ejb-name >
[*]
[*] < resource-env-ref >
[*] < description > This is a reference to a JMS queue used in the
[*] processing of Stock info
[*] </ description >
[*] < resource-env-ref-name > jms/StockInfo </ resource-env-ref-name >
[*] < resource-env-ref-type > javax.jms.Queue </ resource-env-ref-type >
[*] </ resource-env-ref >
[*] <!-- ... -->
[*]</ session >
view plaincopy
[*]<session>
[*] <ejb-name>MyBean</ejb-name>
[*]
[*] <resource-env-ref>
[*] <description>This is a reference to a JMS queue used in the
[*] processing of Stock info
[*] </description>
[*] <resource-env-ref-name>jms/StockInfo</resource-env-ref-name>
[*] <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
[*] </resource-env-ref>
[*] <!-- ... -->
[*]</session>
在程序中访问:
Java代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]InitialContext iniCtx =newInitialContext();
[*]javax.jms.Queue q = (javax.jms.Queue)
[*]envCtx.lookup( "java:comp/env/jms/StockInfo" );
view plaincopy
[*]InitialContext iniCtx = new InitialContext();
[*]javax.jms.Queue q = (javax.jms.Queue)
[*]envCtx.lookup("java:comp/env/jms/StockInfo");
同样道理,特定平台仍需提供 resource-env-ref-name 至 JNDI 的转换,比如在JBOSS.xml文件中:
Xml代码 http://blog.csdn.net/drykilllogic/article/details/images/icon_copy.gif
[*]< session >
[*] < ejb-name > MyBean </ ejb-name >
[*] < resource-env-ref >
[*] < resource-env-ref-name > jms/StockInfo </ resource-env-ref-name >
[*] < jndi-name > queue/StockInfoQueue </ jndi-name > 此为真正的全局JNDI名字
[*] </ resource-env-ref >
[*] <!-- ... -->
[*]</ session >
view plaincopy
[*]<session>
[*] <ejb-name>MyBean</ejb-name>
[*] <resource-env-ref>
[*] <resource-env-ref-name>jms/StockInfo</resource-env-ref-name>
[*] <jndi-name>queue/StockInfoQueue</jndi-name> 此为真正的全局JNDI名字
[*] </resource-env-ref>
[*] <!-- ... -->
[*]</session>
转自:http://lym6520.javaeye.com/blog/316169
页:
[1]