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