@Override
  public void initialize() throws HyracksDataException {
    try {
      writer.open();
      switch (message.getMessageType()) {
        case END:
          EndFeedMessage endFeedMessage = (EndFeedMessage) message;
          switch (endFeedMessage.getEndMessageType()) {
            case DISCONNECT_FEED:
              hanldeDisconnectFeedTypeMessage(endFeedMessage);
              break;
            case DISCONTINUE_SOURCE:
              handleDiscontinueFeedTypeMessage(endFeedMessage);
              break;
          }
          break;
        case PREPARE_STALL:
          {
            handlePrepareStallMessage((PrepareStallMessage) message);
            break;
          }
        case TERMINATE_FLOW:
          {
            FeedConnectionId connectionId = ((TerminateDataFlowMessage) message).getConnectionId();
            handleTerminateFlowMessage(connectionId);
            break;
          }
        case COMMIT_ACK_RESPONSE:
          {
            handleFeedTupleCommitResponseMessage((FeedTupleCommitResponseMessage) message);
            break;
          }
        case THROTTLING_ENABLED:
          {
            handleThrottlingEnabledMessage((ThrottlingEnabledFeedMessage) message);
            break;
          }
        default:
          break;
      }

    } catch (Exception e) {
      throw new HyracksDataException(e);
    } finally {
      writer.close();
    }
  }
 private void handleDiscontinueFeedTypeMessage(EndFeedMessage endFeedMessage) throws Exception {
   FeedId sourceFeedId = endFeedMessage.getSourceFeedId();
   SubscribableFeedRuntimeId subscribableRuntimeId =
       new SubscribableFeedRuntimeId(sourceFeedId, FeedRuntimeType.INTAKE, partition);
   ISubscribableRuntime feedRuntime =
       feedManager.getFeedSubscriptionManager().getSubscribableRuntime(subscribableRuntimeId);
   IAdapterRuntimeManager adapterRuntimeManager =
       ((IngestionRuntime) feedRuntime).getAdapterRuntimeManager();
   adapterRuntimeManager.stop();
   if (LOGGER.isLoggable(Level.INFO)) {
     LOGGER.info("Stopped Adapter " + adapterRuntimeManager);
   }
 }
  private void hanldeDisconnectFeedTypeMessage(EndFeedMessage endFeedMessage) throws Exception {
    if (LOGGER.isLoggable(Level.INFO)) {
      LOGGER.info("Ending feed:" + endFeedMessage.getFeedConnectionId());
    }
    FeedRuntimeId runtimeId = null;
    FeedRuntimeType subscribableRuntimeType = ((EndFeedMessage) message).getSourceRuntimeType();
    if (endFeedMessage.isCompleteDisconnection()) {
      // subscribableRuntimeType represents the location at which the feed connection receives data
      FeedRuntimeType runtimeType = null;
      switch (subscribableRuntimeType) {
        case INTAKE:
          runtimeType = FeedRuntimeType.COLLECT;
          break;
        case COMPUTE:
          runtimeType = FeedRuntimeType.COMPUTE_COLLECT;
          break;
        default:
          throw new IllegalStateException(
              "Invalid subscribable runtime type " + subscribableRuntimeType);
      }

      runtimeId = new FeedRuntimeId(runtimeType, partition, FeedRuntimeId.DEFAULT_OPERAND_ID);
      CollectionRuntime feedRuntime =
          (CollectionRuntime)
              feedManager.getFeedConnectionManager().getFeedRuntime(connectionId, runtimeId);
      feedRuntime.getSourceRuntime().unsubscribeFeed(feedRuntime);
      if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("Complete Unsubscription of " + endFeedMessage.getFeedConnectionId());
      }
    } else {
      // subscribaleRuntimeType represents the location for data hand-off in presence of subscribers
      switch (subscribableRuntimeType) {
        case INTAKE:
          // illegal state as data hand-off from one feed to another does not happen at intake
          throw new IllegalStateException(
              "Illegal State, invalid runtime type  " + subscribableRuntimeType);
        case COMPUTE:
          // feed could be primary or secondary, doesn't matter
          SubscribableFeedRuntimeId feedSubscribableRuntimeId =
              new SubscribableFeedRuntimeId(
                  connectionId.getFeedId(), FeedRuntimeType.COMPUTE, partition);
          ISubscribableRuntime feedRuntime =
              feedManager
                  .getFeedSubscriptionManager()
                  .getSubscribableRuntime(feedSubscribableRuntimeId);
          DistributeFeedFrameWriter dWriter =
              (DistributeFeedFrameWriter) feedRuntime.getFeedFrameWriter();
          Map<IFrameWriter, FeedFrameCollector> registeredCollectors =
              dWriter.getRegisteredReaders();

          IFrameWriter unsubscribingWriter = null;
          for (Entry<IFrameWriter, FeedFrameCollector> entry : registeredCollectors.entrySet()) {
            IFrameWriter frameWriter = entry.getKey();
            FeedRuntimeInputHandler feedFrameWriter = (FeedRuntimeInputHandler) frameWriter;
            if (feedFrameWriter.getConnectionId().equals(endFeedMessage.getFeedConnectionId())) {
              unsubscribingWriter = feedFrameWriter;
              dWriter.unsubscribeFeed(unsubscribingWriter);
              if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.info("Partial Unsubscription of " + unsubscribingWriter);
              }
              break;
            }
          }
          break;
      }
    }

    if (LOGGER.isLoggable(Level.INFO)) {
      LOGGER.info("Unsubscribed from feed :" + connectionId);
    }
  }