You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by bu...@apache.org on 2016/03/15 22:22:46 UTC

svn commit: r982886 - in /websites/production/activemq/content: cache/main.pageCache how-to-unit-test-jms-code.html

Author: buildbot
Date: Tue Mar 15 21:22:46 2016
New Revision: 982886

Log:
Production update by buildbot for activemq

Modified:
    websites/production/activemq/content/cache/main.pageCache
    websites/production/activemq/content/how-to-unit-test-jms-code.html

Modified: websites/production/activemq/content/cache/main.pageCache
==============================================================================
Binary files - no diff available.

Modified: websites/production/activemq/content/how-to-unit-test-jms-code.html
==============================================================================
--- websites/production/activemq/content/how-to-unit-test-jms-code.html (original)
+++ websites/production/activemq/content/how-to-unit-test-jms-code.html Tue Mar 15 21:22:46 2016
@@ -36,6 +36,7 @@
       <link href='http://activemq.apache.org/styles/highlighter/styles/shThemeEclipse.css' rel='stylesheet' type='text/css' /> 
       <script src='http://activemq.apache.org/styles/highlighter/scripts/shCore.js' type='text/javascript'></script> 
               <script src='http://activemq.apache.org/styles/highlighter/scripts/shBrushJava.js' type='text/javascript'></script> 
+              <script src='http://activemq.apache.org/styles/highlighter/scripts/shBrushXml.js' type='text/javascript'></script> 
          
       <script type="text/javascript"> 
         SyntaxHighlighter.defaults['toolbar'] = false; 
@@ -81,57 +82,65 @@
   <tbody>
         <tr>
         <td valign="top" width="100%">
-<div class="wiki-content maincontent"><p>When unit testing code with JMS you'll typically want to avoid the overhead of running separate proceses; plus you'll want to increase startup time as fast as possible as you tend to run unit tests often and want immediate feedback. Also persistence can often cause problems - as previous test case results can adversely affect future test case runs - so you often need to purge queues on startup.</p>
-
-<p>So when unit testing JMS code we recommend the following</p>
-
-<ul><li>Use <a shape="rect" href="how-do-i-embed-a-broker-inside-a-connection.html">an embedded broker</a> to avoid a separate broker process being required</li><li>Disable <a shape="rect" href="broker-configuration-uri.html">broker persistence</a> so that no queue purging is required before/after tests</li><li>It's often simpler and faster to just use Java code to create the broker via an XML configuration file using Spring etc.</li></ul>
-
-
-<p>You can do all of this using the following Java code to create your JMS ConnectionFactory which will also automatically create an embedded broker</p>
-
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
-ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
+<div class="wiki-content maincontent"><p>When unit testing code with JMS you'll typically want to avoid the overhead of running separate proceses; plus you'll want to increase startup time as fast as possible as you tend to run unit tests often and want immediate feedback. Also persistence can often cause problems - as previous test case results can adversely affect future test case runs - so you often need to purge queues on startup.</p><p>So when unit testing JMS code we recommend the following</p><ul><li>Use <a shape="rect" href="how-do-i-embed-a-broker-inside-a-connection.html">an embedded broker</a> to avoid a separate broker process being required</li><li>Disable <a shape="rect" href="broker-configuration-uri.html">broker persistence</a> so that no queue purging is required before/after tests</li><li>It's often simpler and faster to just use Java code to create the broker via an XML configuration file using Spring etc.</li></ul><p>You can do all of this using the following Jav
 a code to create your JMS ConnectionFactory which will also automatically create an embedded broker</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
 </pre>
-</div></div>
-
-<p>For more configuration options see the <a shape="rect" href="vm-transport-reference.html">VM Transport Reference</a> and the <a shape="rect" href="broker-configuration-uri.html">Broker Configuration URI</a></p>
-
-<p>Or if you really would rather be more explicit you can create the broker first using the following Java code </p>
-
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
-BrokerService broker = new BrokerService();
+</div></div><p>For more configuration options see the <a shape="rect" href="vm-transport-reference.html">VM Transport Reference</a> and the <a shape="rect" href="broker-configuration-uri.html">Broker Configuration URI</a></p><p>Or if you really would rather be more explicit you can create the broker first using the following Java code</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">BrokerService broker = new BrokerService();
 broker.setPersistent(false);
 broker.start();
 </pre>
-</div></div>
-
-<p>or you could use the <a shape="rect" href="spring-support.html">Spring Support</a></p>
-
-<h3 id="HowtounittestJMScode-UsingJNDI">Using JNDI</h3>
-
-<p>If your application code is using JNDI to lookup the JMS ConnectionFactory and Destinations to use, then you could use the <a shape="rect" href="jndi-support.html">JNDI Support</a> in ActiveMQ.</p>
-
-<p>Add the following jndi.properties to your classpath (e.g. in src/test/resources if you are using maven).</p>
-
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
-java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
-java.naming.provider.url = vm://localhost?broker.persistent=false
+</div></div><p>or you could use the <a shape="rect" href="spring-support.html">Spring Support</a></p><h3 id="HowtounittestJMScode-UsingJNDI">Using JNDI</h3><p>If your application code is using JNDI to lookup the JMS ConnectionFactory and Destinations to use, then you could use the&#160;<a shape="rect" href="jndi-support.html">JNDI Support</a>&#160;in ActiveMQ.</p><p>Add the following jndi.properties to your classpath (e.g. in src/test/resources if you are using maven).</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
+java.naming.provider.url = vm://localhost?broker.persistent=false</pre>
+</div></div><p>You should then consider using&#160;<a shape="rect" href="jndi-support.html">Dynamic destinations in JNDI</a>&#160;so that your code looks up destinations via</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">context.lookup("dynamicQueues/FOO.BAR");</pre>
+</div></div><h3 id="HowtounittestJMScode-UsingtheEmbeddedActiveMQBrokerJUnitRule(ActiveMQ5.13)">Using the EmbeddedActiveMQBroker JUnit Rule (ActiveMQ 5.13)</h3><p>If your test code is using JUnit, then you could use the EmbeddedActiveMQBroker JUnit Rule provided&#160;in the activemq-junit library.</p><p>Add the activemq-junit library along with the activemq-broker libraries for the version of ActiveMQ you want to test with. &#160;The rule will use whatever version of ActiveMQ it finds in the classpath, so the ActiveMQ libraries need to be specified if they are not already there.</p><p>If you are using Maven, add the following to your pom.xml</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Maven configuration for JUnit Rule</b></div><div class="codeContent panelContent pdl">
+<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;dependency&gt;
+    &lt;groupId&gt;org.apache.activemq.tooling&lt;/groupId&gt;
+    &lt;artifactId&gt;activemq-junit&lt;/artifactId&gt;
+    &lt;version&gt;${activemq-junit-version}&lt;/version&gt;
+    &lt;scope&gt;test&lt;/scope&gt;
+&lt;/dependency&gt; 
+
+&lt;dependency&gt;
+    &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
+    &lt;artifactId&gt;activemq-broker&lt;/artifactId&gt;
+    &lt;version&gt;${activemq-version}&lt;/version&gt;
+    &lt;scope&gt;test&lt;/scope&gt;
+&lt;/dependency&gt;</pre>
+</div></div><p>&#160;Then add the EmbeddedActiveMQBroker JUnit Rule to your test, and JUnit will start the embedded broker at the beginning of each test and stop the broker at the end of the test.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Use ActiveMQ JUnit Rule</b></div><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">@Rule
+public EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();</pre>
+</div></div><p>By default, the EmbeddedActiveMQBroker will configure the broker as non-persistent, and the only transport available will be the VM transport. &#160;To customize this configuration, either extend the EmbeddedActiveMQBroker class and override the configure() method, or use an XML configuration for the broker. &#160;Note that to use the XML configuration, you may to to add additional libraries on the classpath to support the XBean configuration of ActiveMQ.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Customize EmbeddedActiveMQBroker using Java configuration</b></div><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">@Rule
+EmbeddedActiveMQBroker customizedBroker = new EmbeddedActiveMQBroker() {
+    @Override
+    protected void configure() {
+        // Perform additional configuration here
+    }
+}</pre>
+</div></div><p><span>Note that to use the XML configuration, you may to to add additional libraries on the classpath to support the XBean configuration of ActiveMQ.</span></p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Customize EmbeddedActiveMQBroker using XML configuration</b></div><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">@Rule
+EmbeddedActiveMQBroker customizedBroker = new EmbeddedActiveMQBroker("bean:customize-activemq.xml");
 </pre>
-</div></div>
-
-<p>You should then consider using <a shape="rect" href="jndi-support.html">Dynamic destinations in JNDI</a> so that your code looks up destinations via</p>
-
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
-context.lookup("dynamicQueues/FOO.BAR");
-</pre>
-</div></div>
-
-<p>To avoid having to explicitly configure every single JMS destination in the jndi.properties file, please see the <a shape="rect" href="how-do-i-create-new-destinations.html">other options for creating destinations</a>.</p></div>
+</div></div><p>Note that to use the XML configuration, you may to to add additional libraries on the classpath to support the XBean configuration of ActiveMQ. &#160;The versions of the spring-context library should correspond with the version used by your selected version of ActiveMQ.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Maven configuration for XBean configuration</b></div><div class="codeContent panelContent pdl">
+<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;dependency&gt;
+    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
+    &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
+    &lt;version&gt;Appropriate version for activemq-version&lt;/version&gt;
+&lt;/dependency&gt;
+
+&lt;dependency&gt;
+    &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
+    &lt;artifactId&gt;activemq-spring&lt;/artifactId&gt;
+    &lt;version&gt;${activemq-version&gt;&lt;/version&gt;
+&lt;/dependency&gt;</pre>
+</div></div><p>Then you can use the VM URI to connect with the broker</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://embedded-broker?create=false");</pre>
+</div></div><p>You can also get a connection factory from the EmbeddedActiveMQBroker</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">ConnectionFactory connectionFactory = embeddedBroker.createConnectionFactory();</pre>
+</div></div></div>
         </td>
         <td valign="top">
           <div class="navigation">