예제 #1
0
  // 模拟创建一个终结节点
  protected void initTermin(Long processId) {
    TerminEventData data = new TerminEventData();
    data.setPipelineId(pipelineId);
    data.setProcessId(processId);
    data.setType(TerminType.NORMAL);

    byte[] bytes = JsonUtils.marshalToByte(data);
    zookeeper.create(StagePathUtils.getTermin(pipelineId, processId), bytes, CreateMode.PERSISTENT);
  }
  @Test
  public void testDemo() {
    // 设置启动标志
    channelEvent.start(channelId);
    sleep(); // 停顿一下

    // 优先启动主导线程
    mainStem.submit(pipelineId);

    PermitMonitor permit = ArbitrateFactory.getInstance(pipelineId, PermitMonitor.class);
    try {
      permit.waitForPermit(); // 阻塞等待授权
    } catch (InterruptedException e1) {
      want.fail();
    }

    // 启动
    select.submit(pipelineId);
    extract.submit(pipelineId);
    view.submit(pipelineId);
    this.termin.submit(pipelineId);

    transform.submit(oppositePipelineId); // 注意是反方向的
    load.submit(oppositePipelineId); // 注意是反方向的

    try {
      System.in.read();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // 发送结束事件
    TerminEventData termin = new TerminEventData();
    termin.setPipelineId(pipelineId);
    termin.setType(TerminType.SHUTDOWN);
    arbitrateEventService.terminEvent().single(termin);
    sleep(5 * 1000L); // 等待处理完所有的termin事件

    // 关闭
    select.destory(pipelineId);
    extract.destory(pipelineId);
    view.destory(pipelineId);
    this.termin.destory(pipelineId);

    transform.destory(oppositePipelineId);
    load.destory(oppositePipelineId);
    ArbitrateFactory.destory(pipelineId);
  }