Client Kickoff Example

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.

Example configuration

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.

Example step-by-step

To run the example, simply type mvn verify -Pexample from this directory

  1. 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 client-jndi.properties
  2.             InitialContext initialContext = getContext(0);
            
  3. We look up the JMS connection factory object from JNDI
  4.             ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
            
  5. We create a JMS connection
  6.             connection = cf.createConnection();
            
  7. We set a ExceptionListener on the connection to be notified after a problem occurred
  8.             final AtomicReference<JMSException> exception = new AtomicReference<JMSException>();
                connection.setExceptionListener(new ExceptionListener()
                {
                   public void onException(JMSException e)
                   {
                      exception.set(e);
                   }
                });
            
  9. We start the connection
  10.             connection.start();
           
  11. We create a MBean proxy to the ActiveMQServerControlMBean used to manage ActiveMQ Artemis server (see JMX example for a complete explanation of the different steps)
  12.             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);
                
            
  13. Using the server MBean, we list the remote address connected to the server
  14.             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.

  15. We close the connections corresponding to this remote address
  16.               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
                
            
  17. We display the exception received by the connection's ExceptionListener
  18.             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.

  19. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  20.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }
            

More information