Search in sources :

Example 1 with ServiceEventHandler

use of com.tencent.polaris.api.plugin.server.ServiceEventHandler in project polaris-java by polarismesh.

the class InMemoryRegistry method loadRemoteValue.

/**
 * 加载资源
 *
 * @param svcEventKey 服务资源名
 * @param notifier    通知器
 * @throws PolarisException 异常
 */
private void loadRemoteValue(ServiceEventKey svcEventKey, EventCompleteNotifier notifier) throws PolarisException {
    checkDestroyed();
    CacheHandler handler = cacheHandlers.get(svcEventKey.getEventType());
    if (null == handler) {
        throw new PolarisException(ErrorCode.INTERNAL_ERROR, String.format("[LocalRegistry] unRegistered resource type %s", svcEventKey.getEventType()));
    }
    CacheObject cacheObject = resourceMap.computeIfAbsent(svcEventKey, serviceEventKey -> new CacheObject(handler, svcEventKey, InMemoryRegistry.this));
    // 添加监听器
    cacheObject.addNotifier(notifier);
    // 触发往serverConnector注册
    if (cacheObject.startRegister()) {
        LOG.info("[LocalRegistry]start to register service handler for {}", svcEventKey);
        try {
            connector.registerServiceHandler(enhanceServiceEventHandler(new ServiceEventHandler(svcEventKey, cacheObject)));
        } catch (Throwable e) {
            PolarisException polarisException;
            if (e instanceof PolarisException) {
                polarisException = (PolarisException) e;
            } else {
                polarisException = new PolarisException(ErrorCode.INTERNAL_ERROR, String.format("exception occurs while registering service handler for %s", svcEventKey));
            }
            cacheObject.resumeUnRegistered(polarisException);
            throw polarisException;
        }
        if (svcEventKey.getEventType() == EventType.INSTANCE) {
            // 注册了监听后,认为是被用户需要的服务,加入serviceSet
            services.put(svcEventKey.getServiceKey(), true);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) CacheHandler(com.tencent.polaris.api.plugin.registry.CacheHandler) ServiceEventHandler(com.tencent.polaris.api.plugin.server.ServiceEventHandler)

Aggregations

PolarisException (com.tencent.polaris.api.exception.PolarisException)1 CacheHandler (com.tencent.polaris.api.plugin.registry.CacheHandler)1 ServiceEventHandler (com.tencent.polaris.api.plugin.server.ServiceEventHandler)1