Search in sources :

Example 21 with GenericService

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

the class GenericServiceTest method testGenericReferenceException.

@SuppressWarnings("unchecked")
@Test
public void testGenericReferenceException() {
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setInterface(DemoService.class.getName());
    service.setRef(new DemoServiceImpl());
    ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
    reference.setInterface(DemoService.class);
    reference.setUrl("dubbo://127.0.0.1:29581?scope=remote&timeout=3000");
    reference.setGeneric(true);
    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 {
        GenericService genericService = ReferenceConfigCache.getCache().get(reference);
        List<Map<String, Object>> users = new ArrayList<Map<String, Object>>();
        Map<String, Object> user = new HashMap<String, Object>();
        user.put("class", "org.apache.dubbo.config.api.User");
        user.put("name", "actual.provider");
        users.add(user);
        users = (List<Map<String, Object>>) genericService.$invoke("getUsers", new String[] { List.class.getName() }, new Object[] { users });
        Assertions.assertEquals(1, users.size());
        Assertions.assertEquals("actual.provider", users.get(0).get("name"));
    } finally {
        bootstrap.stop();
    }
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) GenericService(org.apache.dubbo.rpc.service.GenericService) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) 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) ArrayList(java.util.ArrayList) List(java.util.List) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 22 with GenericService

use of org.apache.dubbo.rpc.service.GenericService in project incubator-dubbo-ops by apache.

the class GenericServiceImpl method invoke.

public Object invoke(String service, String method, String[] parameterTypes, Object[] params) {
    ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
    String group = Tool.getGroup(service);
    String version = Tool.getVersion(service);
    String intf = Tool.getInterface(service);
    reference.setGeneric(true);
    reference.setApplication(applicationConfig);
    reference.setInterface(intf);
    reference.setVersion(version);
    reference.setGroup(group);
    // Keep it consistent with the ConfigManager cache
    reference.setSticky(false);
    try {
        removeGenericSymbol(parameterTypes);
        GenericService genericService = reference.get();
        return genericService.$invoke(method, parameterTypes, params);
    } finally {
        reference.destroy();
    }
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) ReferenceConfig(org.apache.dubbo.config.ReferenceConfig)

Example 23 with GenericService

use of org.apache.dubbo.rpc.service.GenericService in project incubator-dubbo-ops by apache.

the class ApiDocsDubboGenericUtil method invoke.

/**
 * Call duboo provider and return {@link CompletableFuture}
 *
 * @return java.util.concurrent.CompletableFuture<java.lang.Object>
 * @param: address
 * @param: interfaceName
 * @param: methodName
 * @param: async  Whether the provider is asynchronous is to directly return the {@link CompletableFuture}
 * returned by the provider, not to wrap it as {@link CompletableFuture}
 * @param: paramTypes
 * @param: paramValues
 */
public static CompletableFuture<Object> invoke(String address, String interfaceName, String methodName, boolean async, String version, String[] paramTypes, Object[] paramValues, String group) {
    CompletableFuture future = null;
    ReferenceConfig<GenericService> reference = getReferenceConfig(address, interfaceName, version, group);
    if (null != reference) {
        GenericService genericService = reference.get();
        if (null != genericService) {
            if (async) {
                future = genericService.$invokeAsync(methodName, paramTypes, paramValues);
            } else {
                future = CompletableFuture.supplyAsync(() -> genericService.$invoke(methodName, paramTypes, paramValues), EXECUTOR);
            }
            future.exceptionally(ex -> {
                if (StringUtils.contains(ex.toString(), "Failed to invoke remote method")) {
                    removeReferenceConfig(address, interfaceName, version, group);
                }
                return ex;
            });
        }
    }
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) GenericService(org.apache.dubbo.rpc.service.GenericService)

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