activemq-artemis/examples/jms/client-kickoff/readme.html

137 lines
7.0 KiB
HTML
Raw Normal View History

<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>