Package org.hornetq.tests.integration.jms.server.management

Source Code of org.hornetq.tests.integration.jms.server.management.JMSServerControl2Test

/*
* Copyright 2009 Red Hat, Inc.
* Red Hat 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.
*/

package org.hornetq.tests.integration.jms.server.management;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TemporaryTopic;
import javax.jms.TextMessage;
import javax.jms.Topic;

import junit.framework.Assert;

import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.jms.HornetQJMSClient;
import org.hornetq.api.jms.management.JMSConnectionInfo;
import org.hornetq.api.jms.management.JMSConsumerInfo;
import org.hornetq.api.jms.management.JMSServerControl;
import org.hornetq.api.jms.management.JMSSessionInfo;
import org.hornetq.core.config.Configuration;
import org.hornetq.core.config.impl.ConfigurationImpl;
import org.hornetq.core.logging.Logger;
import org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory;
import org.hornetq.core.remoting.impl.invm.InVMConnectorFactory;
import org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory;
import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.core.server.HornetQServers;
import org.hornetq.jms.client.HornetQMessage;
import org.hornetq.jms.client.HornetQQueue;
import org.hornetq.jms.server.impl.JMSServerManagerImpl;
import org.hornetq.tests.integration.management.ManagementControlHelper;
import org.hornetq.tests.integration.management.ManagementTestBase;
import org.hornetq.tests.unit.util.InVMContext;
import org.hornetq.tests.util.RandomUtil;

/**
* A QueueControlTest
*
* @author <a href="jmesnil@redhat.com">Jeff Mesnil</a>
*
* Created 14 nov. 2008 13:35:10
*
*
*
*/
public class JMSServerControl2Test extends ManagementTestBase
{
   // Constants -----------------------------------------------------

   private static final Logger log = Logger.getLogger(JMSServerControl2Test.class);

   private static final long CONNECTION_TTL = 1000;

   private static final long PING_PERIOD = JMSServerControl2Test.CONNECTION_TTL / 2;

   // Attributes ----------------------------------------------------

   private HornetQServer server;

   JMSServerManagerImpl serverManager;

   private InVMContext context;

   // Static --------------------------------------------------------

   private void startHornetQServer(final String acceptorFactory) throws Exception
   {
      Configuration conf = createBasicConfig();
      conf.setSecurityEnabled(false);
      conf.setJMXManagementEnabled(true);
      conf.getAcceptorConfigurations().add(new TransportConfiguration(acceptorFactory));
      server = HornetQServers.newHornetQServer(conf, mbeanServer, true);
      server.start();

      context = new InVMContext();
      serverManager = new JMSServerManagerImpl(server);
      serverManager.setContext(context);
      serverManager.start();
      serverManager.activated();
   }

   // Constructors --------------------------------------------------

   // Public --------------------------------------------------------

   public void testListClientConnectionsForInVM() throws Exception
   {
      doListClientConnections(InVMAcceptorFactory.class.getName(), InVMConnectorFactory.class.getName());
   }

   public void testListClientConnectionsForNetty() throws Exception
   {
      doListClientConnections(NettyAcceptorFactory.class.getName(), NettyConnectorFactory.class.getName());
   }

   public void testCloseConnectionsForAddressForInVM() throws Exception
   {
      doCloseConnectionsForAddress(InVMAcceptorFactory.class.getName(), InVMConnectorFactory.class.getName());
   }

   public void testCloseConnectionsForAddressForNetty() throws Exception
   {
      doCloseConnectionsForAddress(NettyAcceptorFactory.class.getName(), NettyConnectorFactory.class.getName());
   }

   public void testCloseConnectionsForUnknownAddressForInVM() throws Exception
   {
      doCloseConnectionsForUnknownAddress(InVMAcceptorFactory.class.getName(), InVMConnectorFactory.class.getName());
   }

   public void testCloseConnectionsForUnknownAddressForNetty() throws Exception
   {
      doCloseConnectionsForUnknownAddress(NettyAcceptorFactory.class.getName(), NettyConnectorFactory.class.getName());
   }

   public void testListSessionsForInVM() throws Exception
   {
      doListSessions(InVMAcceptorFactory.class.getName(), InVMConnectorFactory.class.getName());
   }

   public void testListSessionsForNetty() throws Exception
   {
      doListSessions(NettyAcceptorFactory.class.getName(), NettyConnectorFactory.class.getName());
   }

   public void testListConnectionIDsForInVM() throws Exception
   {
      doListConnectionIDs(InVMAcceptorFactory.class.getName(), InVMConnectorFactory.class.getName());
   }

   public void testListConnectionIDsForNetty() throws Exception
   {
      doListConnectionIDs(NettyAcceptorFactory.class.getName(), NettyConnectorFactory.class.getName());
   }
  
   public void testListConnectionsAsJSONForNetty() throws Exception
   {
      doListConnectionsAsJSON(NettyAcceptorFactory.class.getName(), NettyConnectorFactory.class.getName());
   }

   public void testListConnectionsAsJSONForInVM() throws Exception
   {
      doListConnectionsAsJSON(InVMAcceptorFactory.class.getName(), InVMConnectorFactory.class.getName());
   }
  
   public void testListConsumersAsJSON() throws Exception
   {
      String queueName = RandomUtil.randomString();

      try
      {
         startHornetQServer(NettyAcceptorFactory.class.getName());
         serverManager.createQueue(false, queueName, null, true, queueName);
         Queue queue = HornetQJMSClient.createQueue(queueName);

         JMSServerControl control = createManagementControl();

         long startTime = System.currentTimeMillis();
        
         String jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
         JMSConnectionInfo[] infos = JMSConnectionInfo.from(jsonStr);
         assertEquals(0, infos.length);

         ConnectionFactory cf1 = JMSUtil.createFactory(NettyConnectorFactory.class.getName(),
                                                       JMSServerControl2Test.CONNECTION_TTL,
                                                       JMSServerControl2Test.PING_PERIOD);
         Connection connection = cf1.createConnection();
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         TemporaryTopic temporaryTopic = session.createTemporaryTopic();

         // create a regular message consumer
         MessageConsumer consumer = session.createConsumer(queue);

         jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
         infos = JMSConnectionInfo.from(jsonStr);
         assertEquals(1, infos.length);
         String connectionID = infos[0].getConnectionID();
        
         String consJsonStr = control.listConsumersAsJSON(connectionID);
         assertNotNull(consJsonStr);
         JMSConsumerInfo[] consumerInfos = JMSConsumerInfo.from(consJsonStr);
         assertEquals(1, consumerInfos.length);
         JMSConsumerInfo consumerInfo = consumerInfos[0];
         assertNotNull(consumerInfo.getConsumerID());
         assertEquals(connectionID, consumerInfo.getConnectionID());
         assertEquals(queue.getQueueName(), consumerInfo.getDestinationName());
         assertEquals("queue", consumerInfo.getDestinationType());
         assertNull(consumerInfo.getFilter());
         assertEquals(false, consumerInfo.isBrowseOnly());
         assertEquals(false, consumerInfo.isDurable());
         assertTrue(startTime <= consumerInfo.getCreationTime() && consumerInfo.getCreationTime() <= System.currentTimeMillis());

         consumer.close();
        
         consJsonStr = control.listConsumersAsJSON(connectionID);
         assertNotNull(consJsonStr);
         consumerInfos = JMSConsumerInfo.from(consJsonStr);
         assertEquals(0, consumerInfos.length);

         // create a queue browser
         QueueBrowser browser = session.createBrowser(queue);
         // the server resources are created when the browser starts enumerating
         browser.getEnumeration();
        
         consJsonStr = control.listConsumersAsJSON(connectionID);
         assertNotNull(consJsonStr);
         System.out.println(consJsonStr);
         consumerInfos = JMSConsumerInfo.from(consJsonStr);
         assertEquals(1, consumerInfos.length);
         consumerInfo = consumerInfos[0];
         assertNotNull(consumerInfo.getConsumerID());
         assertEquals(connectionID, consumerInfo.getConnectionID());
         assertEquals(queue.getQueueName(), consumerInfo.getDestinationName());
         assertEquals("queue", consumerInfo.getDestinationType());
         assertNull(consumerInfo.getFilter());
         assertEquals(true, consumerInfo.isBrowseOnly());
         assertEquals(false, consumerInfo.isDurable());
         assertTrue(startTime <= consumerInfo.getCreationTime() && consumerInfo.getCreationTime() <= System.currentTimeMillis());

         browser.close();
        
         // create a regular consumer w/ filter on a temp topic
         String filter = "color = 'red'";
         consumer = session.createConsumer(temporaryTopic, filter);
        
         consJsonStr = control.listConsumersAsJSON(connectionID);
         assertNotNull(consJsonStr);
         System.out.println(consJsonStr);
         consumerInfos = JMSConsumerInfo.from(consJsonStr);
         assertEquals(1, consumerInfos.length);
         consumerInfo = consumerInfos[0];
         assertNotNull(consumerInfo.getConsumerID());
         assertEquals(connectionID, consumerInfo.getConnectionID());
         assertEquals(temporaryTopic.getTopicName(), consumerInfo.getDestinationName());
         assertEquals("temptopic", consumerInfo.getDestinationType());
         assertEquals(filter, consumerInfo.getFilter());
         assertEquals(false, consumerInfo.isBrowseOnly());
         assertEquals(false, consumerInfo.isDurable());
         assertTrue(startTime <= consumerInfo.getCreationTime() && consumerInfo.getCreationTime() <= System.currentTimeMillis());

         consumer.close();

         connection.close();
      }
      finally
      {
         if (serverManager != null)
         {
            serverManager.destroyQueue(queueName);
            serverManager.stop();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }

   /**
    * test for durable subscriber
    */
   public void testListConsumersAsJSON2() throws Exception
   {
      String topicName = RandomUtil.randomString();
      String clientID = RandomUtil.randomString();
      String subName = RandomUtil.randomString();

      try
      {
         startHornetQServer(NettyAcceptorFactory.class.getName());
         serverManager.createTopic(false, topicName, topicName);
         Topic topic = HornetQJMSClient.createTopic(topicName);
        
         JMSServerControl control = createManagementControl();

         long startTime = System.currentTimeMillis();
        
         String jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
         JMSConnectionInfo[] infos = JMSConnectionInfo.from(jsonStr);
         assertEquals(0, infos.length);

         ConnectionFactory cf1 = JMSUtil.createFactory(NettyConnectorFactory.class.getName(),
                                                       JMSServerControl2Test.CONNECTION_TTL,
                                                       JMSServerControl2Test.PING_PERIOD);
         Connection connection = cf1.createConnection();
         connection.setClientID(clientID);
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

         // create a durable subscriber
         MessageConsumer consumer = session.createDurableSubscriber(topic, subName);
        
         jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
         infos = JMSConnectionInfo.from(jsonStr);
         assertEquals(1, infos.length);
         String connectionID = infos[0].getConnectionID();
        
         String consJsonStr = control.listConsumersAsJSON(connectionID);
         assertNotNull(consJsonStr);
         JMSConsumerInfo[] consumerInfos = JMSConsumerInfo.from(consJsonStr);
         assertEquals(1, consumerInfos.length);
         JMSConsumerInfo consumerInfo = consumerInfos[0];
         assertNotNull(consumerInfo.getConsumerID());
         assertEquals(connectionID, consumerInfo.getConnectionID());
         assertEquals(topic.getTopicName(), consumerInfo.getDestinationName());
         assertEquals("topic", consumerInfo.getDestinationType());
         assertNull(consumerInfo.getFilter());
         assertEquals(false, consumerInfo.isBrowseOnly());
         assertEquals(true, consumerInfo.isDurable());
         assertTrue(startTime <= consumerInfo.getCreationTime() && consumerInfo.getCreationTime() <= System.currentTimeMillis());

         consumer.close();

         connection.close();
      }
      finally
      {
         if (serverManager != null)
         {
            serverManager.destroyTopic(topicName);
            serverManager.stop();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }
  
   //https://jira.jboss.org/browse/HORNETQ-416
   public void testProducerInfo() throws Exception
   {
      String queueName = RandomUtil.randomString();

      System.out.println("queueName is: " + queueName);
     
      Connection connection = null;
     
      try
      {
         startHornetQServer(NettyAcceptorFactory.class.getName());
         serverManager.createQueue(false, queueName, null, true, queueName);
         Queue queue = HornetQJMSClient.createQueue(queueName);

         JMSServerControl control = createManagementControl();

         long startTime = System.currentTimeMillis();

         ConnectionFactory cf1 = JMSUtil.createFactory(NettyConnectorFactory.class.getName(),
                                                       JMSServerControl2Test.CONNECTION_TTL,
                                                       JMSServerControl2Test.PING_PERIOD);
         connection = cf1.createConnection();
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        
         MessageProducer producer = session.createProducer(queue);
        
         TextMessage msgSent = null;
         for (int i = 0; i < 10; i++)
         {
            msgSent = session.createTextMessage("mymessage-" + i);
            producer.send(msgSent);
            System.out.println("sending msgID " + msgSent.getJMSMessageID());
         }
        
        

         connection.start();
        
         // create a regular message consumer
         MessageConsumer consumer = session.createConsumer(queue);

         TextMessage receivedMsg = null;
         for (int i = 0; i < 10; i++)
         {
            receivedMsg = (TextMessage)consumer.receive(3000);
            assertNotNull(receivedMsg);
            System.out.println("receiveMsg: " + receivedMsg);
         }
        
         assertEquals(msgSent.getJMSMessageID(), receivedMsg.getJMSMessageID());
        
         HornetQMessage jmsMessage = (HornetQMessage)receivedMsg;
         String lastMsgID = jmsMessage.getCoreMessage().getUserID().toString();
        
         String jsonStr = control.listConnectionsAsJSON();
         JMSConnectionInfo[] infos = JMSConnectionInfo.from(jsonStr);
        
         JMSConnectionInfo connInfo = infos[0];
        
         String sessionsStr = control.listSessionsAsJSON(connInfo.getConnectionID());
         JMSSessionInfo[] sessInfos = JMSSessionInfo.from(sessionsStr);
        
         assertTrue(sessInfos.length > 0);
         boolean lastMsgFound = false;
         for (JMSSessionInfo sInfo : sessInfos)
         {
            System.out.println("Session name: " + sInfo.getSessionID());
            assertNotNull(sInfo.getSessionID());
            long createTime = sInfo.getCreationTime();
            assertTrue(startTime <= createTime && createTime <= System.currentTimeMillis());
            String lastID = control.getLastSentMessageID(sInfo.getSessionID(), "jms.queue." + queueName);
            if (lastID != null)
            {
               assertEquals(lastMsgID, lastID);
               lastMsgFound = true;
            }
         }
         assertTrue(lastMsgFound);

         consumer.close();

         connection.close();
      }
      catch (Exception e)
      {
         e.printStackTrace();
         throw e;
      }
      finally
      {
         try
         {
            if (connection != null)
            {
               connection.close();
            }
           
            if (serverManager != null)
            {
               serverManager.destroyQueue(queueName);
               serverManager.stop();
            }
         }
         catch (Throwable ignored)
         {
            ignored.printStackTrace();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }
   // Package protected ---------------------------------------------

   // Protected -----------------------------------------------------

   protected JMSServerControl createManagementControl() throws Exception
   {
      return ManagementControlHelper.createJMSServerControl(mbeanServer);
   }

   @Override
   protected void tearDown() throws Exception
   {
      serverManager = null;

      server = null;

      super.tearDown();
   }

   // Private -------------------------------------------------------

   private void doListConnectionIDs(final String acceptorFactory, final String connectorFactory) throws Exception
   {
      try
      {
         startHornetQServer(acceptorFactory);

         JMSServerControl control = createManagementControl();

         Assert.assertEquals(0, control.listConnectionIDs().length);

         ConnectionFactory cf1 = JMSUtil.createFactory(connectorFactory,
                                                       JMSServerControl2Test.CONNECTION_TTL,
                                                       JMSServerControl2Test.PING_PERIOD);
         Connection connection = cf1.createConnection();

         String[] connectionIDs = control.listConnectionIDs();
         Assert.assertEquals(1, connectionIDs.length);

         ConnectionFactory cf2 = JMSUtil.createFactory(connectorFactory,
                                                       JMSServerControl2Test.CONNECTION_TTL,
                                                       JMSServerControl2Test.PING_PERIOD);
         Connection connection2 = cf2.createConnection();
         Assert.assertEquals(2, control.listConnectionIDs().length);

         connection.close();

         waitForConnectionIDs(1, control);

         connection2.close();

         waitForConnectionIDs(0, control);
      }
      finally
      {
         if (serverManager != null)
         {
            serverManager.stop();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }

   private void doListConnectionsAsJSON(final String acceptorFactory, final String connectorFactory) throws Exception
   {
      try
      {
         startHornetQServer(acceptorFactory);

         JMSServerControl control = createManagementControl();

         long startTime = System.currentTimeMillis();
        
         String jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
         JMSConnectionInfo[] infos = JMSConnectionInfo.from(jsonStr);
         assertEquals(0, infos.length);

         ConnectionFactory cf1 = JMSUtil.createFactory(connectorFactory,
                                                       JMSServerControl2Test.CONNECTION_TTL,
                                                       JMSServerControl2Test.PING_PERIOD);
         Connection connection = cf1.createConnection();

         jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
         infos = JMSConnectionInfo.from(jsonStr);
         assertEquals(1, infos.length);
         for (JMSConnectionInfo info : infos)
         {
            assertNotNull(info.getConnectionID());
            assertNotNull(info.getClientAddress());
            assertTrue(startTime <= info.getCreationTime() && info.getCreationTime() <= System.currentTimeMillis());
         }

         ConnectionFactory cf2 = JMSUtil.createFactory(connectorFactory,
                                                       JMSServerControl2Test.CONNECTION_TTL,
                                                       JMSServerControl2Test.PING_PERIOD);
         Connection connection2 = cf2.createConnection();

         jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
         infos = JMSConnectionInfo.from(jsonStr);
         assertEquals(2, infos.length);
         for (JMSConnectionInfo info : infos)
         {
            assertNotNull(info.getConnectionID());
            assertNotNull(info.getClientAddress());
            assertTrue(startTime <= info.getCreationTime() && info.getCreationTime() <= System.currentTimeMillis());
            assertNull(info.getClientID());
            assertNull(info.getUsername());
         }

         connection.close();

         waitForConnectionIDs(1, control);

         connection2.close();

         waitForConnectionIDs(0, control);
        
         Connection connection3 = cf2.createConnection("guest", "guest");
         connection3.setClientID("MyClient");
        
         jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
        
         infos = JMSConnectionInfo.from(jsonStr);
         JMSConnectionInfo info = infos[0];
         assertEquals("MyClient", info.getClientID());
         assertEquals("guest", info.getUsername());
        
         connection3.close();
        
         jsonStr = control.listConnectionsAsJSON();
         assertNotNull(jsonStr);
         infos = JMSConnectionInfo.from(jsonStr);
         assertEquals(0, infos.length);
      }
      finally
      {
         if (serverManager != null)
         {
            serverManager.stop();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }
  
   private void waitForConnectionIDs(final int num, final JMSServerControl control) throws Exception
   {
      final long timeout = 10000;
      long start = System.currentTimeMillis();
      while (true)
      {
         if (control.listConnectionIDs().length == num)
         {
            return;
         }

         if (System.currentTimeMillis() - start > timeout)
         {
            throw new IllegalStateException("Timed out waiting for number of connections");
         }

         Thread.sleep(10);
      }
   }

   private void doListSessions(final String acceptorFactory, final String connectorFactory) throws Exception
   {
      try
      {
         startHornetQServer(acceptorFactory);

         JMSServerControl control = createManagementControl();

         Assert.assertEquals(0, control.listConnectionIDs().length);

         ConnectionFactory cf = JMSUtil.createFactory(connectorFactory,
                                                      JMSServerControl2Test.CONNECTION_TTL,
                                                      JMSServerControl2Test.PING_PERIOD);
         Connection connection = cf.createConnection();

         String[] connectionIDs = control.listConnectionIDs();
         Assert.assertEquals(1, connectionIDs.length);
         String connectionID = connectionIDs[0];

         String[] sessions = control.listSessions(connectionID);
         Assert.assertEquals(1, sessions.length);
         connection.close();
         sessions = control.listSessions(connectionID);
         Assert.assertEquals("got " + Arrays.asList(sessions), 0, sessions.length);

         connection.close();

         waitForConnectionIDs(0, control);

         Assert.assertEquals(0, control.listConnectionIDs().length);
      }
      finally
      {
         if (serverManager != null)
         {
            serverManager.stop();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }

   private void doListClientConnections(final String acceptorFactory, final String connectorFactory) throws Exception
   {
      try
      {
         startHornetQServer(acceptorFactory);

         JMSServerControl control = createManagementControl();

         Assert.assertEquals(0, control.listRemoteAddresses().length);

         ConnectionFactory cf = JMSUtil.createFactory(connectorFactory,
                                                      JMSServerControl2Test.CONNECTION_TTL,
                                                      JMSServerControl2Test.PING_PERIOD);
         Connection connection = cf.createConnection();

         String[] remoteAddresses = control.listRemoteAddresses();
         Assert.assertEquals(1, remoteAddresses.length);

         for (String remoteAddress : remoteAddresses)
         {
            System.out.println(remoteAddress);
         }

         connection.close();

         waitForConnectionIDs(0, control);

         remoteAddresses = control.listRemoteAddresses();
         Assert.assertEquals("got " + Arrays.asList(remoteAddresses), 0, remoteAddresses.length);
      }
      finally
      {
         if (serverManager != null)
         {
            serverManager.stop();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }

   private void doCloseConnectionsForAddress(final String acceptorFactory, final String connectorFactory) throws Exception
   {
      try
      {
         startHornetQServer(acceptorFactory);

         JMSServerControl control = createManagementControl();

         Assert.assertEquals(0, server.getConnectionCount());
         Assert.assertEquals(0, control.listRemoteAddresses().length);

         ConnectionFactory cf = JMSUtil.createFactory(connectorFactory,
                                                      JMSServerControl2Test.CONNECTION_TTL,
                                                      JMSServerControl2Test.PING_PERIOD);
         Connection connection = cf.createConnection();

         Assert.assertEquals(1, server.getConnectionCount());

         String[] remoteAddresses = control.listRemoteAddresses();
         Assert.assertEquals(1, remoteAddresses.length);
         String remoteAddress = remoteAddresses[0];

         final CountDownLatch exceptionLatch = new CountDownLatch(1);
         connection.setExceptionListener(new ExceptionListener()
         {
            public void onException(final JMSException e)
            {
               exceptionLatch.countDown();
            }
         });

         Assert.assertTrue(control.closeConnectionsForAddress(remoteAddress));

         boolean gotException = exceptionLatch.await(2 * JMSServerControl2Test.CONNECTION_TTL, TimeUnit.MILLISECONDS);
         Assert.assertTrue("did not received the expected JMSException", gotException);

         remoteAddresses = control.listRemoteAddresses();
         Assert.assertEquals("got " + Arrays.asList(remoteAddresses), 0, remoteAddresses.length);
         Assert.assertEquals(0, server.getConnectionCount());

         connection.close();
      }
      finally
      {
         if (serverManager != null)
         {
            serverManager.stop();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }

   private void doCloseConnectionsForUnknownAddress(final String acceptorFactory, final String connectorFactory) throws Exception
   {
      String unknownAddress = RandomUtil.randomString();

      try
      {
         startHornetQServer(acceptorFactory);

         JMSServerControl control = createManagementControl();

         Assert.assertEquals(0, server.getConnectionCount());
         Assert.assertEquals(0, control.listRemoteAddresses().length);

         ConnectionFactory cf = JMSUtil.createFactory(connectorFactory,
                                                      JMSServerControl2Test.CONNECTION_TTL,
                                                      JMSServerControl2Test.PING_PERIOD);
         Connection connection = cf.createConnection();

         Assert.assertEquals(1, server.getConnectionCount());
         String[] remoteAddresses = control.listRemoteAddresses();
         Assert.assertEquals(1, remoteAddresses.length);

         final CountDownLatch exceptionLatch = new CountDownLatch(1);
         connection.setExceptionListener(new ExceptionListener()
         {
            public void onException(final JMSException e)
            {
               exceptionLatch.countDown();
            }
         });

         Assert.assertFalse(control.closeConnectionsForAddress(unknownAddress));

         boolean gotException = exceptionLatch.await(2 * JMSServerControl2Test.CONNECTION_TTL, TimeUnit.MILLISECONDS);
         Assert.assertFalse(gotException);

         Assert.assertEquals(1, control.listRemoteAddresses().length);
         Assert.assertEquals(1, server.getConnectionCount());

         connection.close();

      }
      finally
      {
         if (serverManager != null)
         {
            serverManager.stop();
         }

         if (server != null)
         {
            server.stop();
         }
      }
   }

   // Inner classes -------------------------------------------------

}
TOP

Related Classes of org.hornetq.tests.integration.jms.server.management.JMSServerControl2Test

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.