|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.plugin.logging.admin.listener; |
| 19 | + |
| 20 | +import org.minbox.framework.api.boot.plugin.logging.ApiBootLog; |
| 21 | +import org.minbox.framework.api.boot.plugin.logging.ApiBootLogClientNotice; |
| 22 | +import org.minbox.framework.api.boot.plugin.logging.admin.endpoint.LoggingEndpoint; |
| 23 | +import org.minbox.framework.api.boot.plugin.logging.admin.event.ReportLogEvent; |
| 24 | +import org.minbox.framework.api.boot.plugin.logging.admin.storage.LoggingStorage; |
| 25 | +import org.minbox.framework.api.boot.plugin.tools.JsonTools; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import org.springframework.context.ApplicationEvent; |
| 29 | +import org.springframework.context.event.SmartApplicationListener; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.ObjectUtils; |
| 32 | + |
| 33 | +import java.util.concurrent.ConcurrentHashMap; |
| 34 | +import java.util.concurrent.ConcurrentMap; |
| 35 | + |
| 36 | +/** |
| 37 | + * Storage Report Logs Listener |
| 38 | + * |
| 39 | + * @author:恒宇少年 - 于起宇 |
| 40 | + * <p> |
| 41 | + * DateTime:2019-07-22 16:17 |
| 42 | + * Blog:http://blog.yuqiyu.com |
| 43 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 44 | + * Gitee:https://gitee.com/hengboy |
| 45 | + * GitHub:https://github.com/hengboy |
| 46 | + */ |
| 47 | +public class ReportLogStorageListener implements SmartApplicationListener { |
| 48 | + /** |
| 49 | + * logger instance |
| 50 | + */ |
| 51 | + static Logger logger = LoggerFactory.getLogger(ReportLogStorageListener.class); |
| 52 | + /** |
| 53 | + * ServiceDetails IDS |
| 54 | + */ |
| 55 | + ConcurrentMap<String, String> SERVICE_DETAIL_IDS = new ConcurrentHashMap(); |
| 56 | + /** |
| 57 | + * ApiBoot Logging Storage |
| 58 | + */ |
| 59 | + private LoggingStorage loggingStorage; |
| 60 | + |
| 61 | + public ReportLogStorageListener(LoggingStorage loggingStorage) { |
| 62 | + this.loggingStorage = loggingStorage; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Storage Log |
| 67 | + * |
| 68 | + * @param event ReportLogEvent |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public void onApplicationEvent(ApplicationEvent event) { |
| 72 | + try { |
| 73 | + logger.debug("Starting Storage Report Request Logs."); |
| 74 | + ReportLogEvent reportLogEvent = (ReportLogEvent) event; |
| 75 | + ApiBootLogClientNotice notice = reportLogEvent.getLogClientNotice(); |
| 76 | + String serviceDetail = formatServiceDetailID(notice.getClientServiceId(), notice.getClientServiceIp(), notice.getClientServicePort()); |
| 77 | + String serviceDetailId = SERVICE_DETAIL_IDS.get(serviceDetail); |
| 78 | + |
| 79 | + // new service |
| 80 | + if (ObjectUtils.isEmpty(serviceDetailId)) { |
| 81 | + // select service detail id from database |
| 82 | + serviceDetailId = loggingStorage.selectServiceDetailId(notice.getClientServiceId(), notice.getClientServiceIp(), notice.getClientServicePort()); |
| 83 | + // if don't have in database |
| 84 | + // create new service detail |
| 85 | + if (ObjectUtils.isEmpty(serviceDetailId)) { |
| 86 | + serviceDetailId = loggingStorage.insertServiceDetail(notice.getClientServiceId(), notice.getClientServiceIp(), notice.getClientServicePort()); |
| 87 | + } |
| 88 | + if (!ObjectUtils.isEmpty(serviceDetailId)) { |
| 89 | + SERVICE_DETAIL_IDS.put(serviceDetail, serviceDetailId); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // save logs |
| 94 | + if (!ObjectUtils.isEmpty(notice.getLoggers())) { |
| 95 | + for (ApiBootLog log : notice.getLoggers()) { |
| 96 | + loggingStorage.insertLog(serviceDetailId, log); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // update last report time |
| 101 | + loggingStorage.updateLastReportTime(serviceDetailId); |
| 102 | + |
| 103 | + } catch (Exception e) { |
| 104 | + logger.error(e.getMessage(), e); |
| 105 | + } finally { |
| 106 | + logger.debug("Storage Report Request Logs Complete."); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * format serviceDetail ID |
| 112 | + * |
| 113 | + * @param serviceId service id |
| 114 | + * @param serviceIp service ip address |
| 115 | + * @param servicePort service port |
| 116 | + * @return |
| 117 | + */ |
| 118 | + String formatServiceDetailID(String serviceId, String serviceIp, Integer servicePort) { |
| 119 | + Assert.notNull(serviceId, "Service Id Is Required."); |
| 120 | + Assert.notNull(serviceIp, "Service Ip Is Required."); |
| 121 | + Assert.notNull(servicePort, "Service Port Is Required."); |
| 122 | + return String.format("%s-%s:%d", serviceId, serviceIp, servicePort); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) { |
| 127 | + return eventType == ReportLogEvent.class; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean supportsSourceType(Class<?> sourceType) { |
| 132 | + return sourceType == LoggingEndpoint.class; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * second execute |
| 137 | + * |
| 138 | + * @return execute order |
| 139 | + */ |
| 140 | + @Override |
| 141 | + public int getOrder() { |
| 142 | + return 1; |
| 143 | + } |
| 144 | +} |
0 commit comments