Markliniubility commented on code in PR #3322: URL: https://github.com/apache/incubator-eventmesh/pull/3322#discussion_r1122731653
########## eventmesh-registry-plugin/eventmesh-registry-etcd/src/main/java/org/apache/eventmesh/registry/etcd/service/EtcdCustomService.java: ########## @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eventmesh.registry.etcd.service; + +import org.apache.eventmesh.api.exception.RegistryException; +import org.apache.eventmesh.api.registry.bo.EventMeshAppSubTopicInfo; +import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.common.utils.JsonUtils; +import org.apache.eventmesh.registry.etcd.constant.EtcdConstant; + +import org.apache.commons.collections4.CollectionUtils; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.etcd.jetcd.ByteSequence; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.KeyValue; +import io.etcd.jetcd.options.GetOption; + +public class EtcdCustomService extends EtcdRegistryService { + + private static final String KEY_PREFIX = "eventMesh" + EtcdConstant.KEY_SEPARATOR; + private static final String KEY_APP = "app"; + private static final String KEY_SERVICE = "service"; + private static final Logger logger = LoggerFactory.getLogger(EtcdCustomService.class); + + + public EventMeshAppSubTopicInfo findEventMeshAppSubTopicInfoByGroup(String group) throws RegistryException { + + Client client = getEtcdClient(); + String keyPrefix = KEY_PREFIX + KEY_APP + EtcdConstant.KEY_SEPARATOR + group; + List<KeyValue> keyValues = null; + try { + ByteSequence keyByteSequence = ByteSequence.from(keyPrefix.getBytes(Constants.DEFAULT_CHARSET)); + + GetOption getOption = GetOption.newBuilder().withPrefix(keyByteSequence).build(); + + keyValues = client.getKVClient().get(keyByteSequence, getOption).get().getKvs(); + + + if (CollectionUtils.isNotEmpty(keyValues)) { + for (KeyValue kv : keyValues) { + EventMeshAppSubTopicInfo eventMeshAppSubTopicInfo = + JsonUtils.parseObject(new String(kv.getValue().getBytes(), Constants.DEFAULT_CHARSET), EventMeshAppSubTopicInfo.class); + return eventMeshAppSubTopicInfo; + } + } + } catch (Exception e) { + logger.error("[EtcdRegistryService][findEventMeshAppSubTopicInfoByGroup] error, group: {}", group, e); + throw new RegistryException(e.getMessage()); + } + + return null; Review Comment: nit: When you have to return null or empty, maybe you can consider using `Optional` as a replacement or add a `@Nullable` annotation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
