use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class BCallbackSampleServiceImpl method hello.
@Override
public String hello() {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b1-----:" + context);
reqBaggage = context.getRequestBaggage("reqBaggageB");
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
final String[] str = new String[2];
final CountDownLatch latch = new CountDownLatch(2);
try {
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
str[0] = (String) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
});
sampleServiceC.hello();
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
str[1] = (String) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
});
sampleServiceD.hello();
latch.await(2000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
return str[0] + str[1];
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class BFutureSampleServiceImpl method echoObj.
@Override
public EchoResponse echoObj(EchoRequest req) {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b1-----:" + context);
reqBaggage = context.getRequestBaggage("reqBaggageB");
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
EchoResponse s1 = null;
EchoResponse s2 = null;
try {
s1 = sampleServiceC.echoObj(req);
Future futureC = SofaResponseFuture.getFuture(true);
s2 = sampleServiceD.echoObj(req);
Future futureD = SofaResponseFuture.getFuture();
s1 = (EchoResponse) futureC.get();
s2 = (EchoResponse) futureD.get(2000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
return EchoResponse.newBuilder().setCode(200).setMessage(s1.getMessage() + s2.getMessage()).build();
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class BFutureSampleServiceImpl method hello.
@Override
public String hello() {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b1-----:" + context);
reqBaggage = context.getRequestBaggage("reqBaggageB");
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
String s1 = null;
String s2 = null;
try {
s1 = sampleServiceC.hello();
Future futureC = SofaResponseFuture.getFuture(true);
s2 = sampleServiceD.hello();
Future futureD = SofaResponseFuture.getFuture();
s1 = (String) futureC.get();
s2 = (String) futureD.get(2000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
return s1 + s2;
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class BaggageCallbackTest method doTest.
@Override
public void doTest() throws Exception {
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
// C服务的服务端
CSampleServiceImpl refC = new CSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
serviceBeanC.setInterfaceId(SampleService.class.getName());
serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
serviceBeanC.setUniqueId("C3");
serviceBeanC.setRef(refC);
serviceBeanC.setServer(serverConfig);
serviceBeanC.setRegister(false);
serviceBeanC.export();
// D服务的服务端
DSampleServiceImpl refD = new DSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
serviceBeanD.setInterfaceId(SampleService.class.getName());
serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
serviceBeanD.setUniqueId("D3");
serviceBeanD.setRef(refD);
serviceBeanD.setServer(serverConfig);
serviceBeanD.setRegister(false);
serviceBeanD.export();
// B服务里的C服务客户端
ConsumerConfig referenceBeanC = new ConsumerConfig();
referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanC.setInterfaceId(SampleService.class.getName());
referenceBeanC.setUniqueId("C3");
referenceBeanC.setDirectUrl("localhost:12299");
referenceBeanC.setTimeout(1000);
MethodConfig methodConfigC = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
referenceBeanC.setMethods(Collections.singletonList(methodConfigC));
SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
// B服务里的D服务客户端
ConsumerConfig referenceBeanD = new ConsumerConfig();
referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanD.setInterfaceId(SampleService.class.getName());
referenceBeanD.setUniqueId("D3");
referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
referenceBeanD.setTimeout(1000);
MethodConfig methodConfigD = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
referenceBeanD.setMethods(Collections.singletonList(methodConfigD));
SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
// B服务的服务端
BCallbackSampleServiceImpl refB = new BCallbackSampleServiceImpl(sampleServiceC, sampleServiceD);
ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
ServiceBeanB.setInterfaceId(SampleService.class.getName());
ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
ServiceBeanB.setUniqueId("B3");
ServiceBeanB.setRef(refB);
ServiceBeanB.setServer(serverConfig);
ServiceBeanB.setRegister(false);
ServiceBeanB.export();
// A 服务
final String[] str = new String[1];
final CountDownLatch[] latch = new CountDownLatch[] { new CountDownLatch(1) };
final RpcInvokeContext[] contexts = new RpcInvokeContext[1];
ConsumerConfig referenceBeanA = new ConsumerConfig();
referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
referenceBeanA.setUniqueId("B3");
referenceBeanA.setInterfaceId(SampleService.class.getName());
referenceBeanA.setDirectUrl("localhost:12299");
referenceBeanA.setTimeout(3000);
MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
methodConfigA.setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
Assert.assertNotSame(RpcInvokeContext.getContext(), contexts[0]);
str[0] = (String) appResponse;
latch[0].countDown();
}
@Override
public void onAppException(Throwable t, String methodName, RequestBase request) {
assertEquals("sampleService", t.getMessage());
assertEquals("sayException", methodName);
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
// never go to this
assertEquals("sampleService", sofaException.getMessage());
assertEquals("sayException", methodName);
}
});
referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
SampleService service = (SampleService) referenceBeanA.refer();
// 开始测试
RpcInvokeContext context = RpcInvokeContext.getContext();
contexts[0] = context;
context.putRequestBaggage("reqBaggageB", "a2bbb");
context.putRequestBaggage("reqBaggageC", "a2ccc");
context.putRequestBaggage("reqBaggageD", "a2ddd");
String ret = service.hello();
Assert.assertEquals(ret, null);
latch[0].await(5000, TimeUnit.MILLISECONDS);
ret = str[0];
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
RpcInvokeContext.removeContext();
context = RpcInvokeContext.getContext();
contexts[0] = context;
latch[0] = new CountDownLatch(1);
str[0] = null;
ret = null;
ret = service.hello();
Assert.assertEquals(ret, null);
latch[0].await(5000, TimeUnit.MILLISECONDS);
ret = str[0];
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertNull(refB.getReqBaggage());
Assert.assertNull(refC.getReqBaggage());
Assert.assertNull(refD.getReqBaggage());
Assert.assertNull(context.getResponseBaggage("respBaggageB"));
Assert.assertNull(context.getResponseBaggage("respBaggageC"));
Assert.assertNull(context.getResponseBaggage("respBaggageD"));
Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.
the class AbstractHttpClientHandler method pickupBaggage.
protected void pickupBaggage(SofaResponse response) {
if (RpcInvokeContext.isBaggageEnable()) {
RpcInvokeContext invokeCtx = null;
if (context != null) {
invokeCtx = (RpcInvokeContext) context.getAttachment(RpcConstants.HIDDEN_KEY_INVOKE_CONTEXT);
}
if (invokeCtx == null) {
invokeCtx = RpcInvokeContext.getContext();
} else {
RpcInvokeContext.setContext(invokeCtx);
invokeCtx = RpcInvokeContext.getContext();
}
BaggageResolver.pickupFromResponse(invokeCtx, response);
}
}
Aggregations