⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch07s02.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
字号:
<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Declaring Resource Reference</title><link rel="stylesheet" href="styles.css" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/styles.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets Vimages/callouts/"><link rel="home" href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html" title="JBoss 3.0 Documentation"><link rel="up" href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html" title="Chapter 7. Advanced container configuration : use of jboss.xml"><link rel="previous" href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html" title="Chapter 7. Advanced container configuration : use of jboss.xml"><link rel="next" href="ch07s10.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s10.html" title="Specifying the deployment name of your beans"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="jboss.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jboss.gif" border="0"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch07s10.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s10.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table><div class="section"><a name="d0e2501"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="d0e2501"></a>Declaring Resource Reference</h2></div></div><p>Author:<span class="author">Vincent Harcq</span>
		<tt>&lt;<a href="mailto:vincent.harcq@hubmethods.com">vincent.harcq@hubmethods.com</a>&gt;</tt>
	</p><div class="section"><a name="d0e2515"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2515"></a>Introduction</h3></div></div><p>Resources are mainly used to access SQL DataSource object for use in BMP (Bean Managed Persistence)
entity beans or session beans.  They can also be used to access other type of resources like JavaMail Session
for example.</p><p>The EJB specification defines how to define deployment descriptor entries to define such Resources.
JBoss defines an entry in jboss.xml for that purpose that can be seen as an indirection of what is done in 
ejb-jar.xml to the actual implementation of the resource.</p></div><div class="section"><a name="d0e2522"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2522"></a>ejb-jar.xml</h3></div></div><p>The EJB specification facilitate the access to external "resources" through the use of deployment
descriptor entries in ejb-jar.xml.  The tag is &lt;resource-ref&gt; and takes 4 arguments &lt;description&gt;, 
&lt;res-ref-name&gt;, &lt;res-type&gt; and &lt;res-auth&gt;.</p><div class="itemizedlist"><ul><li><p><a name="d0e2528"></a>The res-ref-name element specifies the name of a resource manager connection factory reference.</p></li><li><p><a name="d0e2531"></a>The res-type element specifies the type of the data source. The type is specified by the Java 
interface (or class) expected to be implemented by the data source.</p></li><li><p><a name="d0e2534"></a>The res-auth element specifies whether the enterprise bean code signs on programmatically to 
the resource manager, or whether the Container will sign on to the resource manager on behalf of the bean. 
In the latter case, the Container uses information that is supplied by the Deployer.  Valid entry are 
"Container" and "Application".</p></li></ul></div><p>Example:
<pre class="programlisting">
    &lt;!-- JDBC DataSources (java:comp/env/jdbc) --&gt;
    &lt;resource-ref&gt;
        &lt;description&gt;The default DS&lt;/description&gt;
        &lt;res-ref-name&gt;jdbc/DefaultDS&lt;/res-ref-name&gt;
        &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
        &lt;res-auth&gt;Container&lt;/res-auth&gt;
    &lt;/resource-ref&gt;
    &lt;!-- URL Connection Factories (java:comp/env/url) --&gt;
    &lt;resource-ref&gt;
        &lt;description&gt;The default URL&lt;/description&gt;
        &lt;res-ref-name&gt;url/defaultUrl&lt;/res-ref-name&gt;
        &lt;res-type&gt;java.net.url&lt;/res-type&gt;
        &lt;res-auth&gt;Container&lt;/res-auth&gt;
    &lt;/resource-ref&gt;
    &lt;!-- JavaMail Connection Factories (java:comp/env/mail) --&gt;
    &lt;resource-ref&gt;
        &lt;description&gt;Default Mail&lt;/description&gt;
        &lt;res-ref-name&gt;mail/DefaultMail&lt;/res-ref-name&gt;
        &lt;res-type&gt;javax.mail.Session&lt;/res-type&gt;
        &lt;res-auth&gt;Container&lt;/res-auth&gt;
    &lt;/resource-ref&gt;
    &lt;!-- JMS Connection Factories (java:comp/env/jms) --&gt;
    &lt;resource-ref&gt;
        &lt;description&gt;Default QueueFactory&lt;/description&gt;
        &lt;res-ref-name&gt;jms/QueFactory&lt;/res-ref-name&gt;
        &lt;res-type&gt;javax.jms.QueueConnectionFactory&lt;/res-type&gt;
        &lt;res-auth&gt;Container&lt;/res-auth&gt;
    &lt;/resource-ref&gt;
</pre>
</p></div><div class="section"><a name="d0e2542"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2542"></a>jboss.xml</h3></div></div><p>jboss.xml will redirect the res-ref-name entry to the implementation of the resource.  This is done
in &lt;resource-ref&gt; that takes different arguments depending on how the tag is used: &lt;resource-name&gt;, 
&lt;res-ref-name&gt;, &lt;res-type&gt; and &lt;jndi-name&gt;.</p><div class="itemizedlist"><ul><li><p><a name="d0e2548"></a>res-ref-name must always be present and takes the value of corresponding entry in ejb-jar.xml.</p></li><li><p><a name="d0e2551"></a>jndi-name points to the general definition of the resource in JBoss installation.</p></li><li><p><a name="d0e2554"></a>res-url is used for java.net.URL type Resource only and define the URL.</p></li><li><p><a name="d0e2557"></a>resource-name is there to introduce a new level of indirection to a Resource Manager.</p></li></ul></div><p>A Resource Manager is defined globally for an EJB JAR inside &lt;resource-managers&gt; and takes
different arguments : &lt;res-name&gt;, &lt;res-url&gt;, &lt;res-jndi-name&gt; and res-class.</p><div class="itemizedlist"><ul><li><p><a name="d0e2563"></a>res-name must always be defined and takes the value of corresponding resource-name entry of 
resource-ref.</p></li><li><p><a name="d0e2566"></a>res-jndi-name points to the general definition of the resource in JBoss installation.</p></li><li><p><a name="d0e2569"></a>res-url is used for java.net.URL type Resource only and define the URL.</p></li><li><p><a name="d0e2572"></a>res-class can be blank, or is the Java type of the resource.</p></li></ul></div><p>Example:
<pre class="programlisting">
    &lt;enterprise-beans&gt;
        &lt;session&gt;
            ...
    	    &lt;resource-ref&gt;
                &lt;res-ref-name&gt;jdbc/DefaultDS&lt;/res-ref-name&gt;
                &lt;resource-name&gt;DefaultDS&lt;/jndi-name&gt;
    	    &lt;/resource-ref&gt;
    	    &lt;resource-ref&gt;
                &lt;res-ref-name&gt;url/defaultUrl&lt;/res-ref-name&gt;
                &lt;res-url&gt;http://www.jboss.org/&lt;/res-url&gt;
    	    &lt;/resource-ref&gt;
    	    &lt;resource-ref&gt;
                &lt;res-ref-name&gt;url/otherUrl&lt;/res-ref-name&gt;
	            &lt;resource-name&gt;SourceforgeHomePage&lt;/resource-name&gt;
    	    &lt;/resource-ref&gt;
    	    &lt;resource-ref&gt;
                &lt;res-ref-name&gt;mail/DefaultMail&lt;/res-ref-name&gt;
                &lt;resource-name&gt;DefaultMail&lt;/jndi-name&gt;
    	    &lt;/resource-ref&gt;
    	    &lt;resource-ref&gt;
                &lt;res-ref-name&gt;jms/QueFactory&lt;/res-ref-name&gt;
                &lt;jndi-name&gt;QueueConnectionFactory&lt;/jndi-name&gt;
    	    &lt;/resource-ref&gt;
    	    ...
        &lt;/session&gt;
    &lt;/enterprise-beans&gt;
    
    &lt;resource-managers&gt;
        &lt;resource-manager res-class=""&gt;
            &lt;res-name&gt;DefaultDS&lt;/res-name&gt;
            &lt;res-jndi-name&gt;java:/DefaultDS&lt;/res-jndi-name&gt;
        &lt;/resource-manager&gt;
        &lt;resource-manager res-class=""&gt;
            &lt;res-name&gt;DefaultMail&lt;/res-name&gt;
            &lt;res-jndi-name&gt;java:/Mail&lt;/res-jndi-name&gt;
        &lt;/resource-manager&gt;
        &lt;resource-manager res-class="java.net.URL"&gt;
            &lt;res-name&gt;otherUrl&lt;/res-name&gt;
            &lt;res-url&gt;http://sourceforge.net/&lt;/res-url&gt;
        &lt;/resource-manager&gt;
    &lt;/resource-managers&gt;
</pre>
Note that this example include complexity not necessary in all cases.  Resource Managers does not have to be
specified.  The simple example for JDBC DataSource is:
<pre class="programlisting">
    &lt;resource-ref&gt;
    	&lt;res-ref-name&gt;jdbc/DefaultDS&lt;/res-ref-name&gt;
    	&lt;jndi-name&gt;java:/DefaultDS&lt;/jndi-name&gt;
    &lt;/resource-ref&gt;
</pre>
</p><p>To access the Resource from your bean, do something like:
<pre class="programlisting">
    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/DefaultDS");
</pre>
or
<pre class="programlisting">
    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup("jdbc/DefaultDS");
</pre>
		</p></div><div class="section"><a name="d0e2591"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2591"></a>JDBC Datasources</h3></div></div><p>For JDBC datasources, it points to the JNDI name bound to the Connection Pool.  This is defined in
jboss.jcml like:
<pre class="programlisting">
  &lt;mbean code="org.jboss.jdbc.XADataSourceLoader" name="DefaultDomain:service=XADataSource,name=DefaultDS"&gt;
    &lt;attribute name="PoolName"&gt;DefaultDS&lt;/attribute&gt;
    ...
  &lt;/mbean&gt;
</pre>
</p></div><div class="section"><a name="d0e2599"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2599"></a>JavaMail Session</h3></div></div><p>For JavaMail session, it points to the JNDI name bound to the JavaMail session.  This is defined in
jboss.jcml like:
<pre class="programlisting">
  &lt;mbean code="org.jboss.mail.MailService" name=":service=Mail"&gt;
    &lt;attribute name="JNDIName"&gt;Mail&lt;/attribute&gt;
    ...
  &lt;/mbean&gt;
</pre>
</p></div><div class="section"><a name="d0e2607"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2607"></a>JMS Connection Factory</h3></div></div><p>This is QueueConnectionFactory for JBoss 2.2 and ConnectionFactory for JBoss 2.4.</p></div><div class="section"><a name="d0e2612"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2612"></a>Other</h3></div></div><p>You can define your own Resources as MBean, then use them from ejbs.  A example to look at for 
more information is <a href="javascript:if(confirm('http://www.jboss.org/jboss-castor.jsp  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.jboss.org/jboss-castor.jsp'" tppabs="http://www.jboss.org/jboss-castor.jsp" target="_top">Castor</a>.</p></div></div><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="432" height="79"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch07.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch07s10.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch07s10.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -