📄 ch08s32.html
字号:
<resource-name>topicfactoryref</resource-name>
</resource-ref>
<resource-ref>
<res-ref-name>jms/TopicName</res-ref-name>
<resource-name>topicref</resource-name>
</resource-ref>
</session>
</pre></div><p>Here there are the complete listings for the deployment descriptors:</p><div class="figure"><p><a name="d0e5754"></a><b>Figure 8.89. [<span class="bold">2.4.0</span>] JMS resource example <tt>ejb-jar.xml</tt>, from <tt>TopicHello-ejb-jar.xml</tt> in directory <tt>org/jboss/docs/jms/resources</tt></b></p><pre class="programlisting">
<?xml version="1.0" encoding="Cp1252"?>
<ejb-jar>
<description>Topic Hello</description>
<display-name>TopicHelloBean</display-name>
<enterprise-beans>
<session>
<display-name>TopicHello</display-name>
<ejb-name>TopicHello</ejb-name>
<home>org.jboss.docs.jms.ra.interfaces.HelloHome</home>
<remote>org.jboss.docs.jms.ra.interfaces.Hello</remote>
<ejb-class>org.jboss.docs.jms.ra.bean.TopicHelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref>
<description>A Topic ConnectionFactory</description>
<res-ref-name>jms/MyTopicConnection</res-ref-name>
<res-type>javax.jms.TopicConnectionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>A Topic </description>
<res-ref-name>jms/TopicName</res-ref-name>
<res-type>javax.jms.Topic</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</session>
</enterprise-beans>
<assembly-descriptor/>
</ejb-jar>
</pre></div><div class="figure"><p><a name="d0e5770"></a><b>Figure 8.90. [<span class="bold">2.4.0</span>] JMS resource example <tt>jboss.xml</tt>, from <tt>TopicHello24-jboss.xml</tt> in directory <tt>org/jboss/docs/jms/resources</tt></b></p><pre class="programlisting">
<?xml version="1.0" encoding="Cp1252"?>
<jboss>
<secure>false</secure>
<resource-managers>
<resource-manager>
<res-name>topicfactoryref</res-name>
<res-jndi-name>java:/JmsXA</res-jndi-name>
</resource-manager>
<resource-manager>
<res-name>topicref</res-name>
<res-jndi-name>topic/testTopic</res-jndi-name>
</resource-manager>
</resource-managers>
<enterprise-beans>
<session>
<ejb-name>TopicHello</ejb-name>
<jndi-name>TopicHello</jndi-name>
<configuration-name>Standard Stateless SessionBean</configuration-name>
<resource-ref>
<res-ref-name>jms/MyTopicConnection</res-ref-name>
<resource-name>topicfactoryref</resource-name>
</resource-ref>
<resource-ref>
<res-ref-name>jms/TopicName</res-ref-name>
<resource-name>topicref</resource-name>
</resource-ref>
</session>
</enterprise-beans>
</jboss>
</pre></div><p>It is possible to run the example with the Ant target:</p><div class="figure"><p><a name="d0e5788"></a><b>Figure 8.91. [<span class="bold">2.4.0</span>] Running the JMS resource example</b></p><pre class="programlisting">
ant jms-topic-hello24
</pre></div></div><div class="section"><a name="jms-resource-example-remote"></a><div class="titlepage"><div><h3 class="title"><a name="jms-resource-example-remote"></a>[<span class="bold"><b>2.4.0</b></span>] JMS remote resource</h3></div></div><p>A nice twist to the example above is how easy it is to reconfigure it to send messages to a remote JBoss server. You basically have to do two things.</p><p>First, you need to add two things to <tt>jboss.xml</tt>: The remote JMS provider and a JMS resource adapter that uses the remote JMS provider. When defining the remote JMS provider you should fill in the <tt>ProviderUrl</tt> property. For JBoss version <span class="bold"><b>2.4.0</b></span> and for the URL <tt>linutv1.annons.dn.se:1099</tt>, it might look like this:</p><div class="figure"><p><a name="d0e5819"></a><b>Figure 8.92. [<span class="bold">2.4.0</span>] Defining a remote JMSProviderLoader in <tt>jboss.jcml</tt>, from <tt>TopicHello24-Remote-snippet-jboss.jcml</tt> in directory <tt>org/jboss/docs/jms/resources</tt></b></p><pre class="programlisting">
<mbean code="org.jboss.jms.jndi.JMSProviderLoader"
name=":service=JMSProviderLoader,name=MyRemoteLoader">
<attribute name="ProviderName">MyRemoteProvider</attribute>
<attribute name="ProviderUrl">linutv1.annons.dn.se:1099</attribute>
<attribute name="QueueFactoryRef">XAQueueConnectionFactory</attribute>
<attribute name="TopicFactoryRef">XATopicConnectionFactory</attribute>
<attribute name="ProviderAdapterClass">org.jboss.jms.jndi.JBossMQProvider</attribute>
</mbean>
</pre></div><p>[<span class="bold"><b>2.4.1</b></span>. In this version it looks slightly different.]</p><div class="figure"><p><a name="d0e5840"></a><b>Figure 8.93. [<span class="bold">2.4.1</span>] Defining a remote JMSProviderLoader in <tt>jboss.jcml</tt></b></p><pre class="programlisting">
<!-- This must go into jboss.jcml -->
<!-- to get remote version to work -->
<mbean code="org.jboss.jms.jndi.JMSProviderLoader"
name=":service=JMSProviderLoader,name=MyRemoteLoader">
<attribute name="ProviderName">MyRemoteProvider</attribute>
<attribute name="ProviderUrl">linutv1.annons.dn.se:1099</attribute>
<attribute name="QueueFactoryRef">XAConnectionFactory</attribute>
<attribute name="TopicFactoryRef">XAConnectionFactory</attribute>
<attribute name="ProviderAdapterClass">org.jboss.jms.jndi.JBossMQProvider</attribute>
</mbean>
</pre></div><p>You then need to add a JMS resource adapter that uses the remote provider. We give it a name, in this case <tt>RemoteJmsXA</tt>, and we configure the adapter property <tt>JmsProviderAdapterJNDI</tt> to point to our newly-defined remote provider <tt>java:MyRemoteProvider</tt>.</p><div class="figure"><p><a name="d0e5861"></a><b>Figure 8.94. [<span class="bold">2.4.0</span>] Defining a remote JMS Resource Adapter in <tt>jboss.jcml</tt>, from <tt>TopicHello24-Remote-snippet-jboss.jcml</tt> in directory <tt>org/jboss/docs/jms/resources</tt></b></p><pre class="programlisting">
<!-- JMS XA Resource adapter, -->
<!-- use this to get transacted JMS in beans -->
<mbean code="org.jboss.resource.ConnectionFactoryLoader"
name="JCA:service=ConnectionFactoryLoader,name=RemoteJmsXA">
<attribute name="FactoryName">RemoteJmsXA</attribute>
<attribute name="RARDeployerName">JCA:service=RARDeployer</attribute>
<attribute name="ResourceAdapterName">JMS Adapter</attribute>
<attribute name="ConnectionManagerFactoryName">MinervaXACMFactory</attribute>
<!-- See the documentation for the specific connection manager
implementation you are using for the properties you can set -->
<attribute name="ConnectionManagerProperties">
<attribute name="Properties">
JmsProviderAdapterJNDI=java:MyRemoteProvider
# Pool type - uncomment to force, otherwise it is the default
#PoolConfiguration=per-factory
# Connection pooling properties - see
# org.opentools.minerva.pool.PoolParameters
MinSize=0
MaxSize=10
Blocking=true
GCEnabled=false
IdleTimeoutEnabled=false
InvalidateOnError=false
TrackLastUsed=false
GCIntervalMillis=120000
GCMinIdleMillis=1200000
IdleTimeoutMillis=1800000
MaxIdleTimeoutPercent=1.0
</attribute>
<!-- Principal mapping configuration -->
<attribute name="PrincipalMappingClass">org.jboss.resource.security.ManyToOnePrincipalMapping</attribute>
<attribute name="PrincipalMappingProperties">
</attribute>
</mbean>
</pre></div><p>The second thing to do is to rewrite the <tt>jboss.xml</tt> deployment descriptor. We now let the <tt>res-jndi-name</tt> property for the <tt>topicfactoryref</tt> resource point to our defined resource adapter <tt>java:/RemoteJmsXA</tt>, and we point the destination resource to the JNDI namespace of our remote server, <tt>jnp://linutv1.annons.dn.se:1099/topic/testTopic</tt>. Here it is the complete descriptor file:</p><div class="figure"><p><a name="d0e5894"></a><b>Figure 8.95. [<span class="bold">2.4.0</span>] <tt>jboss.xml</tt> for remote version of JMS resource example, from <tt>TopicHello24-Remote-jboss.xml</tt> in directory <tt>org/jboss/docs/jms/resources</tt></b></p><pre class="programlisting">
<?xml version="1.0" encoding="Cp1252"?>
<jboss>
<secure>false</secure>
<resource-managers>
<resource-manager>
<res-name>topicfactoryref</res-name>
<res-jndi-name>java:/RemoteJmsXA</res-jndi-name>
</resource-manager>
<resource-manager>
<res-name>topicref</res-name>
<res-jndi-name>jnp://linutv1.annons.dn.se:1099/topic/testTopic</res-jndi-name>
</resource-manager>
</resource-managers>
<enterprise-beans>
<session>
<ejb-name>TopicHello</ejb-name>
<jndi-name>TxTopicHello</jndi-name>
<configuration-name>Standard Stateless SessionBean</configuration-name>
<resource-ref>
<res-ref-name>jms/MyTopicConnection</res-ref-name>
<resource-name>topicfactoryref</resource-name>
</resource-ref>
<resource-ref>
<res-ref-name>jms/TopicName</res-ref-name>
<resource-name>topicref</resource-name>
</resource-ref>
</session>
</enterprise-beans>
</jboss>
</pre></div></div><div class="section"><a name="jms-resource-22"></a><div class="titlepage"><div><h3 class="title"><a name="jms-resource-22"></a>JBoss emulated resource</h3></div></div><p>Is there no way of getting JMS to work as a resource in JBoss <span class="bold"><b>2.2.x</b></span> then? Well, it depends. Probably, it is possible to deploy the resource adapter for JMS in JBoss version <span class="bold"><b>2.2.x</b></span>, but that is an advanced and untried subject. Go ahead and test it and report whether it works. However, there is at least one more thing you can do to a least ease the transition from JBoss release <span class="bold"><b>2.2.x</b></span> to later ones. You may actually code your beans <span class="emphasis"><i>as if</i></span> they are using JMS as a resource.</p><p>It is very important to understand that even if you do this you will <span class="emphasis"><i>not</i></span> see the correct behavior of getting the transactions enlisted. The only benefit for you is that you will get it all when upgrading to at least version <span class="bold"><b>2.4.0</b></span> and without a lot of code or descriptor file rewriting.</p><p>So, how is it done? The first thing you need to do, in comparison to the example compatible with version <span class="bold"><b>2.4.0</b></span>, is to make a small change in the code where the JMS session is created. You need to mark the session as not transacted:</p><div class="figure"><p><a name="d0e5941"></a><b>Figure 8.96. Creating a session for JMS resource emulation, from <tt>TopicHelloBean22.java</tt> in directory <tt>org/jboss/docs/jms/ra/bean</tt></b></p><pre class="programlisting">
topicSession = topicConnection.createTopicSession(
// NOT transacted
false,
Session.AUTO_ACKNOWLEDGE);
</pre></div><p>To be completely honest, it is actually possible to use this code with the JMS resource adapter in version <span class="bold"><b>2.4.0</b></span> too, since the topic connection will not care whether you mark your JMS session as transacted or not when running with XA transactions. By writing your code like this it will work in version <span class="bold"><b>2.2.x</b></span> and later.</p><p>However, you are forced to change the deployment descriptor file. In version <span class="bold"><b>2.4.0</b></span> we pointed the connection factory resource to the JMS resource adapter. In <span class="bold"><b>2.2.x</b></span> you must point it directly to a ConnectionFactory. It is not possible to use the <tt>res-jndi-name</tt> element, either; use the <tt>res-url</tt> element, instead. This is how it might look:</p><div class="figure"><p><a name="d0e5973"></a><b>Figure 8.97. JMS resource emulation in <tt>jboss.xml</tt></b></p><pre class="programlisting">
<?xml version="1.0" encoding="Cp1252"?>
<jboss>
<secure>false</secure>
<resource-managers>
<resource-manager>
<res-name>topicfactoryref</res-name>
<res-url>TopicConnectionFactory</res-url>
</resource-manager>
<resource-manager>
<res-name>topicref</res-name>
<res-url>topic/testTopic</res-url>
</resource-manager>
</resource-managers>
<enterprise-beans>
<session>
<ejb-name>TopicHello</ejb-name>
<jndi-name>TopicHello</jndi-name>
<configuration-name>Standard Stateless SessionBean</configuration-name>
<resource-ref>
<res-ref-name>jms/MyTopicConnection</res-ref-name>
<resource-name>topicfactoryref</resource-name>
</resource-ref>
<resource-ref>
<res-ref-name>jms/TopicName</res-ref-name>
<resource-name>topicref</resource-name>
</resource-ref>
</session>
</enterprise-beans>
</jboss>
</pre></div><p>You may test the example through Ant:</p><div class="figure"><p><a name="d0e5982"></a><b>Figure 8.98. Running the emulated JMS resource example</b></p><pre class="programlisting">
ant jms-topic-hello22
</pre></div></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="ch08.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch08s20.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch08s20.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch09.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch09.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 + -