use of org.apache.servicecomb.swagger.engine.SwaggerProducerOperation in project incubator-servicecomb-java-chassis by apache.
the class TestProducerSchemaFactory method testGetOrCreateProducer.
@Test
public void testGetOrCreateProducer() throws Exception {
OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
Assert.assertEquals("add", operationMeta.getOperationId());
SwaggerProducerOperation producerOperation = operationMeta.getExtData(Const.PRODUCER_OPERATION);
Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
ReflectUtils.setField(addBody, "x", 1);
ReflectUtils.setField(addBody, "y", 2);
Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { addBody }) {
@Override
public String getInvocationQualifiedName() {
return "";
}
};
Holder<Response> holder = new Holder<>();
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(3, (int) holder.value.getResult());
invocation.setSwaggerArguments(new Object[] { 1, 2 });
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(true, holder.value.isFailed());
InvocationException exception = (InvocationException) holder.value.getResult();
CommonExceptionData data = (CommonExceptionData) exception.getErrorData();
Assert.assertEquals("Cse Internal Server Error", data.getMessage());
}
use of org.apache.servicecomb.swagger.engine.SwaggerProducerOperation in project incubator-servicecomb-java-chassis by apache.
the class ProducerOperationHandler method handle.
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
SwaggerProducerOperation producerOperation = invocation.getOperationMeta().getExtData(Const.PRODUCER_OPERATION);
if (producerOperation == null) {
asyncResp.producerFail(ExceptionUtils.producerOperationNotExist(invocation.getSchemaId(), invocation.getOperationName()));
return;
}
producerOperation.invoke(invocation, asyncResp);
}
use of org.apache.servicecomb.swagger.engine.SwaggerProducerOperation in project incubator-servicecomb-java-chassis by apache.
the class LocalProducerInvoker method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
invocation = new SwaggerInvocation();
SwaggerConsumerOperation consumerOp = consumer.findOperation(method.getName());
SwaggerProducerOperation producerOp = producer.findOperation(consumerOp.getSwaggerMethod().getName());
consumerOp.getArgumentsMapper().toInvocation(args, invocation);
CompletableFuture<Object> future = new CompletableFuture<>();
producerOp.invoke(invocation, ar -> {
producerResponse = ar;
Object realResult = consumerOp.getResponseMapper().mapResponse(producerResponse);
future.complete(realResult);
});
if (CompletableFuture.class.equals(method.getReturnType())) {
return future;
}
return future.get();
}
use of org.apache.servicecomb.swagger.engine.SwaggerProducerOperation in project incubator-servicecomb-java-chassis by apache.
the class ProducerSchemaFactory method getOrCreateProducerSchema.
// 只会在启动流程中调用
public SchemaMeta getOrCreateProducerSchema(String microserviceName, String schemaId, Class<?> producerClass, Object producerInstance) {
MicroserviceMeta microserviceMeta = microserviceMetaManager.getOrCreateMicroserviceMeta(microserviceName);
ProducerSchemaContext context = new ProducerSchemaContext();
context.setMicroserviceMeta(microserviceMeta);
context.setSchemaId(schemaId);
context.setProviderClass(producerClass);
context.setProducerInstance(producerInstance);
SchemaMeta schemaMeta = getOrCreateSchema(context);
SwaggerProducer producer = swaggerEnv.createProducer(producerInstance, schemaMeta.getSwagger());
Executor reactiveExecutor = BeanUtils.getBean(ExecutorManager.EXECUTOR_REACTIVE);
for (OperationMeta operationMeta : schemaMeta.getOperations()) {
SwaggerProducerOperation producerOperation = producer.findOperation(operationMeta.getOperationId());
operationMeta.putExtData(Const.PRODUCER_OPERATION, producerOperation);
if (CompletableFuture.class.equals(producerOperation.getProducerMethod().getReturnType())) {
operationMeta.setExecutor(ExecutorManager.findExecutor(operationMeta, reactiveExecutor));
}
}
return schemaMeta;
}
Aggregations