Java tutorial
/* * Licensed to JIVR under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. JIVR 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 me.bulat.jivr.webmin.consul.service; import me.bulat.jivr.consul.manager.ConsulManager; import me.bulat.jivr.consul.manager.ServiceState; import me.bulat.jivr.consul.model.NodeKey; import me.bulat.jivr.consul.model.ServiceKey; import me.bulat.jivr.webmin.consul.node.NodeRow; import me.bulat.jivr.webmin.consul.node.NodeRowList; import me.bulat.jivr.webmin.consul.node.ServiceRow; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.util.List; import java.util.concurrent.*; import java.util.stream.Collectors; /** * @author Alex Bogatikov * Created on 14/09/16. */ @Service public class ConsulServiceImpl implements ConsulService { /** * Health check service. */ private final ScheduledExecutorService healthCheck; /** * consul manager. */ private ConsulManager consul; @Autowired public ConsulServiceImpl(final ConsulManager consul) { this.consul = consul; this.healthCheck = Executors.newScheduledThreadPool(1, new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, "JIVR-WEB-HEALTH-SERVICE"); } }); } @PostConstruct @Override public void registerInConsul() { consul.registerNode(); healthCheck.scheduleAtFixedRate(this::run, 0, 4L, TimeUnit.SECONDS); } @PreDestroy @Override public void unRegisterInConsul() { consul.unRegisterNode(); healthCheck.shutdown(); } /** * set data to consul. */ private void run() { consul.setNodeState(ServiceState.passing, "JIvr WebMin working"); } @Override public NodeRowList getNodeList() { List<NodeRow> rows = consul.getNodeList().stream().map(this::collectNode).collect(Collectors.toList()); NodeRowList result = new NodeRowList(); result.getRows().addAll(rows); return result; } private NodeRow collectNode(final NodeKey key) { NodeRow nodeRow = new NodeRow(); nodeRow.setNodeName(key.getNodeName().replaceAll("nodes/", "").replaceAll("/", "")); nodeRow.setNodeStatus(consul.getNodeState(nodeRow.getNodeName()).toString()); nodeRow.getServiceRowList().addAll(key.getServices().stream() .map(s -> collectService(nodeRow.getNodeName(), s)).collect(Collectors.toList())); return nodeRow; } private ServiceRow collectService(final String nodeName, final ServiceKey serviceKey) { ServiceRow serviceRow = new ServiceRow(); serviceRow.setServiceName(serviceKey.getServiceName()); serviceRow.setNodeName(nodeName); serviceRow.setServiceStatus(consul.getServiceStatus(nodeName, serviceRow.getServiceName()).toString()); return serviceRow; } }