client.disconnect();
   public synchronized Object invoke(Invocation invocation) throws Throwable   {      MethodInvocation mi = (MethodInvocation)invocation;      String methodName = mi.getMethod().getName();      if (trace) { log.trace("invoking " + methodName + " on server"); }      SimpleMetaData md = mi.getMetaData();      md.addMetaData(Dispatcher.DISPATCHER,                     Dispatcher.OID,                     new Integer(id),                     PayloadKey.AS_IS);            /*       * If the method being invoked is createConnectionDelegate then we must invoke it on the same       * remoting client subsequently used by the connection.       * This is because we need to pass in the remoting session id in the call to createConnection       * All other invocations can be invoked on an arbitrary client, which can be created for each invocation.       * If we disable pinging on the client then it is a reasonably light weight operation to create the client       * since it will use the already existing invoker.       * This prevents us from having to maintain a Client instance per connection factory, which gives       * difficulties in knowing when to close it.       */      Client client;             JMSRemotingConnection remotingConnection = null;      if ("createConnectionDelegate".equals(methodName))      {         //Create a new connection                  remotingConnection = new JMSRemotingConnection(serverLocatorURI, clientPing);                  client = remotingConnection.getInvokingClient();                  md.addMetaData(MetaDataConstants.JMS,                        MetaDataConstants.REMOTING_SESSION_ID,                        client.getSessionId(),                        PayloadKey.AS_IS);         md.addMetaData(MetaDataConstants.JMS,                        MetaDataConstants.JMS_CLIENT_VM_ID,                        JMSClientVMIdentifier.instance,                        PayloadKey.AS_IS);      }      else      {         //Create a client - make sure pinging is off                  Map configuration = new HashMap();         configuration.put(Client.ENABLE_LEASE, String.valueOf(false));         client = new Client(new InvokerLocator(serverLocatorURI), configuration);                       client.setSubsystem(ServerPeer.REMOTING_JMS_SUBSYSTEM);                  client.connect();                           client.setMarshaller(new JMSWireFormat());         client.setUnMarshaller(new JMSWireFormat());               }      byte v = getVersionToUse().getProviderIncrementingVersion();            MessagingMarshallable request = new MessagingMarshallable(v, mi);            MessagingMarshallable response;      try      {         response = (MessagingMarshallable)client.invoke(request, null);                  if (trace) { log.trace("got server response for " + methodName); }      }      catch (Throwable t)      {         //If we were invoking createConnectionDelegate and failure occurs then we need to clear         //up the JMSRemotingConnection                  if (remotingConnection != null)         {            try            {               remotingConnection.close();            }            catch (Throwable ignore)            {                           }         }                  throw t;      }      finally      {         if (remotingConnection == null)         {            //Not a call to createConnectionDelegate - disconnect the client                        client.disconnect();         }      }      Object ret = response.getLoad();            if (remotingConnection != null)      {         //It was a call to createConnectionDelegate - set the remoting connection to use         ClientConnectionDelegate connectionDelegate = (ClientConnectionDelegate)ret;                  connectionDelegate.setRemotingConnection(remotingConnection);      }      return ret;   }