activemq-artemis/examples/jms/client-kickoff
Andy Taylor 4b63891aaa ACTIVEMQ6-67 - cleaned up configuration
https://issues.apache.org/jira/browse/ACTIVEMQ6-67

fixed distribution so that file based security works and hot deployers as broken and no longer needed with new bootstrap.

Also combined the jms and core configuration files.
2015-01-15 15:48:22 +00:00
..
src/main ACTIVEMQ6-67 - cleaned up configuration 2015-01-15 15:48:22 +00:00
pom.xml ActiveMQ6-65 JBoss JMS1.1 -> Geronimo 2.0 spec jar 2015-01-07 19:54:42 +00:00
readme.html ACTIVEMQ6-43(reopened) : Replace License Headers on codebase 2015-01-05 13:14:25 -05:00

readme.html

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<html>
  <head>
    <title>ActiveMQ Client Kickoff Example</title>
    <link rel="stylesheet" type="text/css" href="../common/common.css" />
    <link rel="stylesheet" type="text/css" href="../common/prettify.css" />
    <script type="text/javascript" src="../common/prettify.js"></script>
  </head>
  <body onload="prettyPrint()">
     <h1>Client Kickoff Example</h1>

     <p>This example shows how to kick off a client connected to ActiveMQ 
         using <a href="http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/">JMX</a></p>

     <p>The example will connect to ActiveMQ. Using JMX, we will list the remote addresses connected to the 
         server and close the corresponding connections. The client will be kicked off from ActiveMQ receiving
         an exception that its JMS connection was interrupted.</p>

     <h2>Example configuration</h2>

     <p>ActiveMQ exposes its managed resources by default on the platform MBeanServer.</p>
     <p>To access this MBeanServer remotely, the Java Virtual machine must be started with system properties:
         <pre class="prettyprint">
             <code>-Dcom.sun.management.jmxremote
             -Dcom.sun.management.jmxremote.port=3000
             -Dcom.sun.management.jmxremote.ssl=false
             -Dcom.sun.management.jmxremote.authenticate=false</code>
        </pre>
        <p>These properties are explained in the Java 5 <a href="http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html#remote">Management guide</a>
            (please note that for this example, we will disable user authentication for simplicity).</p>        
        <p>With these properties, ActiveMQ server will be manageable remotely using standard JMX URL on port <code>3000</code>.</p> 
     </p>
         
     <h2>Example step-by-step</h2>
     <p><em>To run the example, simply type <code>mvn verify</code> from this directory</em></p>
     <ol>
        <li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get its properties from <a href="src/main/resources/activemq/server0/client-jndi.properties">client-jndi.properties</a></li>
        <pre class="prettyprint">
            <code>InitialContext initialContext = getContext(0);</code>
        </pre>

        <li>We look up the JMS connection factory object from JNDI</li>
        <pre class="prettyprint">
            <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
        </pre>

        <li>We create a JMS connection</li>
        <pre class="prettyprint">
            <code>connection = cf.createConnection();</code>
        </pre>
        
        <li>We set a <code>ExceptionListener</code> on the connection to be notified after a problem occurred</li>
        <pre class="prettyprint">
            <code>final AtomicReference&lt;JMSException&gt; exception = new AtomicReference&lt;JMSException&gt;();
            connection.setExceptionListener(new ExceptionListener()
            {
               public void onException(JMSException e)
               {
                  exception.set(e);
               }
            });</code>
        </pre>

        <li>We start the connection</li>
        <pre class="prettyprint">
            <code>connection.start();</code>
       </pre>

        <li>We create a MBean proxy to the ActiveMQServerControlMBean used to manage ActiveMQ server
            (see <a href="../jmx/readme.html">JMX example</a> for a complete explanation of the different steps)</li>
        <pre class="prettyprint">
            <code>ObjectName on = ObjectNameBuilder.DEFAULT.getActiveMQServerObjectName();
            JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap<String, String>());
            MBeanServerConnection mbsc = connector.getMBeanServerConnection();
            ActiveMQServerControlMBean serverControl = (ActiveMQServerControlMBean)MBeanServerInvocationHandler.newProxyInstance(mbsc,
                                                                                                on,
                                                                                                ActiveMQServerControlMBean.class,
                                                                                                false);
            </code>
        </pre>

        <li>Using the server MBean, we list the remote address connected to the server</li>
        <pre class="prettyprint">
            <code>String[] remoteAddresses = serverControl.listRemoteAddresses();
            for (String remoteAddress : remoteAddresses)
            {
               System.out.println(remoteAddress);
            }
            </code>
        </pre>

        <p>It will display a single address corresponding to the connection opened at step 3.</p>
            
        <li>We close the connections corresponding to this remote address</li>
        <pre class="prettyprint">
              <code>serverControl.closeConnectionsForAddress(remoteAddresses[0]);</code>
        </pre>

        <p>Warnings be displayed on the server output:</p>
        <pre class="prettyprint">
            <code>org.apache.activemq.jms.example.SpawnedJMSServer out:11:22:33,034 WARN  @RMI TCP Connection(3)-192.168.0.10 [RemotingConnectionImpl] Connection failure has been detected connections for /192.168.0.10:52707 closed by management:0
            org.apache.activemq.jms.example.SpawnedJMSServer out:11:22:33,035 WARN  @RMI TCP Connection(3)-192.168.0.10 [ServerSessionImpl] Client connection failed, clearing up resources for session 4646da35-2fe8-11de-9ce9-752ccc2b26e4
            org.apache.activemq.jms.example.SpawnedJMSServer out:11:22:33,035 WARN  @RMI TCP Connection(3)-192.168.0.10 [ServerSessionImpl] Cleared up resources for session 4646da35-2fe8-11de-9ce9-752ccc2b26e4
            </code>
        </pre>
        
        <li>We display the exception received by the connection's ExceptionListener</li>
        <pre class="prettyprint">
            <code>exception.get().printStackTrace();</code>
        </pre>
        
        <p>When the connection was closed on the server-side by the call to <code>serverControl.closeConnectionsForAddress()</code>, 
        the client's connection was disconnected and its exception listener was notified.</p>
        
        <li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>

        <pre class="prettyprint">
           <code>finally
           {
              if (initialContext != null)
              {
                initialContext.close();
              }
              if (connection != null)
              {
                 connection.close();
              }
           }</code>
        </pre>
     </ol>
                
     <h2>More information</h2>
     
     <ul>
        <li><a href="http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html">Java 5 Management guide</a></li>
     </ul>
  </body>
</html>