Search in sources :

Example 1 with GenericException

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();
    }
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) GenericService(org.apache.dubbo.rpc.service.GenericService) ArrayList(java.util.ArrayList) GenericException(org.apache.dubbo.rpc.service.GenericException) ServiceConfig(org.apache.dubbo.config.ServiceConfig) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) ReferenceConfig(org.apache.dubbo.config.ReferenceConfig) DubboBootstrap(org.apache.dubbo.config.bootstrap.DubboBootstrap) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig) Test(org.junit.jupiter.api.Test)

Example 2 with GenericException

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()));
        }
    }
}
Also used : IOException(java.io.IOException) GenericException(org.apache.dubbo.rpc.service.GenericException) Serialization(org.apache.dubbo.common.serialize.Serialization) RpcException(org.apache.dubbo.rpc.RpcException) UnsafeByteArrayOutputStream(org.apache.dubbo.common.io.UnsafeByteArrayOutputStream)

Example 3 with GenericException

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();
    }
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) GenericService(org.apache.dubbo.rpc.service.GenericService) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) GenericException(org.apache.dubbo.rpc.service.GenericException) JavaBeanDescriptor(org.apache.dubbo.common.beanutil.JavaBeanDescriptor) ServiceConfig(org.apache.dubbo.config.ServiceConfig) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) DubboBootstrap(org.apache.dubbo.config.bootstrap.DubboBootstrap) ArrayList(java.util.ArrayList) List(java.util.List) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig) Test(org.junit.jupiter.api.Test)

Aggregations

GenericException (org.apache.dubbo.rpc.service.GenericException)3 ArrayList (java.util.ArrayList)2 ApplicationConfig (org.apache.dubbo.config.ApplicationConfig)2 ProtocolConfig (org.apache.dubbo.config.ProtocolConfig)2 RegistryConfig (org.apache.dubbo.config.RegistryConfig)2 ServiceConfig (org.apache.dubbo.config.ServiceConfig)2 DubboBootstrap (org.apache.dubbo.config.bootstrap.DubboBootstrap)2 GenericService (org.apache.dubbo.rpc.service.GenericService)2 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JavaBeanDescriptor (org.apache.dubbo.common.beanutil.JavaBeanDescriptor)1 UnsafeByteArrayOutputStream (org.apache.dubbo.common.io.UnsafeByteArrayOutputStream)1 Serialization (org.apache.dubbo.common.serialize.Serialization)1 ReferenceConfig (org.apache.dubbo.config.ReferenceConfig)1 RpcException (org.apache.dubbo.rpc.RpcException)1