Search in sources :

Example 16 with GenericService

use of org.apache.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class RmiProtocolTest method testGenericInvoke.

@Test
public void testGenericInvoke() {
    int availablePort = NetUtils.getAvailablePort();
    DemoService service = new DemoServiceImpl();
    URL url = URL.valueOf("rmi://127.0.0.1:" + availablePort + "/" + DemoService.class.getName() + "?release=2.7.0");
    Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(service, DemoService.class, url));
    Invoker<GenericService> invoker = protocol.refer(GenericService.class, url);
    GenericService client = proxy.getProxy(invoker, true);
    String result = (String) client.$invoke("sayHi", new String[] { "java.lang.String" }, new Object[] { "haha" });
    Assertions.assertEquals("Hi, haha", result);
    invoker.destroy();
    exporter.unexport();
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 17 with GenericService

use of org.apache.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class EnumBak method testGenricCustomArg.

// verify compatibility when 2.0.5 invokes 2.0.3, enum in custom parameter
@Disabled
@Test
public void testGenricCustomArg() {
    int port = NetUtils.getAvailablePort();
    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[] { "org.apache.dubbo.rpc.CustomArgument" }, new Object[] { arg });
    System.out.println("obj---------->" + obj);
    reference.destroy();
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) HashMap(java.util.HashMap) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 18 with GenericService

use of org.apache.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class GenericServiceTest method testGeneric.

@Test
public void testGeneric() {
    DemoService server = new DemoServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("dubbo://127.0.0.1:5342/" + DemoService.class.getName() + "?version=1.0.0");
    Exporter<DemoService> exporter = protocol.export(proxyFactory.getInvoker(server, DemoService.class, url));
    Invoker<DemoService> invoker = protocol.refer(DemoService.class, url);
    GenericService client = (GenericService) proxyFactory.getProxy(invoker, true);
    Object result = client.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "haha" });
    Assertions.assertEquals("hello haha", result);
    org.apache.dubbo.rpc.service.GenericService newClient = (org.apache.dubbo.rpc.service.GenericService) proxyFactory.getProxy(invoker, true);
    Object res = newClient.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "hehe" });
    Assertions.assertEquals("hello hehe", res);
    invoker.destroy();
    exporter.unexport();
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) DemoService(org.apache.dubbo.service.DemoService) URL(org.apache.dubbo.common.URL) GenericService(org.apache.dubbo.rpc.service.GenericService) ComplexObject(org.apache.dubbo.service.ComplexObject) Protocol(org.apache.dubbo.rpc.Protocol) DemoServiceImpl(org.apache.dubbo.service.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 19 with GenericService

use of org.apache.dubbo.rpc.service.GenericService in project dubbo by alibaba.

the class GenericServiceTest method testGenericComplexCompute4FullServiceMetadata.

@Test
public void testGenericComplexCompute4FullServiceMetadata() {
    DemoService server = new DemoServiceImpl();
    ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
    Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
    URL url = URL.valueOf("dubbo://127.0.0.1:5342/" + DemoService.class.getName() + "?version=1.0.0&generic=true$timeout=3000");
    Exporter<DemoService> exporter = protocol.export(proxyFactory.getInvoker(server, DemoService.class, url));
    String var1 = "v1";
    int var2 = 234;
    long l = 555;
    String[] var3 = { "var31", "var32" };
    List<Integer> var4 = Arrays.asList(2, 4, 8);
    ComplexObject.TestEnum testEnum = ComplexObject.TestEnum.VALUE2;
    FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(DemoService.class);
    MethodDefinition methodDefinition = getMethod("complexCompute", fullServiceDefinition.getMethods());
    Map mapObject = createComplexObject(fullServiceDefinition, var1, var2, l, var3, var4, testEnum);
    ComplexObject complexObject = map2bean(mapObject);
    Invoker<GenericService> invoker = protocol.refer(GenericService.class, url);
    GenericService client = proxyFactory.getProxy(invoker, true);
    Object result = client.$invoke(methodDefinition.getName(), methodDefinition.getParameterTypes(), new Object[] { "haha", mapObject });
    Assertions.assertEquals("haha###" + complexObject.toString(), result);
    Invoker<DemoService> invoker2 = protocol.refer(DemoService.class, url);
    GenericService client2 = (GenericService) proxyFactory.getProxy(invoker2, true);
    Object result2 = client2.$invoke("complexCompute", methodDefinition.getParameterTypes(), new Object[] { "haha2", mapObject });
    Assertions.assertEquals("haha2###" + complexObject.toString(), result2);
    invoker.destroy();
    exporter.unexport();
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) DemoService(org.apache.dubbo.service.DemoService) URL(org.apache.dubbo.common.URL) FullServiceDefinition(org.apache.dubbo.metadata.definition.model.FullServiceDefinition) ComplexObject(org.apache.dubbo.service.ComplexObject) MethodDefinition(org.apache.dubbo.metadata.definition.model.MethodDefinition) ComplexObject(org.apache.dubbo.service.ComplexObject) Protocol(org.apache.dubbo.rpc.Protocol) HashMap(java.util.HashMap) Map(java.util.Map) DemoServiceImpl(org.apache.dubbo.service.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 20 with GenericService

use of org.apache.dubbo.rpc.service.GenericService 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

GenericService (org.apache.dubbo.rpc.service.GenericService)23 Test (org.junit.jupiter.api.Test)20 URL (org.apache.dubbo.common.URL)14 Protocol (org.apache.dubbo.rpc.Protocol)9 ProxyFactory (org.apache.dubbo.rpc.ProxyFactory)9 ApplicationConfig (org.apache.dubbo.config.ApplicationConfig)8 RegistryConfig (org.apache.dubbo.config.RegistryConfig)8 DubboBootstrap (org.apache.dubbo.config.bootstrap.DubboBootstrap)8 ReferenceConfig (org.apache.dubbo.config.ReferenceConfig)7 ServiceConfig (org.apache.dubbo.config.ServiceConfig)7 ArrayList (java.util.ArrayList)5 ProtocolConfig (org.apache.dubbo.config.ProtocolConfig)5 ComplexObject (org.apache.dubbo.service.ComplexObject)4 DemoService (org.apache.dubbo.service.DemoService)4 DemoServiceImpl (org.apache.dubbo.service.DemoServiceImpl)4 HashMap (java.util.HashMap)3 List (java.util.List)3 JavaBeanDescriptor (org.apache.dubbo.common.beanutil.JavaBeanDescriptor)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2