Search in sources :

Example 1 with GenericService

use of com.alibaba.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class GenericServiceTest method testGenericServiceException.

@Test
public void testGenericServiceException() {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("generic-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29581));
    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;
        }
    });
    service.export();
    try {
        ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
        reference.setApplication(new ApplicationConfig("generic-consumer"));
        reference.setInterface(DemoService.class);
        reference.setUrl("dubbo://127.0.0.1:29581?generic=true");
        DemoService demoService = reference.get();
        try {
            // say name
            Assert.assertEquals("Generic Haha", demoService.sayName("Haha"));
            // get users
            List<User> users = new ArrayList<User>();
            users.add(new User("Aaa"));
            users = demoService.getUsers(users);
            Assert.assertEquals("Aaa", users.get(0).getName());
            // throw demo exception
            try {
                demoService.throwDemoException();
                Assert.fail();
            } catch (DemoException e) {
                Assert.assertEquals("Generic", e.getMessage());
            }
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : User(com.alibaba.dubbo.config.api.User) GenericService(com.alibaba.dubbo.rpc.service.GenericService) ArrayList(java.util.ArrayList) DemoService(com.alibaba.dubbo.config.api.DemoService) GenericException(com.alibaba.dubbo.rpc.service.GenericException) DemoException(com.alibaba.dubbo.config.api.DemoException) Test(org.junit.Test)

Example 2 with GenericService

use of com.alibaba.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class GenericServiceTest method testGenericSerializationJava.

@Test
public void testGenericSerializationJava() throws Exception {
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setApplication(new ApplicationConfig("generic-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29581));
    service.setInterface(DemoService.class.getName());
    DemoServiceImpl ref = new DemoServiceImpl();
    service.setRef(ref);
    service.export();
    try {
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
        reference.setApplication(new ApplicationConfig("generic-consumer"));
        reference.setInterface(DemoService.class);
        reference.setUrl("dubbo://127.0.0.1:29581?scope=remote");
        reference.setGeneric(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA);
        GenericService genericService = reference.get();
        try {
            String name = "kimi";
            ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
            ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").serialize(null, bos).writeObject(name);
            byte[] arg = bos.toByteArray();
            Object obj = genericService.$invoke("sayName", new String[] { String.class.getName() }, new Object[] { arg });
            Assert.assertTrue(obj instanceof byte[]);
            byte[] result = (byte[]) obj;
            Assert.assertEquals(ref.sayName(name), ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").deserialize(null, new ByteArrayInputStream(result)).readObject().toString());
            // getUsers
            List<User> users = new ArrayList<User>();
            User user = new User();
            user.setName(name);
            users.add(user);
            bos = new ByteArrayOutputStream(512);
            ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").serialize(null, bos).writeObject(users);
            obj = genericService.$invoke("getUsers", new String[] { List.class.getName() }, new Object[] { bos.toByteArray() });
            Assert.assertTrue(obj instanceof byte[]);
            result = (byte[]) obj;
            Assert.assertEquals(users, ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").deserialize(null, new ByteArrayInputStream(result)).readObject());
            // echo(int)
            bos = new ByteArrayOutputStream(512);
            ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").serialize(null, bos).writeObject(Integer.MAX_VALUE);
            obj = genericService.$invoke("echo", new String[] { int.class.getName() }, new Object[] { bos.toByteArray() });
            Assert.assertTrue(obj instanceof byte[]);
            Assert.assertEquals(Integer.MAX_VALUE, ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").deserialize(null, new ByteArrayInputStream((byte[]) obj)).readObject());
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : User(com.alibaba.dubbo.config.api.User) GenericService(com.alibaba.dubbo.rpc.service.GenericService) ArrayList(java.util.ArrayList) DemoService(com.alibaba.dubbo.config.api.DemoService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Serialization(com.alibaba.dubbo.common.serialize.Serialization) ByteArrayInputStream(java.io.ByteArrayInputStream) DemoServiceImpl(com.alibaba.dubbo.config.provider.impl.DemoServiceImpl) Test(org.junit.Test)

Example 3 with GenericService

use of com.alibaba.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class ValidationTest method testGenericValidation.

@Test
public void testGenericValidation() {
    ServiceConfig<ValidationService> service = new ServiceConfig<ValidationService>();
    service.setApplication(new ApplicationConfig("validation-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(ValidationService.class.getName());
    service.setRef(new ValidationServiceImpl());
    service.setValidation(String.valueOf(true));
    service.export();
    try {
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
        reference.setApplication(new ApplicationConfig("validation-consumer"));
        reference.setInterface(ValidationService.class.getName());
        reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&validation=true");
        reference.setGeneric(true);
        GenericService validationService = reference.get();
        try {
            // Save OK
            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("name", "liangfei");
            parameter.put("Email", "liangfei@liang.fei");
            parameter.put("Age", 50);
            parameter.put("LoginDate", new Date(System.currentTimeMillis() - 1000000));
            parameter.put("ExpiryDate", new Date(System.currentTimeMillis() + 1000000));
            validationService.$invoke("save", new String[] { ValidationParameter.class.getName() }, new Object[] { parameter });
            // Save Error
            try {
                parameter = new HashMap<String, Object>();
                validationService.$invoke("save", new String[] { ValidationParameter.class.getName() }, new Object[] { parameter });
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            // Delete OK
            validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 2, "abc" });
            // Delete Error
            try {
                validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 0, "abc" });
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            try {
                validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 2, null });
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            try {
                validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 0, null });
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) GenericService(com.alibaba.dubbo.rpc.service.GenericService) HashMap(java.util.HashMap) Date(java.util.Date) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) RpcException(com.alibaba.dubbo.rpc.RpcException) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) Test(org.junit.Test)

Example 4 with GenericService

use of com.alibaba.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class EnumBak method testGenricCustomArg.

//    @Test
//测试2.0.5调用2.0.3的兼容 自定义类型参数中包含enum类型
public void testGenricCustomArg() {
    int port = 20880;
    URL consumerurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=2000000");
    Invoker<GenericService> reference = protocol.refer(GenericService.class, consumerurl);
    GenericService demoProxy = (GenericService) proxy.getProxy(reference);
    Map<String, Object> arg = new HashMap<String, Object>();
    arg.put("type", "High");
    arg.put("name", "hi");
    Object obj = demoProxy.$invoke("get", new String[] { "com.alibaba.dubbo.rpc.CustomArgument" }, new Object[] { arg });
    System.out.println("obj---------->" + obj);
    reference.destroy();
}
Also used : GenericService(com.alibaba.dubbo.rpc.service.GenericService) HashMap(java.util.HashMap) URL(com.alibaba.dubbo.common.URL)

Example 5 with GenericService

use of com.alibaba.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class EnumBak method testGenericEnum.

@Test
public void testGenericEnum() throws InterruptedException {
    int port = NetUtils.getAvailablePort();
    URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE);
    DemoService demo = new DemoServiceImpl();
    Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
    protocol.export(invoker);
    URL consumerurl = serviceurl;
    Invoker<GenericService> reference = protocol.refer(GenericService.class, consumerurl);
    GenericService demoProxy = (GenericService) proxy.getProxy(reference);
    Object obj = demoProxy.$invoke("enumlength", new String[] { Type[].class.getName() }, new Object[] { new Type[] { Type.High, Type.High } });
    System.out.println("obj---------->" + obj);
    invoker.destroy();
    reference.destroy();
}
Also used : GenericService(com.alibaba.dubbo.rpc.service.GenericService) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Aggregations

GenericService (com.alibaba.dubbo.rpc.service.GenericService)11 Test (org.junit.Test)9 DemoService (com.alibaba.dubbo.config.api.DemoService)5 ArrayList (java.util.ArrayList)5 URL (com.alibaba.dubbo.common.URL)4 User (com.alibaba.dubbo.config.api.User)4 GenericException (com.alibaba.dubbo.rpc.service.GenericException)4 ApplicationConfig (com.alibaba.dubbo.config.ApplicationConfig)3 RegistryConfig (com.alibaba.dubbo.config.RegistryConfig)3 ServiceConfig (com.alibaba.dubbo.config.ServiceConfig)3 DemoServiceImpl (com.alibaba.dubbo.config.provider.impl.DemoServiceImpl)3 HashMap (java.util.HashMap)3 List (java.util.List)3 JavaBeanDescriptor (com.alibaba.dubbo.common.beanutil.JavaBeanDescriptor)2 ReferenceConfig (com.alibaba.dubbo.config.ReferenceConfig)2 DemoService (com.alibaba.dubbo.config.spring.api.DemoService)2 RpcException (com.alibaba.dubbo.rpc.RpcException)2 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)2 Serialization (com.alibaba.dubbo.common.serialize.Serialization)1 ProtocolConfig (com.alibaba.dubbo.config.ProtocolConfig)1