示例#1
0
  public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (SocketWriterMeta) smi;
    data = (SocketWriterData) sdi;

    if (super.init(smi, sdi)) {
      try {
        data.serverSocketPort = Integer.parseInt(environmentSubstitute(meta.getPort()));
        data.serverSocket =
            getTrans()
                .getSocketRepository()
                .openServerSocket(
                    data.serverSocketPort, getTransMeta().getName() + " - " + this.toString());

        return true;
      } catch (Exception e) {
        logError("Error creating server socket: " + e.toString());
        logError(Const.getStackTracker(e));
      }
    }
    return false;
  }
示例#2
0
  public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (SocketWriterMeta) smi;
    data = (SocketWriterData) sdi;

    try {
      if (first) {
        int bufferSize = Const.toInt(environmentSubstitute(meta.getBufferSize()), 1000);

        data.clientSocket = data.serverSocket.accept();

        if (meta.isCompressed()) {
          data.outputStream =
              new DataOutputStream(
                  new BufferedOutputStream(
                      new GZIPOutputStream(data.clientSocket.getOutputStream()), bufferSize));
        } else {
          data.outputStream =
              new DataOutputStream(
                  new BufferedOutputStream(data.clientSocket.getOutputStream(), bufferSize));
        }

        data.flushInterval = Const.toInt(environmentSubstitute(meta.getFlushInterval()), 4000);
      }
    } catch (Exception e) {
      logError("Error accepting from socket : " + e.toString());
      logError("Stack trace: " + Const.CR + Const.getStackTracker(e));

      setErrors(1);
      stopAll();
      setOutputDone();
      if (data.clientSocket != null) {
        try {
          data.clientSocket.shutdownInput();
          data.clientSocket.shutdownOutput();
          data.clientSocket.close();
          logError("Closed connection to SocketWriter");
        } catch (IOException e1) {
          logError("Failed to close connection to SocketWriter");
        }
      }

      return false;
    }

    Object[] r = getRow(); // get row, set busy!
    // Input rowMeta is automatically set, available when needed

    if (r == null) { // no more input to be expected...

      setOutputDone();
      return false;
    }

    try {
      if (first) {
        getInputRowMeta().writeMeta(data.outputStream);
        first = false;
      }
      getInputRowMeta().writeData(data.outputStream, r);
      incrementLinesOutput();

      // flush every X rows
      if (getLinesOutput() > 0
          && data.flushInterval > 0
          && (getLinesOutput() % data.flushInterval) == 0) {
        data.outputStream.flush();
      }

    } catch (Exception e) {
      logError("Error writing to socket : " + e.toString());
      logError("Failing row : " + getInputRowMeta().getString(r));
      logError("Stack trace: " + Const.CR + Const.getStackTracker(e));

      setErrors(1);
      stopAll();
      setOutputDone();
      return false;
    }

    if (checkFeedback(getLinesRead())) {
      logBasic(BaseMessages.getString(PKG, "SocketWriter.Log.LineNumber") + getLinesRead());
    }

    return true;
  }