download:Zookeeper source code analysis

Zookeeper is the perfect registration center for Alibaba and Dubbo, and its status is undeniable. This course starts with the diagram of the framework and components of Zookeeper framework, and then “divide and conquer” the source code of each component of Zookeeper framework, such as serialization, data model, persistence, Client and Server architecture, election process, etc. Finally, it is summarized and sorted out quickly. Suitable for people with code cleanliness in the middle and senior Java engineers engaged in distributed system design and development geek approved “source-driven” learning style student party technical reserve requires a solid Java foundation, Knowledge of IO/NIO network communication knowledge of serialization, persistence, multithreading Understanding of the general use of ZooKeeper command package whu.extract. Pubtime. core; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import whu.utils.TimeUtil; / * *

  • Created On March 13, 2014 2:49:05 PM

  • @description gets the publication time of the web page

    / public class FetchPubTime {/ ** indicates a sequential 8-bit date in the URL. Private static String url_reg

    whole= “([-|/|

    ]{1}20\d{6})”; /** Indicates dates separated by – or /. Private static String url_reg_sep

    ymd = “([-|/|

    ]{1}20\d{2}[-|/|

    ] {1} \ d {1, 2} [- | / |

    ] {1} \ d {1, 2}) “; Private static String url_reg_sep private static String url_reg_sep private static String url_reg_sep

    ym = “([-|/|

    ] 20 \ d {1} {2} – | / | _ {1} \ d {1, 2}) “; private static Calendar current = Calendar.getInstance(); /** a well-formed time regular expression

    / private static String rightTimeReg = “^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]? ((((0? [13578])|(1[02]))[\-\/\s]? ((0? [1-9]) | ([1-2] [0-9]) | | (3 [01]))) (((0? [469])|(11))[\-\/\s]? ((0? [1-9]) | ([1-2] [0-9]) | | (30))) (0? 2[\-\/\s]? ((0? [1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]? ((((0? [13578])|(1[02]))[\-\/\s]? ((0? [1-9]) | ([1-2] [0-9]) | | (3 [01]))) (((0? [469])|(11))[\-\/\s]? ((0? [1-9]) | ([1-2] [0-9]) | | (30))) (0? 2[\-\/\s]? ((0? [1-9])|(1[0-9])|(2[0-8]))))))(\s(((0? [0-9]) | ([1-2] [0, 3])) \ [[0 to 5)? [0-9])((\s)|(\:([0-5]? [0-9])))))? $”;

    / * *

    • @param url

    • @param urlContent

    • @return

      */

      public static String getPubTimeVarious(String url,String urlContent) {

      String pubTime = getPubTimeFromUrl(url);

      If (pubTime == null) {if(urlContent! =null&&! urlContent.trim().equals(“”)) return extractPageDate(urlContent); }

      return pubTime;

      }

    /** Extract the time from the url and return a string in the format YYYY-MM-DD HH: MM :ss

    • @param url

    • @return */ public static String getPubTimeFromUrl(String url) { Pattern p_whole = Pattern.compile(url_reg_whole); Matcher m_whole = p_whole.matcher(url); if(m_whole.find(0)&&m_whole.groupCount()>0) { String time = m_whole.group(0); time = time.substring(1,time.length()); If (current.compareTo(TimeUtil. StrToCalendar (time, “YyyyMMdd”)) > = 0) {return time. The substring (0, 4) + “-” + time. The substring (4, 6) + “-” + time. The substring (6, 8) + “” +” 00:00:00 “; }}

      p_whole = null;

      m_whole = null;

      Pattern p_sep = Pattern.compile(url_reg_sep_ymd);

      Matcher m_sep = p_sep.matcher(url);

      if(m_sep.find(0)&&m_sep.groupCount()>0)

      {

      String time = m

      sep.group(0);

      time = time.substring(1,time.length());

      String[] seg = time.split(“[-|/|

      ] {1} “); Calendar theTime = Calendar.getInstance(); theTime.set(Calendar.YEAR,Integer.parseInt(seg[0])); theTime.set(Calendar.MONTH, Integer.parseInt(seg[1])); theTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(seg[2])); if(current.compareTo(theTime)>=0) {

      return seg[0]+"-"+seg[1]+"-"+seg[2]+" "+"00:00:00";
          }
      Copy the code

      }

      p_sep = null;

      m_sep = null;

      Pattern p_sep_ym = Pattern.compile(url_reg_sep_ym);

      Matcher m_sep_ym = p_sep_ym.matcher(url);

      if(m_sep_ym.find(0)&&m_sep_ym.groupCount()>0)

      {

      String time = m_sep

      ym.group(0);

      time = time.substring(1,time.length());

      Calendar theTime = Calendar.getInstance();

      String[] seg = time.split(“[-|/|

      ] {1} “); theTime.set(Calendar.YEAR,Integer.parseInt(seg[0])); theTime.set(Calendar.MONTH, Integer.parseInt(seg[1])); theTime.set(Calendar.DAY_OF_MONTH, 1); if(current.compareTo(theTime)>=0) {

      return seg[0]+"-"+seg[1]+"-"+"01"+" "+"00:00:00";
      }
      Copy the code

      }

      return null;

      }

    /** Fetch the source time from the web page

    • Regular expressions in Java extract date completion code from a string

    • December 19, 2013 15:58:42

    • Read the time in 2013-12-19 15:48:33 or 2013-12-19 or 2012/3/05 mode

    • @param text Specifies the string to be extracted

    • @return Returns the date

    • @author: oschina

    • @Createtime: Jan 21, 2013 */ public static String extractPageDate(String text) { boolean containsHMS =false; String dateStr = text.replaceAll(“r? n”, ” “); try { List matches = null; The Pattern running p_detail = Pattern.com (” (20 \ d {2} [- /] \ d {1, 2} [- /] \ d \ d {1, 2} {1, 2} : \ d {1, 2} : \ d {1, 2}) | (20 \ d \ d {1, 2} {2} years month \ d {1, 2}) “, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE); // If only the date (month, year, day, hour, minute, second) is extracted, Is in accordance with the following the Pattern p = Pattern.com running (” (20 \ d {2} [- /] \ d {1, 2} [- /] \ d {1, 2}) | (20 \ d \ d {1, 2} {2} year month \ d {1, 2}) “, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE); //Matcher matcher = p.matcher(dateStr); Matcher matcher_detail = p_detail.matcher(dateStr);

      if(! (matcher_detail.find(0) && matcher_detail.groupCount() >= 1)) { matcher_detail = p.matcher(dateStr); containsHMS = true; }else matcher_detail = p_detail.matcher(dateStr); if (matcher_detail.find() && matcher_detail.groupCount() >= 1) { matches = new ArrayList(); for (int i = 1; i <= matcher_detail.groupCount(); i++) { String temp = matcher_detail.group(i); matches.add(temp); } } else { matches = Collections.EMPTY_LIST; } if (matches.size() > 0) { for(int i=0; i<matches.size(); i++) { String pubTime = matches.get(i).toString().trim(); / / remove the first value pubTime = pubTime. Replace ("/", "-"). The replace (" year ", "-"). The replace (" month ", "-"). The replace (" day ", "-"); if(current.compareTo(TimeUtil.strToCalendar(pubTime, "yyyy-MM-dd"))>=0) { if(containsHMS) pubTime+=" "+"00:00:00"; if(pubTime.matches(rightTimeReg)) { return pubTime; } } } } else { return null; }Copy the code

      } catch (Exception e) { return null; } return null; }}