Search in sources :

Example 1 with SwaggerProducerOperation

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());
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Holder(javax.xml.ws.Holder) SwaggerProducerOperation(org.apache.servicecomb.swagger.engine.SwaggerProducerOperation) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 2 with SwaggerProducerOperation

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);
}
Also used : SwaggerProducerOperation(org.apache.servicecomb.swagger.engine.SwaggerProducerOperation)

Example 3 with SwaggerProducerOperation

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();
}
Also used : SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) CompletableFuture(java.util.concurrent.CompletableFuture) SwaggerConsumerOperation(org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation) SwaggerProducerOperation(org.apache.servicecomb.swagger.engine.SwaggerProducerOperation)

Example 4 with SwaggerProducerOperation

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;
}
Also used : Executor(java.util.concurrent.Executor) SwaggerProducer(org.apache.servicecomb.swagger.engine.SwaggerProducer) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) SwaggerProducerOperation(org.apache.servicecomb.swagger.engine.SwaggerProducerOperation) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta)

Aggregations

SwaggerProducerOperation (org.apache.servicecomb.swagger.engine.SwaggerProducerOperation)4 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 Executor (java.util.concurrent.Executor)1 Holder (javax.xml.ws.Holder)1 Invocation (org.apache.servicecomb.core.Invocation)1 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)1 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)1 SwaggerConsumerOperation (org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation)1 SwaggerProducer (org.apache.servicecomb.swagger.engine.SwaggerProducer)1 Response (org.apache.servicecomb.swagger.invocation.Response)1 SwaggerInvocation (org.apache.servicecomb.swagger.invocation.SwaggerInvocation)1 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)1 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)1 Test (org.junit.Test)1