Search in sources :

Example 1 with ProducerOperation

use of io.servicecomb.core.provider.producer.ProducerOperation in project java-chassis by ServiceComb.

the class TestProducerSchemaFactory method testGetOrCreateProducer.

@Test
public void testGetOrCreateProducer() throws Exception {
    OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
    Assert.assertEquals("add", operationMeta.getOperationId());
    ProducerOperation 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 });
    Holder<Response> holder = new Holder<>();
    producerOperation.invoke(invocation, resp -> {
        holder.value = resp;
    });
    Assert.assertEquals(3, (int) holder.value.getResult());
    invocation = new Invocation((Endpoint) null, operationMeta, 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(io.servicecomb.core.Response) Invocation(io.servicecomb.core.Invocation) Endpoint(io.servicecomb.core.Endpoint) ProducerOperation(io.servicecomb.core.provider.producer.ProducerOperation) InvocationException(io.servicecomb.core.exception.InvocationException) Holder(javax.xml.ws.Holder) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 2 with ProducerOperation

use of io.servicecomb.core.provider.producer.ProducerOperation in project java-chassis by ServiceComb.

the class ProducerSchemaFactory method createOperation.

protected void createOperation(ProducerSchemaContext context, OperationMeta operationMeta, ProducerArgumentsMapper argsMapper) {
    Object producerInstance = context.getProducerInstance();
    Method method = ReflectUtils.findMethod(producerInstance.getClass(), operationMeta.getMethod().getName());
    ProducerResponseMapper responseMapper = responseMapperFactory.createResponseMapper(method.getReturnType());
    ProducerOperation producerOperation = new ProducerOperation(producerInstance, method, argsMapper, responseMapper);
    operationMeta.putExtData(Const.PRODUCER_OPERATION, producerOperation);
}
Also used : ProducerResponseMapper(io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper) ProducerOperation(io.servicecomb.core.provider.producer.ProducerOperation) Method(java.lang.reflect.Method)

Example 3 with ProducerOperation

use of io.servicecomb.core.provider.producer.ProducerOperation in project java-chassis by ServiceComb.

the class ProducerOperationHandler method handle.

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    ProducerOperation 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 : ProducerOperation(io.servicecomb.core.provider.producer.ProducerOperation)

Aggregations

ProducerOperation (io.servicecomb.core.provider.producer.ProducerOperation)3 Endpoint (io.servicecomb.core.Endpoint)1 Invocation (io.servicecomb.core.Invocation)1 Response (io.servicecomb.core.Response)1 OperationMeta (io.servicecomb.core.definition.OperationMeta)1 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)1 InvocationException (io.servicecomb.core.exception.InvocationException)1 ProducerResponseMapper (io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper)1 Method (java.lang.reflect.Method)1 Holder (javax.xml.ws.Holder)1 Test (org.junit.Test)1