|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.eventmesh.admin.server; |
| 19 | + |
| 20 | +import org.apache.eventmesh.common.Constants; |
| 21 | +import org.apache.eventmesh.common.config.CommonConfiguration; |
| 22 | +import org.apache.eventmesh.common.config.ConfigService; |
| 23 | +import org.apache.eventmesh.common.remote.Task; |
| 24 | +import org.apache.eventmesh.common.remote.exception.ErrorCode; |
| 25 | +import org.apache.eventmesh.common.remote.request.ReportHeartBeatRequest; |
| 26 | +import org.apache.eventmesh.common.utils.IPUtils; |
| 27 | +import org.apache.eventmesh.common.utils.PagedList; |
| 28 | +import org.apache.eventmesh.registry.RegisterServerInfo; |
| 29 | +import org.apache.eventmesh.registry.RegistryFactory; |
| 30 | +import org.apache.eventmesh.registry.RegistryService; |
| 31 | + |
| 32 | +import org.apache.commons.lang3.StringUtils; |
| 33 | + |
| 34 | +import javax.annotation.PostConstruct; |
| 35 | + |
| 36 | +import org.springframework.boot.context.event.ApplicationReadyEvent; |
| 37 | +import org.springframework.context.ApplicationListener; |
| 38 | +import org.springframework.stereotype.Service; |
| 39 | + |
| 40 | +import lombok.extern.slf4j.Slf4j; |
| 41 | + |
| 42 | +@Service |
| 43 | +@Slf4j |
| 44 | +public class AdminServer implements Admin, ApplicationListener<ApplicationReadyEvent> { |
| 45 | + |
| 46 | + private final RegistryService registryService; |
| 47 | + |
| 48 | + private final RegisterServerInfo adminServeInfo; |
| 49 | + |
| 50 | + private final CommonConfiguration configuration; |
| 51 | + |
| 52 | + public AdminServer(AdminServerProperties properties) { |
| 53 | + configuration = |
| 54 | + ConfigService.getInstance().buildConfigInstance(CommonConfiguration.class); |
| 55 | + if (configuration == null) { |
| 56 | + throw new AdminServerRuntimeException(ErrorCode.STARTUP_CONFIG_MISS, "common configuration file miss"); |
| 57 | + } |
| 58 | + this.adminServeInfo = new RegisterServerInfo(); |
| 59 | + |
| 60 | + adminServeInfo.setHealth(true); |
| 61 | + adminServeInfo.setAddress(IPUtils.getLocalAddress() + ":" + properties.getPort()); |
| 62 | + String name = Constants.ADMIN_SERVER_REGISTRY_NAME; |
| 63 | + if (StringUtils.isNotBlank(properties.getServiceName())) { |
| 64 | + name = properties.getServiceName(); |
| 65 | + } |
| 66 | + adminServeInfo.setServiceName(name); |
| 67 | + registryService = RegistryFactory.getInstance(configuration.getEventMeshRegistryPluginType()); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean createOrUpdateTask(Task task) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean deleteTask(Long id) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Task getTask(Long id) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public PagedList<Task> getTaskPaged(Task task) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void reportHeartbeat(ReportHeartBeatRequest heartBeat) { |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + @PostConstruct |
| 98 | + public void start() { |
| 99 | + if (configuration.isEventMeshRegistryPluginEnabled()) { |
| 100 | + registryService.init(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void destroy() { |
| 106 | + if (configuration.isEventMeshRegistryPluginEnabled()) { |
| 107 | + registryService.unRegister(adminServeInfo); |
| 108 | + try { |
| 109 | + Thread.sleep(3000); |
| 110 | + } catch (InterruptedException ignore) { |
| 111 | + log.warn("interrupted when sleep"); |
| 112 | + Thread.currentThread().interrupt(); |
| 113 | + } |
| 114 | + registryService.shutdown(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onApplicationEvent(ApplicationReadyEvent event) { |
| 120 | + if (configuration.isEventMeshRegistryPluginEnabled()) { |
| 121 | + log.info("application is started and registry plugin is enabled, it's will register admin self"); |
| 122 | + registryService.register(adminServeInfo); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments