This example shows how to kick off a client connected to ActiveMQ using JMX
The example will connect to ActiveMQ Artemis. 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 Artemis receiving an exception that its JMS connection was interrupted.
ActiveMQ Artemis exposes its managed resources by default on the platform MBeanServer.
To access this MBeanServer remotely, the Java Virtual machine must be started with system properties:
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=3000
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
These properties are explained in the Java 5 Management guide (please note that for this example, we will disable user authentication for simplicity).
With these properties, ActiveMQ Artemis server will be manageable remotely using standard JMX URL on port 3000
.
To run the example, simply type mvn verify -Pexample
from this directory
InitialContext initialContext = getContext(0);
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
ExceptionListener
on the connection to be notified after a problem occurred
final AtomicReference<JMSException> exception = new AtomicReference<JMSException>();
connection.setExceptionListener(new ExceptionListener()
{
public void onException(JMSException e)
{
exception.set(e);
}
});
connection.start();
ObjectName on = ObjectNameBuilder.DEFAULT.getActiveMQServerObjectName();
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap());
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ActiveMQServerControlMBean serverControl = (ActiveMQServerControlMBean)MBeanServerInvocationHandler.newProxyInstance(mbsc,
on,
ActiveMQServerControlMBean.class,
false);
String[] remoteAddresses = serverControl.listRemoteAddresses();
for (String remoteAddress : remoteAddresses)
{
System.out.println(remoteAddress);
}
It will display a single address corresponding to the connection opened at step 3.
serverControl.closeConnectionsForAddress(remoteAddresses[0]);
Warnings be displayed on the server output:
org.apache.activemq.artemis.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.artemis.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.artemis.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
exception.get().printStackTrace();
When the connection was closed on the server-side by the call to serverControl.closeConnectionsForAddress()
,
the client's connection was disconnected and its exception listener was notified.
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}