All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wichell.core.util.DubboUtil Maven / Gradle / Ivy

The newest version!
package com.wichell.core.util;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.context.ApplicationContext;

import com.alibaba.dubbo.config.spring.ReferenceBean;

/**
 * @author Wichell
 * @version 2016年5月27日 下午4:23:06
 */
public class DubboUtil {
    private DubboUtil() {
    }

    private static final ConcurrentMap> referenceConfigs = new ConcurrentHashMap>();

    /** 获取Dubbo服务 */
    public static Object refer(ApplicationContext applicationContext, String interfaceName) {
        String key = "/" + interfaceName + ":";
        ReferenceBean referenceConfig = referenceConfigs.get(key);
        if (referenceConfig == null) {
            referenceConfig = new ReferenceBean();
            referenceConfig.setInterface(interfaceName);
            if (applicationContext != null) {
                referenceConfig.setApplicationContext(applicationContext);
                try {
                    referenceConfig.afterPropertiesSet();
                } catch (Exception e) {
                    throw new IllegalStateException(e.getMessage(), e);
                }
            }
            referenceConfigs.putIfAbsent(key, referenceConfig);
            referenceConfig = referenceConfigs.get(key);
        }
        return referenceConfig.get();
    }
}