org.apache.servicecomb.core.Handler Java Examples

The following examples show how to use org.apache.servicecomb.core.Handler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractRestInvocation.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private Holder<Boolean> checkQpsFlowControl(OperationMeta operationMeta) {
  Holder<Boolean> qpsFlowControlReject = new Holder<>(false);
  @SuppressWarnings("deprecation")
  Handler providerQpsFlowControlHandler = operationMeta.getProviderQpsFlowControlHandler();
  if (null != providerQpsFlowControlHandler) {
    try {
      providerQpsFlowControlHandler.handle(invocation, response -> {
        qpsFlowControlReject.value = true;
        produceProcessor = ProduceProcessorManager.INSTANCE.findDefaultJsonProcessor();
        sendResponse(response);
      });
    } catch (Throwable e) {
      LOGGER.error("failed to execute ProviderQpsFlowControlHandler", e);
      qpsFlowControlReject.value = true;
      sendFailResponse(e);
    }
  }
  return qpsFlowControlReject;
}
 
Example #2
Source File: TestAbstractRestInvocation.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void doInvoke(@Mocked Endpoint endpoint, @Mocked OperationMeta operationMeta,
    @Mocked Object[] swaggerArguments, @Mocked SchemaMeta schemaMeta) throws Throwable {
  Response response = Response.ok("ok");
  Handler handler = (invocation, asyncResp) -> asyncResp.complete(response);
  List<Handler> handlerChain = Arrays.asList(handler);
  Deencapsulation.setField(invocation, "handlerList", handlerChain);

  Holder<Response> result = new Holder<>();
  restInvocation = new AbstractRestInvocationForTest() {
    @Override
    protected void sendResponse(Response response) {
      result.value = response;
    }
  };
  restInvocation.invocation = invocation;

  restInvocation.doInvoke();

  Assert.assertSame(response, result.value);
  assertEquals(nanoTime, invocation.getInvocationStageTrace().getStartHandlersRequest());
  assertEquals(nanoTime, invocation.getInvocationStageTrace().getFinishHandlersResponse());
}
 
Example #3
Source File: HighwayServerInvoke.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private Holder<Boolean> checkQpsFlowControl(OperationMeta operationMeta) {
  Holder<Boolean> qpsFlowControlReject = new Holder<>(false);
  @SuppressWarnings("deprecation")
  Handler providerQpsFlowControlHandler = operationMeta.getProviderQpsFlowControlHandler();
  if (null != providerQpsFlowControlHandler) {
    try {
      providerQpsFlowControlHandler.handle(invocation, response -> {
        qpsFlowControlReject.value = true;
        sendResponse(header.getContext(), response);
      });
    } catch (Exception e) {
      LOGGER.error("failed to execute ProviderQpsFlowControlHandler", e);
      qpsFlowControlReject.value = true;
      sendResponse(header.getContext(), Response.providerFailResp(e));
    }
  }
  return qpsFlowControlReject;
}
 
Example #4
Source File: MicroserviceMeta.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
/**
 * Only for JavaChassis internal usage.
 */
@Deprecated
public Handler getProviderQpsFlowControlHandler() {
  if (providerQpsFlowControlHandlerSearched) {
    return providerQpsFlowControlHandler;
  }

  for (Handler handler : handlerChain) {
    // matching by class name is more or less better than importing an extra maven dependency
    if ("org.apache.servicecomb.qps.ProviderQpsFlowControlHandler".equals(handler.getClass().getName())) {
      providerQpsFlowControlHandler = handler;
      break;
    }
  }
  providerQpsFlowControlHandlerSearched = true;
  return providerQpsFlowControlHandler;
}
 
Example #5
Source File: AbstractHandlerManager.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private List<Class<Handler>> convertToChainClass(String chainDef) {
  List<Class<Handler>> result = new ArrayList<>();
  if (StringUtils.isEmpty(chainDef)) {
    return result;
  }

  String[] handlerIds = chainDef.split(",");
  Map<String, Class<Handler>> handlerMaps = config.getHandlerClassMap();
  for (String handlerId : handlerIds) {
    if (handlerId != null) {
      handlerId = handlerId.trim();
    }
    if (StringUtils.isEmpty(handlerId)) {
      continue;
    }

    Class<Handler> cls = handlerMaps.get(handlerId);
    if (cls == null) {
      throw new Error("can not find handler :" + handlerId);
    }
    result.add(cls);
  }
  return result;
}
 
Example #6
Source File: AbstractHandlerManager.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Handler> create(String microserviceName) {
  String handlerChainKey = "servicecomb.handler.chain." + getName() + ".service." + microserviceName;
  String chainDef = DynamicPropertyFactory.getInstance()
      .getStringProperty(handlerChainKey,
          defaultChainDef)
      .get();
  LOGGER.info("get handler chain for [{}]: [{}]", handlerChainKey, chainDef);
  return createHandlerChain(chainDef);
}
 
Example #7
Source File: AbstractHandlerManager.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private List<Handler> createHandlerChain(String chainDef) {
  List<Class<Handler>> chainClasses = convertToChainClass(chainDef);

  List<Handler> handlerList = new ArrayList<>();
  for (Class<Handler> cls : chainClasses) {
    try {
      handlerList.add(cls.newInstance());
    } catch (Exception e) {
      // 在启动阶段直接抛异常出来
      throw new Error(e);
    }
  }
  handlerList.add(getLastHandler());
  return handlerList;
}
 
Example #8
Source File: Config.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public Map<String, Class<Handler>> getHandlerClassMap() {
  return this.handlerClassMap;
}
 
Example #9
Source File: HandlerConfig.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void setClazz(String clazz) throws ClassNotFoundException {
  this.clazz = (Class<Handler>) Class.forName(clazz);
}
 
Example #10
Source File: HandlerConfig.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public void setClazz(Class<Handler> clazz) {
  this.clazz = clazz;
}
 
Example #11
Source File: HandlerConfig.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@JacksonXmlProperty(localName = "class", isAttribute = true)
public Class<Handler> getClazz() {
  return clazz;
}
 
Example #12
Source File: ProducerHandlerManager.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
protected Handler getLastHandler() {
  return ProducerOperationHandler.INSTANCE;
}
 
Example #13
Source File: ConsumerHandlerManager.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
protected Handler getLastHandler() {
  return TransportClientHandler.INSTANCE;
}
 
Example #14
Source File: OperationMeta.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
/**
 * Only for JavaChassis internal usage.
 */
@Deprecated
public Handler getProviderQpsFlowControlHandler() {
  return getMicroserviceMeta().getProviderQpsFlowControlHandler();
}
 
Example #15
Source File: MicroserviceMeta.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public void setHandlerChain(List<Handler> handlerChain) {
  this.handlerChain = handlerChain;
}
 
Example #16
Source File: MicroserviceMeta.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public List<Handler> getHandlerChain() {
  return handlerChain;
}
 
Example #17
Source File: TestAbstractRestInvocation.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void scheduleInvocation_flowControlReject() {
  new Expectations(operationMeta) {
    {
      operationMeta.getProviderQpsFlowControlHandler();
      result = (Handler) (invocation, asyncResp) -> asyncResp.producerFail(new InvocationException(
          new HttpStatus(429, "Too Many Requests"),
          new CommonExceptionData("rejected by qps flowcontrol")));
    }
  };
  Holder<Integer> status = new Holder<>();
  Holder<String> reasonPhrase = new Holder<>();
  Holder<Integer> endCount = new Holder<>(0);
  Holder<String> responseBody = new Holder<>();
  responseEx = new AbstractHttpServletResponse() {
    @SuppressWarnings("deprecation")
    @Override
    public void setStatus(int sc, String sm) {
      status.value = sc;
      reasonPhrase.value = sm;
    }

    @Override
    public void flushBuffer() {
      endCount.value = endCount.value + 1;
    }

    @Override
    public void setContentType(String type) {
      assertEquals("application/json; charset=utf-8", type);
    }

    @Override
    public void setBodyBuffer(Buffer bodyBuffer) {
      responseBody.value = bodyBuffer.toString();
    }
  };
  initRestInvocation();
  restInvocation.scheduleInvocation();

  assertEquals(Integer.valueOf(429), status.value);
  assertEquals("Too Many Requests", reasonPhrase.value);
  assertEquals("{\"message\":\"rejected by qps flowcontrol\"}", responseBody.value);
  assertEquals(Integer.valueOf(1), endCount.value);
}
 
Example #18
Source File: AbstractHandlerManager.java    From servicecomb-java-chassis with Apache License 2.0 votes vote down vote up
protected abstract Handler getLastHandler();