com.mengka.diamond.server.service.NotifyService.java Source code

Java tutorial

Introduction

Here is the source code for com.mengka.diamond.server.service.NotifyService.java

Source

/*
 * (C) 2007-2012 Alibaba Group Holding Limited.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 * Authors:
 *   leiwen <chrisredfield1985@126.com> , boyan <killme2008@gmail.com>
 */
package com.mengka.diamond.server.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

import com.mengka.diamond.common.DiamondConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mengka.diamond.domain.ConfigInfo;
import com.mengka.diamond.server.utils.SystemConfig;

/**
 * ?
 * 
 * @author boyan
 * @date 2010-5-6
 */
@Service
public class NotifyService {
    private static final int TIMEOUT = 5000;

    private final String URL_PREFIX = "/notify.do";//?diamond-server

    private final String PROTOCOL = "http://";

    @Autowired
    private ConfigService configService;
    //??
    //private final Properties nodeProperties = new Properties();

    static final Log log = LogFactory.getLog(NotifyService.class);

    Properties getNodeProperties() {
        ConfigInfo info = configService.findConfigInfo(DiamondConstants.SERVER_ADDRESS_AND_IP,
                DiamondConstants.DIAMOND_ADMIN_GROUP_ID);
        Properties nodeProperties = new Properties();
        if (info != null && !StringUtils.isEmpty(info.getContent())) {
            try {
                nodeProperties.load(new StringReader(info.getContent()));
            } catch (IOException e) {
                log.error("?", e);
                new RuntimeException("?", e);
            }
        } else {
            log.error(String.format("?,?dataid:%s,groupid:%s ?",
                    DiamondConstants.DIAMOND_ADMIN_DATA_ID, DiamondConstants.DIAMOND_ADMIN_GROUP_ID));
        }
        log.info(":" + nodeProperties);
        return nodeProperties;
    }

    //    @PostConstruct
    //    public void loadNodes() {
    //        InputStream in = null;
    //        try {
    //            in = ResourceUtils.getResourceAsStream("node.properties");
    //            nodeProperties.load(in);
    //        }
    //        catch (IOException e) {
    //            log.error("?");
    //        }
    //        finally {
    //            try {
    //                if (in != null)
    //                    in.close();
    //            }
    //            catch (IOException e) {
    //                log.error("node.properties", e);
    //            }
    //        }
    //        log.info(":" + nodeProperties);
    //    }

    /**
     * ???
     * @param id
     */
    public void notifyConfigInfoChange(String dataId, String group) {
        Properties nodeProperties = getNodeProperties();
        Enumeration<?> enu = nodeProperties.propertyNames();
        while (enu.hasMoreElements()) {
            String address = (String) enu.nextElement();
            if (address.contains(SystemConfig.LOCAL_IP)) {
                continue;
            }
            //?url
            String urlString = generateNotifyConfigInfoPath(dataId, group, address, nodeProperties);
            //?get
            final String result = invokeURL(urlString);
            log.info("" + address + "??" + result);
        }
    }

    /**
     * ?url
     * http://192.168.1.109:8136/notify.do?method=notifyConfigInfo&dataId=***&group=***
     */
    String generateNotifyConfigInfoPath(String dataId, String group, String address, Properties nodeProperties) {
        String specialUrl = nodeProperties.getProperty(address);
        String urlString = PROTOCOL + address + URL_PREFIX;
        // urlurl
        if (specialUrl != null && StringUtils.hasLength(specialUrl.trim())) {
            urlString = specialUrl;
        }
        urlString += "?method=notifyConfigInfo&dataId=" + dataId + "&group=" + group;
        return urlString;
    }

    /**
     * http get
     * 
     * @param urlString
     * @return
     */
    private String invokeURL(String urlString) {
        HttpURLConnection conn = null;
        URL url = null;
        try {
            url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(TIMEOUT);
            conn.setReadTimeout(TIMEOUT);
            conn.setRequestMethod("GET");
            conn.connect();
            InputStream urlStream = conn.getInputStream();
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(urlStream));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } finally {
                if (reader != null)
                    reader.close();
            }
            return sb.toString();

        } catch (Exception e) {
            log.error("http,url=" + urlString, e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return "error";
    }
}