Package org.serviceconnector.cmd

Examples of org.serviceconnector.cmd.SCMPCommandException


  public synchronized void allocateServerAndCreateSession(SCMPMessage msgToForward, CreateSessionCommandCallback callback,
      Session session, int timeoutMillis) throws Exception {
    int numberOfServer = this.listOfServers.size();
    if (numberOfServer == 0) {
      // no server registered for this service
      SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.NO_SERVER, "service="
          + msgToForward.getServiceName());
      scmpCommandException.setMessageType(msgToForward.getMessageType());
      throw scmpCommandException;
    }
    for (int i = 0; i < numberOfServer; i++) {
      this.serverIndex++;
      if (this.serverIndex >= numberOfServer) {
View Full Code Here


    int otiOnSCMillis = (int) (oti * basicConf.getOperationTimeoutMultiplier());
    String sessionId = reqMessage.getSessionId();
    Session session = this.getSessionById(sessionId);
    if (session.hasPendingRequest() == true) {
      LOGGER.warn("session " + sessionId + " has pending request");
      SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.PARALLEL_REQUEST, "service="
          + reqMessage.getServiceName() + " sid=" + sessionId);
      scmpCommandException.setMessageType(this.getKey());
      throw scmpCommandException;
    }
    // sets the time of last execution
    session.resetExecuteTime();
    synchronized (session) {
      session.setPendingRequest(true); // IMPORTANT - set true before reset timeout - because of parallel echo call
      // reset session timeout to OTI+ECI - during wait for server reply
      this.sessionRegistry.resetSessionTimeout(session, (otiOnSCMillis + session.getSessionTimeoutMillis()));
    }

    if (cacheManager.isCacheEnabled()) {
      try {
        // try to load response from cache
        SCMPMessage message = cacheManager.tryGetMessageFromCacheOrLoad(reqMessage);
        if (message != null) {
          synchronized (session) {
            // reset session timeout to ECI
            this.sessionRegistry.resetSessionTimeout(session, session.getSessionTimeoutMillis());
            session.setPendingRequest(false); // IMPORTANT - set false after reset timeout - parallel echo call
          }
          // message found in cache - hand it to the client
          response.setSCMP(message);
          responderCallback.responseCallback(request, response);
          return;
        }
      } catch (Exception e) {
        synchronized (session) {
          // reset session timeout to ECI
          this.sessionRegistry.resetSessionTimeout(session, session.getSessionTimeoutMillis());
          session.setPendingRequest(false); // IMPORTANT - set false after reset timeout - because of parallel echo call
        }
        throw e;
      }
    }
    ExecuteCommandCallback callback = null;
    StatefulServer server = session.getStatefulServer();
    int tries = (otiOnSCMillis / Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
    // Following loop implements the wait mechanism in case of a busy connection pool
    int i = 0;
    do {
      // reset msgType, might have been modified in below execute try
      reqMessage.setMessageType(this.getKey());
      callback = new ExecuteCommandCallback(request, response, responderCallback, sessionId);
      try {
        server.execute(reqMessage, callback, otiOnSCMillis - (i * Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS));
        // no exception has been thrown - get out of wait loop
        break;
      } catch (ConnectionPoolBusyException ex) {
        LOGGER.debug("ConnectionPoolBusyException caught in wait mec of execute, tries left=" + tries);
        if (i >= (tries - 1)) {
          // only one loop outstanding - don't continue throw current exception
          synchronized (session) {
            // reset session timeout to ECI
            this.sessionRegistry.resetSessionTimeout(session, session.getSessionTimeoutMillis());
            session.setPendingRequest(false); // IMPORTANT - set false after timeout - because of parallel echo call
          }
          LOGGER.debug(SCMPError.NO_FREE_CONNECTION.getErrorText("service=" + reqMessage.getServiceName()));
          SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.NO_FREE_CONNECTION, "service="
              + reqMessage.getServiceName());
          scmpCommandException.setMessageType(this.getKey());
          throw scmpCommandException;
        }
      }
      // sleep for a while and then try again
      Thread.sleep(Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
View Full Code Here

  public synchronized void allocateServerAndSubscribe(SCMPMessage msgToForward, SubscribeCommandCallback callback,
      Subscription subscription, int timeoutMillis) throws Exception {
    int numberOfServer = this.listOfServers.size();
    if (numberOfServer == 0) {
      // no server registered for this service
      SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.NO_SERVER, "service="
          + msgToForward.getServiceName());
      scmpCommandException.setMessageType(msgToForward.getMessageType());
      throw scmpCommandException;
    }
    for (int i = 0; i < numberOfServer; i++) {
      this.serverIndex++;
      if (this.serverIndex >= numberOfServer) {
View Full Code Here

    // Check the presence of the server
    Server server = this.serverRegistry.getServer(serverKey);
    if (server == null) {
      // server does not exist =>
      SCMPCommandException cmdExc = new SCMPCommandException(SCMPError.NO_SERVER, "server=" + serverKey);
      cmdExc.setMessageType(getKey());
      throw cmdExc;
    }
    // reset server timeout
    LOGGER.debug("refresh server timeout in CRG server=" + server.getServerKey() + " timeout(ms)=" + server.getServerTimeoutMillis());
    serverRegistry.resetServerTimeout(server, server.getServerTimeoutMillis());
View Full Code Here

      responderCallback.responseCallback(request, response);
      return;
    case CASCADED_SC:
    case UNDEFINED:
    default:
      throw new SCMPCommandException(SCMPError.SC_ERROR, "delete session not allowed for service "
          + abstractService.getName());
    }
    StatefulServer statefulServer = (StatefulServer) abstractServer;
    DeleteSessionCommandCallback callback;
    // free server from session
    statefulServer.removeSession(session);

    int otiOnSCMillis = (int) (oti * basicConf.getOperationTimeoutMultiplier());
    int tries = (otiOnSCMillis / Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
    // Following loop implements the wait mechanism in case of a busy connection pool
    int i = 0;
    do {
      // reset msgType, might have been modified in below delete session try
      reqMessage.setMessageType(this.getKey());
      callback = new DeleteSessionCommandCallback(request, response, responderCallback, session, statefulServer);
      try {
        statefulServer.deleteSession(reqMessage, callback, otiOnSCMillis
            - (i * Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS));
        // no exception has been thrown - get out of wait loop
        break;
      } catch (ConnectionPoolBusyException ex) {
        LOGGER.debug("ConnectionPoolBusyException caught in wait mec of delete session, tries left=" + tries);
        if (i >= (tries - 1)) {
          // only one loop outstanding - don't continue throw current exception
          statefulServer.abortSession(session, "deleting session failed, connection pool to server busy");
          LOGGER.debug(SCMPError.NO_FREE_CONNECTION.getErrorText("service=" + reqMessage.getServiceName()));
          SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.NO_FREE_CONNECTION, "service="
              + reqMessage.getServiceName());
          scmpCommandException.setMessageType(this.getKey());
          throw scmpCommandException;
        }
      }
      // sleep for a while and then try again
      Thread.sleep(Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
View Full Code Here

    String serviceName = reqMessage.getServiceName();

    // check service is present and enabled
    Service abstractService = this.getService(serviceName);
    if (abstractService.isEnabled() == false) {
      SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.SERVICE_DISABLED, "service="
          + abstractService.getName() + " is disabled");
      scmpCommandException.setMessageType(getKey());
      throw scmpCommandException;
    }

    // enhance ipAddressList
    String ipAddressList = reqMessage.getHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST);
    ipAddressList = ipAddressList + Constants.SLASH + request.getRemoteSocketAddress().getAddress().getHostAddress();
    reqMessage.setHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST, ipAddressList);
    int oti = reqMessage.getHeaderInt(SCMPHeaderAttributeKey.OPERATION_TIMEOUT);

    String mask = reqMessage.getHeader(SCMPHeaderAttributeKey.MASK);
    SubscriptionMask subscriptionMask = new SubscriptionMask(mask);
    String sessionInfo = reqMessage.getHeader(SCMPHeaderAttributeKey.SESSION_INFO);
    int noiInSecs = reqMessage.getHeaderInt(SCMPHeaderAttributeKey.NO_DATA_INTERVAL);
    int noiInMillis = noiInSecs * Constants.SEC_TO_MILLISEC_FACTOR;
    // create temporary subscription
    Subscription tmpSubscription = new Subscription(subscriptionMask, sessionInfo, ipAddressList, noiInMillis, AppContext
        .getBasicConfiguration().getSubscriptionTimeoutMillis(), false);
    tmpSubscription.setService(abstractService);
    reqMessage.setSessionId(tmpSubscription.getId());

    switch (abstractService.getType()) {
    case CASCADED_PUBLISH_SERVICE:
      // publish service is cascaded
      CascadedPublishService cascadedPublishService = (CascadedPublishService) abstractService;
      CascadedSC cascadedSC = cascadedPublishService.getCascadedSC();
      // add server to subscription
      tmpSubscription.setServer(cascadedSC);
      // service is cascaded - subscribe is made by a normal client
      SubscribeCommandCallback callback = new SubscribeCommandCallback(request, response, responderCallback, tmpSubscription);
      cascadedSC.cascadedSCSubscribe(cascadedPublishService.getCascClient(), reqMessage, callback, oti);
      return;
    default:
      // code for other types of services is below
      break;
    }
    // modify message only if it goes to server
    reqMessage.removeHeader(SCMPHeaderAttributeKey.NO_DATA_INTERVAL);
    // check service is present
    PublishService service = this.validatePublishService(abstractService);
    SubscribeCommandCallback callback = null;
    int otiOnSCMillis = (int) (oti * basicConf.getOperationTimeoutMultiplier());
    int tries = (otiOnSCMillis / Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
    int i = 0;
    // Following loop implements the wait mechanism in case of a busy connection pool
    do {
      // reset ipList&msgType, might have been modified in below subscribe try
      reqMessage.setHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST, ipAddressList);
      reqMessage.setMessageType(this.getKey());
      // service is local - normal client is subscribing
      callback = new SubscribeCommandCallback(request, response, responderCallback, tmpSubscription);
      try {
        service.allocateServerAndSubscribe(reqMessage, callback, tmpSubscription, otiOnSCMillis
            - (i * Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS));
        // no exception has been thrown - get out of wait loop
        break;
      } catch (NoFreeServerException ex) {
        LOGGER.debug("NoFreeServerException caught in wait mec of subscribe, tries left=" + tries);
        if (i >= (tries - 1)) {
          // only one loop outstanding - don't continue throw current exception
          throw ex;
        }
      } catch (ConnectionPoolBusyException ex) {
        LOGGER.debug("ConnectionPoolBusyException caught in wait mec of subscribe, tries left=" + tries);
        if (i >= (tries - 1)) {
          // only one loop outstanding - don't continue throw current exception
          LOGGER.warn(SCMPError.NO_FREE_CONNECTION.getErrorText("service=" + reqMessage.getServiceName()));
          SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.NO_FREE_CONNECTION, "service="
              + reqMessage.getServiceName());
          scmpCommandException.setMessageType(this.getKey());
          throw scmpCommandException;
        }
      }
      // sleep for a while and then try again
      Thread.sleep(Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
View Full Code Here

   */
  public StatefulServer getStatefulServerByName(String key) throws SCMPCommandException {
    Server server = this.serverRegistry.getServer(key);
    if (server == null) {
      // server not found in registry
      SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.SERVER_NOT_FOUND, key);
      scmpCommandException.setMessageType(getKey());
      throw scmpCommandException;
    }
    if (server.getType().equals(ServerType.STATEFUL_SERVER) == false) {
      // server is wrong type
      SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.V_WRONG_SERVER_TYPE, key);
      scmpCommandException.setMessageType(getKey());
      throw scmpCommandException;
    }
    return (StatefulServer) server;
  }
View Full Code Here

      FileServer fileServer = session.getFileServer();
      reply = fileServer.serverDownloadFile(session, message, remoteFileName, oti);
    } catch (Exception e) {
      reply = new SCMPMessage();
      SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.FILE_DOWNLOAD_FAILED,
          "Error occured in file server on SC.");
      scmpCommandException.setMessageType(getKey());
      throw scmpCommandException;
    }
    reply.setIsReply(true);
    reply.setMessageType(getKey());
    response.setSCMP(reply);
View Full Code Here

        } catch (ConnectionPoolBusyException ex) {
          LOGGER.warn("ConnectionPoolBusyException caught in wait mec of session abort");
          if (i >= (tries - 1)) {
            // only one loop outstanding - don't continue throw current exception
            LOGGER.warn(SCMPError.NO_FREE_CONNECTION.getErrorText("service=" + abortMessage.getServiceName()));
            SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.NO_FREE_CONNECTION,
                "service=" + abortMessage.getServiceName());
            throw scmpCommandException;
          }
        }
        // sleep for a while and then try again
        Thread.sleep(Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
      } while (++i < tries);

      // validate reply of server
      SCMPMessage reply = callback.getMessageSync(oti);
      if (reply.isFault()) {
        // error in server abort session - destroy server
        this.abortSessionsAndDestroy("Session abort failed, abort reason: " + reason);
      }
    } catch (SCMPCommandException scmpCommandException) {
      LOGGER.warn("ConnectionPoolBusyException in aborting session wait mec " + scmpCommandException.toString());
      // ConnectionPoolBusyException after wait mec - try opening a new connection

      // RemoteNodeConfiguration remoteNodeConfiguration = this.requester.getRemoteNodeConfiguration();
      // set up a new requester to make the SAS - only 1 connection is allowed
      Requester sasRequester = new Requester(this.sasRemoteNodeConfiguration);
View Full Code Here

    SCMPMessage reqMessage = request.getMessage();
    String serviceName = reqMessage.getServiceName();
    // check service is present and enabled
    Service abstractService = this.getService(serviceName);
    if (abstractService.isEnabled() == false) {
      SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.SERVICE_DISABLED, "service="
          + abstractService.getName() + " is disabled");
      scmpCommandException.setMessageType(getKey());
      throw scmpCommandException;
    }

    // enhance ipAddressList
    String ipAddressList = reqMessage.getHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST);
    ipAddressList = ipAddressList + Constants.SLASH + request.getRemoteSocketAddress().getAddress().getHostAddress();
    reqMessage.setHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST, ipAddressList);

    String sessionInfo = reqMessage.getHeader(SCMPHeaderAttributeKey.SESSION_INFO);
    int eciInSeconds = reqMessage.getHeaderInt(SCMPHeaderAttributeKey.ECHO_INTERVAL);
    int eciInMillis = eciInSeconds * Constants.SEC_TO_MILLISEC_FACTOR;
    int oti = reqMessage.getHeaderInt(SCMPHeaderAttributeKey.OPERATION_TIMEOUT);

    switch (abstractService.getType()) {
    case CASCADED_SESSION_SERVICE:
      CascadedSC cascadedSC = ((CascadedSessionService) abstractService).getCascadedSC();
      CommandCascCallback callback = new CommandCascCallback(request, response, responderCallback);
      cascadedSC.createSession(reqMessage, callback, oti);
      return;
    case CASCADED_FILE_SERVICE:
      cascadedSC = ((CascadedFileService) abstractService).getCascadedSC();
      callback = new CommandCascCallback(request, response, responderCallback);
      cascadedSC.createSession(reqMessage, callback, oti);
      return;
    case SESSION_SERVICE:
      // code for type session service is below switch statement
      break;
    case FILE_SERVICE:
      FileService fileService = (FileService) abstractService;
      // create file session
      FileSession fileSession = new FileSession(sessionInfo, ipAddressList, fileService.getPath(),
          fileService.getUploadFileScriptName(), fileService.getGetFileListScriptName());
      fileSession.setService(fileService);
      FileServer fileServer = fileService.allocateFileServerAndCreateSession(fileSession);
      // add server to session
      fileSession.setServer(fileServer);
      fileSession.setSessionTimeoutMillis(eciInMillis * basicConf.getEchoIntervalMultiplier());
      // finally add file session to the registry
      this.sessionRegistry.addSession(fileSession.getId(), fileSession);
      // reply to client
      SCMPMessage reply = new SCMPMessage();
      reply.setIsReply(true);
      reply.setMessageType(getKey());
      reply.setSessionId(fileSession.getId());
      response.setSCMP(reply);
      responderCallback.responseCallback(request, response);
      return;
    default:
      // code for other types of services is below
      break;
    }

    // create session
    Session session = new Session(sessionInfo, ipAddressList);
    session.setService(abstractService);
    session.setSessionTimeoutMillis(eciInMillis * basicConf.getEchoIntervalMultiplier());
    reqMessage.setSessionId(session.getId());
    // no need to forward echo attributes
    reqMessage.removeHeader(SCMPHeaderAttributeKey.ECHO_INTERVAL);

    // tries allocating a server for this session
    CreateSessionCommandCallback callback = null;
    int otiOnSCMillis = (int) (oti * basicConf.getOperationTimeoutMultiplier());
    int tries = (otiOnSCMillis / Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
    // Following loop implements the wait mechanism in case of a busy connection pool
    int i = 0;
    do {
      // reset ipList&msgType, might have been modified in below creates session try
      reqMessage.setHeader(SCMPHeaderAttributeKey.IP_ADDRESS_LIST, ipAddressList);
      reqMessage.setMessageType(this.getKey());
      callback = new CreateSessionCommandCallback(request, response, responderCallback, session);
      try {
        ((SessionService) abstractService).allocateServerAndCreateSession(reqMessage, callback, session, otiOnSCMillis
            - (i * Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS));
        // no exception has been thrown - get out of wait loop
        break;
      } catch (NoFreeServerException ex) {
        LOGGER.debug("NoFreeServerException caught in wait mec of create session, tries left=" + tries);
        if (i >= (tries - 1)) {
          // only one loop outstanding - don't continue throw current exception
          throw ex;
        }
      } catch (ConnectionPoolBusyException ex) {
        LOGGER.debug("ConnectionPoolBusyException caught in wait mec of create session, tries left=" + tries);
        if (i >= (tries - 1)) {
          // only one loop outstanding - don't continue throw current exception
          LOGGER.warn(SCMPError.NO_FREE_CONNECTION.getErrorText("service=" + reqMessage.getServiceName()));
          SCMPCommandException scmpCommandException = new SCMPCommandException(SCMPError.NO_FREE_CONNECTION, "service="
              + reqMessage.getServiceName());
          scmpCommandException.setMessageType(this.getKey());
          throw scmpCommandException;
        }
      }
      // sleep for a while and then try again
      Thread.sleep(Constants.WAIT_FOR_FREE_CONNECTION_INTERVAL_MILLIS);
View Full Code Here

TOP

Related Classes of org.serviceconnector.cmd.SCMPCommandException

Copyright © 2018 www.massapicom. 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.