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();
}
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();
}
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();
}
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();
}
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();
}
}
Aggregations