示例#1
0
  public abstract static class DefaultContextImpl implements Context {
    private static final String durationProp =
        System.getProperty(DefaultContextImpl.class.getName() + ".durationMillis");
    private static final long DURATION =
        durationProp != null ? Long.parseLong(durationProp) : 60_000L;
    private final ReentrantLock lock = new ReentrantLock();
    private final long created;
    private final Map<String, Object> attachments = new HashMap<>();

    private boolean valid = true;

    public DefaultContextImpl() {
      created = new Date().getTime();
    }

    @Override
    public void invalidate() {
      attachments.clear();
      valid = false;
    }

    @Override
    public final boolean isValid() {
      final boolean ret = valid && (new Date().getTime() - created) <= DURATION;
      if (!ret) invalidate();
      return ret;
    }

    @Override
    public final Map<String, Object> getAttachments() {
      return attachments;
    }

    @Override
    public final ReentrantLock getLock() {
      return lock;
    }

    @Override
    public boolean watch() {
      return true;
    }
  }
示例#2
0
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

      messageReceiveCount++;
      if (msg instanceof HttpResponse) {
        try {
          HttpResponse response = (HttpResponse) msg;

          StringBuilder sb = new StringBuilder();
          if (LOG.isDebugEnabled()) {
            sb.append("STATUS: ")
                .append(response.getStatus())
                .append(", VERSION: ")
                .append(response.getProtocolVersion())
                .append(", HEADER: ");
          }
          if (!response.headers().names().isEmpty()) {
            for (String name : response.headers().names()) {
              for (String value : response.headers().getAll(name)) {
                if (LOG.isDebugEnabled()) {
                  sb.append(name).append(" = ").append(value);
                }
                if (this.length == -1 && name.equals("Content-Length")) {
                  this.length = Long.parseLong(value);
                }
              }
            }
          }
          if (LOG.isDebugEnabled()) {
            LOG.debug(sb.toString());
          }

          if (response.getStatus().code() == HttpResponseStatus.NO_CONTENT.code()) {
            LOG.warn("There are no data corresponding to the request");
            length = 0;
            return;
          } else if (response.getStatus().code() != HttpResponseStatus.OK.code()) {
            LOG.error(response.getStatus().reasonPhrase());
            state = TajoProtos.FetcherState.FETCH_FAILED;
            return;
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }

      if (msg instanceof HttpContent) {
        try {
          HttpContent httpContent = (HttpContent) msg;
          ByteBuf content = httpContent.content();
          if (content.isReadable()) {
            content.readBytes(fc, content.readableBytes());
          }

          if (msg instanceof LastHttpContent) {
            if (raf != null) {
              fileLen = file.length();
            }

            finishTime = System.currentTimeMillis();
            if (state != TajoProtos.FetcherState.FETCH_FAILED) {
              state = TajoProtos.FetcherState.FETCH_FINISHED;
            }

            IOUtils.cleanup(LOG, fc, raf);
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }
    }