Search in sources :

Example 1 with InvocationContext

use of io.servicecomb.core.context.InvocationContext in project java-chassis by ServiceComb.

the class ProducerOperation method invoke.

public void invoke(Invocation invocation, AsyncResponse asyncResp) {
    InvocationContext context = new InvocationContext(invocation.getContext());
    ContextUtils.setInvocationContext(context);
    Response response = null;
    try {
        Object[] args = argsMapper.toProducerArgs(invocation);
        Object result = method.invoke(instance, args);
        response = responseMapper.mapResponse(context.getStatus(), result);
    } catch (Throwable e) {
        response = processException(e);
    }
    ContextUtils.removeInvocationContext();
    asyncResp.handle(response);
}
Also used : Response(io.servicecomb.core.Response) AsyncResponse(io.servicecomb.core.AsyncResponse) InvocationContext(io.servicecomb.core.context.InvocationContext)

Example 2 with InvocationContext

use of io.servicecomb.core.context.InvocationContext in project java-chassis by ServiceComb.

the class TestConfig method testContextUtils.

@Test
public void testContextUtils() {
    ThreadLocal<InvocationContext> contextMgr = new ThreadLocal<>();
    Assert.assertEquals(contextMgr.get(), ContextUtils.getInvocationContext());
    Map<String, String> oContext = new HashMap<>();
    oContext.put("test1", new String("testObject"));
    InvocationContext oInnovation = new InvocationContext(oContext);
    Assert.assertEquals(oContext, oInnovation.getContext());
    oContext.put("test2", new String("testObject"));
    oInnovation.setContext(oContext);
    Assert.assertEquals(oContext, oInnovation.getContext());
    oInnovation.setStatus(Status.OK);
    Assert.assertEquals(200, oInnovation.getStatus().getStatusCode());
    oInnovation.setStatus(204);
    Assert.assertEquals(204, oInnovation.getStatus().getStatusCode());
    oInnovation.setStatus(Status.OK);
    Assert.assertEquals((Status.OK).getStatusCode(), oInnovation.getStatus().getStatusCode());
    oInnovation.setStatus(203, "Done");
    Assert.assertEquals(203, oInnovation.getStatus().getStatusCode());
    ContextUtils.setInvocationContext(oInnovation);
    Assert.assertEquals(oInnovation, ContextUtils.getInvocationContext());
    ContextUtils.removeInvocationContext();
    Assert.assertNotEquals(oContext, oInnovation);
}
Also used : HashMap(java.util.HashMap) InvocationContext(io.servicecomb.core.context.InvocationContext) Test(org.junit.Test)

Aggregations

InvocationContext (io.servicecomb.core.context.InvocationContext)2 AsyncResponse (io.servicecomb.core.AsyncResponse)1 Response (io.servicecomb.core.Response)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1