use of org.apache.dubbo.rpc.service.GenericException in project dubbo by alibaba.
the class GenericServiceTest method testGenericServiceException.
@Test
public void testGenericServiceException() {
ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
service.setInterface(DemoService.class.getName());
service.setRef(new GenericService() {
public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
if ("sayName".equals(method)) {
return "Generic " + args[0];
}
if ("throwDemoException".equals(method)) {
throw new GenericException(DemoException.class.getName(), "Generic");
}
if ("getUsers".equals(method)) {
return args[0];
}
return null;
}
});
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
reference.setInterface(DemoService.class);
reference.setUrl("dubbo://127.0.0.1:29581?generic=true&timeout=3000");
DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(new ApplicationConfig("generic-test")).registry(new RegistryConfig("N/A")).protocol(new ProtocolConfig("dubbo", 29581)).service(service).reference(reference);
bootstrap.start();
try {
DemoService demoService = ReferenceConfigCache.getCache().get(reference);
// say name
Assertions.assertEquals("Generic Haha", demoService.sayName("Haha"));
// get users
List<User> users = new ArrayList<User>();
users.add(new User("Aaa"));
users = demoService.getUsers(users);
Assertions.assertEquals("Aaa", users.get(0).getName());
// throw demo exception
try {
demoService.throwDemoException();
Assertions.fail();
} catch (DemoException e) {
Assertions.assertEquals("Generic", e.getMessage());
}
} finally {
bootstrap.stop();
}
}
use of org.apache.dubbo.rpc.service.GenericException in project dubbo by alibaba.
the class GenericFilter method onResponse.
@Override
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation inv) {
if ((inv.getMethodName().equals($INVOKE) || inv.getMethodName().equals($INVOKE_ASYNC)) && inv.getArguments() != null && inv.getArguments().length == 3 && !GenericService.class.isAssignableFrom(invoker.getInterface())) {
String generic = inv.getAttachment(GENERIC_KEY);
if (StringUtils.isBlank(generic)) {
generic = RpcContext.getContext().getAttachment(GENERIC_KEY);
}
if (appResponse.hasException()) {
Throwable appException = appResponse.getException();
if (appException instanceof GenericException) {
GenericException tmp = (GenericException) appException;
appException = new com.alibaba.dubbo.rpc.service.GenericException(tmp.getExceptionClass(), tmp.getExceptionMessage());
}
if (!(appException instanceof com.alibaba.dubbo.rpc.service.GenericException)) {
appException = new com.alibaba.dubbo.rpc.service.GenericException(appException);
}
appResponse.setException(appException);
}
if (ProtocolUtils.isJavaGenericSerialization(generic)) {
try {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(GENERIC_SERIALIZATION_NATIVE_JAVA).serialize(null, os).writeObject(appResponse.getValue());
appResponse.setValue(os.toByteArray());
} catch (IOException e) {
throw new RpcException("Generic serialization [" + GENERIC_SERIALIZATION_NATIVE_JAVA + "] serialize result failed.", e);
}
} else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
appResponse.setValue(JavaBeanSerializeUtil.serialize(appResponse.getValue(), JavaBeanAccessor.METHOD));
} else if (ProtocolUtils.isProtobufGenericSerialization(generic)) {
try {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(GENERIC_SERIALIZATION_PROTOBUF).serialize(null, os).writeObject(appResponse.getValue());
appResponse.setValue(os.toString());
} catch (IOException e) {
throw new RpcException("Generic serialization [" + GENERIC_SERIALIZATION_PROTOBUF + "] serialize result failed.", e);
}
} else if (ProtocolUtils.isGenericReturnRawResult(generic)) {
return;
} else {
appResponse.setValue(PojoUtils.generalize(appResponse.getValue()));
}
}
}
use of org.apache.dubbo.rpc.service.GenericException in project dubbo by alibaba.
the class GenericServiceTest method testGenericImplementationWithBeanSerialization.
@Test
public void testGenericImplementationWithBeanSerialization() throws Exception {
final AtomicReference reference = new AtomicReference();
ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
service.setInterface(DemoService.class.getName());
service.setRef(new GenericService() {
public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
if ("getUsers".equals(method)) {
GenericParameter arg = new GenericParameter();
arg.method = method;
arg.parameterTypes = parameterTypes;
arg.arguments = args;
reference.set(arg);
return args[0];
}
if ("sayName".equals(method)) {
return null;
}
return args;
}
});
ReferenceConfig<DemoService> ref = null;
ref = new ReferenceConfig<DemoService>();
ref.setInterface(DemoService.class);
ref.setUrl("dubbo://127.0.0.1:29581?scope=remote&generic=bean&timeout=3000");
DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(new ApplicationConfig("generic-test")).registry(new RegistryConfig("N/A")).protocol(new ProtocolConfig("dubbo", 29581)).service(service).reference(ref);
bootstrap.start();
try {
DemoService demoService = bootstrap.getCache().get(ref);
User user = new User();
user.setName("zhangsan");
List<User> users = new ArrayList<User>();
users.add(user);
List<User> result = demoService.getUsers(users);
Assertions.assertEquals(users.size(), result.size());
Assertions.assertEquals(user.getName(), result.get(0).getName());
GenericParameter gp = (GenericParameter) reference.get();
Assertions.assertEquals("getUsers", gp.method);
Assertions.assertEquals(1, gp.parameterTypes.length);
Assertions.assertEquals(ReflectUtils.getName(List.class), gp.parameterTypes[0]);
Assertions.assertEquals(1, gp.arguments.length);
Assertions.assertTrue(gp.arguments[0] instanceof JavaBeanDescriptor);
JavaBeanDescriptor descriptor = (JavaBeanDescriptor) gp.arguments[0];
Assertions.assertTrue(descriptor.isCollectionType());
Assertions.assertEquals(ArrayList.class.getName(), descriptor.getClassName());
Assertions.assertEquals(1, descriptor.propertySize());
descriptor = (JavaBeanDescriptor) descriptor.getProperty(0);
Assertions.assertTrue(descriptor.isBeanType());
Assertions.assertEquals(User.class.getName(), descriptor.getClassName());
Assertions.assertEquals(user.getName(), ((JavaBeanDescriptor) descriptor.getProperty("name")).getPrimitiveProperty());
Assertions.assertNull(demoService.sayName("zhangsan"));
} finally {
bootstrap.stop();
}
}
Aggregations