Skip to content
Permalink
Browse files
Add Triple attachment and exception samples
  • Loading branch information
guohao committed Aug 11, 2021
1 parent e67b8f6 commit dc7c88afe8c5814eb2a661219d3779926f5b9585
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
@@ -21,6 +21,7 @@
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.rpc.RpcContext;

public class ApiWrapperConsumer {
public static void main(String[] args) {
@@ -42,5 +43,16 @@ public static void main(String[] args) {
String reply = iGreeter.sayHello0("haha");
// 4MB response
System.out.println("Reply len:" + reply.length() + " cost:" + (System.currentTimeMillis() - st));

try {
final String exception = iGreeter.sayHelloException("exception");
} catch (Throwable t) {
System.out.println("Exception:" + t.getMessage());
}

RpcContext.getClientAttachment().setAttachment("str", "str");
final String attachment = iGreeter.sayHelloWithAttachment("attachment");
System.out.println(RpcContext.getServerContext().getObjectAttachments());

}
}
@@ -25,4 +25,7 @@ public interface IGreeter2 {
*/
String sayHello0(String request);

String sayHelloException(String request);

String sayHelloWithAttachment(String request);
}
@@ -17,6 +17,8 @@

package com.apache.dubbo.sample.basic;

import org.apache.dubbo.rpc.RpcContext;

public class IGreeter2Impl implements IGreeter2 {
@Override
public String sayHello0(String request) {
@@ -27,4 +29,18 @@ public String sayHello0(String request) {
request = respBuilder.toString();
return request;
}

@Override
public String sayHelloException(String request) {
throw new RuntimeException("Biz exception");
}

@Override
public String sayHelloWithAttachment(String request) {
System.out.println(RpcContext.getServerAttachment().getObjectAttachments());
RpcContext.getServerContext().setAttachment("str", "str")
.setAttachment("integer", 1)
.setAttachment("raw", new byte[]{1, 2, 3, 4});
return "hello," + request;
}
}

0 comments on commit dc7c88a

Please sign in to comment.