posts - 5,  comments - 6,  trackbacks - 0
            2009年10月19日

          Inotify 是文件系統(tǒng)事件監(jiān)控機制,計劃包含在即將發(fā)布的 Linux 內核中作為 dnotify 的有效替代。dnotify 是較早內核支持的文件監(jiān)控機制。Inotify一種強大的、細粒度的、異步的機制,它滿足各種各樣的文件監(jiān)控需要,不僅限于安全和性能。下面讓我們一起學習如何安裝 inotify 和如何構建一個示例用戶空間應用程序來響應文件系統(tǒng)事件。

          1.1同步工具安裝
          1、輸入命令:su root,切換到超級用戶。
          2、先查看linux的內核是否支持inotify,支持inotify的內核最小為2.6.13,輸入命令:uname –a。如下圖所示,內核為2.6.27,支持inotify:
           
          注:如果內核低于2.6.13,請升級內核或重新安裝內核版本更高的linux系統(tǒng)。
          3、建立同步ssh信任關系,輸入命令:cd $HOME,進入用戶根目錄。
          輸入命令:ssh-keygen -t rsa (會出現(xiàn)幾個提示信息,一直按回車即可)。
          會在 cd $HOME/.ssh/目錄下生成2個文件id_rsa、id_rsa.pub。
          輸入命令:cp  id_rsa.pub  authorized_keys,將id_rsa.pub拷貝成authorized_keys。
          將授權密鑰分發(fā)到iEPG服務器(192.168.100.101)上,輸入命令:
          scp  ~/.ssh/authorized_keys root@192.168.100.101:/root/.ssh/
          如果有多臺下載服務器,每臺都須運行一次上面的密鑰下發(fā)命令。
          4、通過如下命令查看系統(tǒng)是否支持inotify:ll /proc/sys/fs/inotify
          如果有如下輸出,表示系統(tǒng)內核已經(jīng)支持inotify:
          total 0
          -rw-r--r-- 1 root root 0 Feb 21 01:15 max_queued_events
          -rw-r--r-- 1 root root 0 Feb 21 01:15 max_user_instances
          -rw-r--r-- 1 root root 0 Feb 21 01:15 max_user_watches
          5、取得軟件包inotify-tools-3.13.tar.gz,放在/tmp下。
          6、輸入命令:tar zvxf inotify-tools-3.13.tar.gz,解壓軟件包。
          7、輸入命令:cd inotify-tools-3.13,進入解壓后的目錄。
          8、輸入命令:./configure
          9、輸入命令:make
          10、輸入命令:make install
          11、在系統(tǒng)下執(zhí)行命令:man inotify、 man inotifywait、 man inotifywatch即可得到相應的幫助信息,表示inotify安裝成功。
          12、輸入命令:rsync,查看rsync是否安裝。
          rsync一般是系統(tǒng)默認安裝,如果沒有安裝就取得軟件包,安裝方法同inotify。

          同步腳本使用
          1、取得syncapps.sh腳本

          #!/bin/sh
              SRC=/root/sys/
              SEND=iEPGService.dat
              PID_FILE=syncapps.pid
              
              function sync_files
              {
                 cat $SEND | while read DST 
                 do
                 rsync -avzq  --delete --exclude '/.version' --exclude '/.bak' $SRC $DST
                 done
                  
              }
              
              function inotify_func
              {
                  inotifywait -mrq -e modify,delete,create ${SRC} | while read D E F;do
                      # echo "$D : $E : $F"
                      sync_files
                  done
              }
              
              function stop
              {
                  pkill inotifywait &>/dev/null && rm -f ${PID_FILE} &> /dev/null
              }
              
              case $1 in
                  stop)
                      echo -n "Stopping sync service"
                      if [ -e ${PID_FILE} ]; then
                          stop
                          echo "Stopped"
                          exit 0
                      else
                          echo "pid file not found"
                          exit 2
                      fi
                      ;;
                  start) 
                      echo -n "Starting sync service"
                      if [ -f ${PID_FILE} ] && ((`ps awux | grep -v grep | grep -c inotifywait`)); then
                          echo " already running: pid file found ($PID_FILE) and an inotifywait process is running"
                          exit 1
                      elif [ -f ${PID_FILE} ]; then
                          echo -n "(stale pid file)"
                      fi                        
                      
                      sync_files
                      inotify_func&
                      
                      pid="$!"
                      ps --ppid $pid -o pid,cmd | grep inotifywait | awk '{print $1}' > ${PID_FILE}
                      
                      echo "Started"
                      ;;
                  restart)
                      $0 stop
                      $0 start
                      exit 0
                      ;;
                  status)
                      echo -n "Getting status for syncer service "
                      pid=`cat ${PID_FILE} 2>/dev/null`
                      if [ -f ${PID_FILE} ] && ((`ps awux | grep -v grep | egrep -c "$pid.*inotifywait"`)); then
                          echo "running (pid $pid)"
                          exit 0
                      elif [ -f ${PID_FILE} ]; then
                          echo "not runing (pid file found $pid)"
                          exit 3
                      elif ((`ps awux | grep -v grep | egrep -c "$pid.*inotifywait"`)); then
                          echo "not running (inotifywait procs found)"
                          exit 4
                      else
                          echo "not running"
                          exit 5
                      fi
                      ;;
                              
                  *)
                      echo "Usage error"
                      echo "Usage: $0 
          <start|stop|restart|status>"
                      ;;
              esac
          2、取得iEPGService.dat腳本。
            root@10.10.80.76:/root/files/
          3、輸入命令:chmod  +x  *.sh,給文件賦可執(zhí)行權限。
          4、輸入命令:./syncapps.sh start,啟動同步工具。
          啟動同步工具的輸入命令:./syncapps.sh start
          停止同步工具的輸入命令:./syncapps.sh stop
          重啟同步工具的輸入命令:./syncapps.sh restart
          查看同步工具狀態(tài)的輸入命令:./syncapps.sh status
          link
          posted @ 2010-01-10 20:07 潯陽江頭夜送客 閱讀(1384) | 評論 (0)編輯 收藏

          首先閱讀此文之前,最好閱讀
          http://hi.baidu.com/maml897/blog/item/324bf86369961ed4e6113a5c.html

          http://hi.baidu.com/maml897/blog/item/fa5f0a7e1edef00129388ae2.html

          其次還要知道一點常識,就是我們在記事本等一些文本工具中 寫的都是字符,沒有誰會去寫字節(jié)(可以寫字節(jié),但是要用具特殊的編輯器),但是其實,我們的寫的是字符,但磁盤上真實存儲的是字節(jié)。

          這里就出現(xiàn)了轉換的問題,當然,這些問題記事本本身會幫助我們解決。我們打開一個記事本,然后文件--另存為,你會發(fā)現(xiàn)有幾種存儲格式供您選擇,
          ANSI格式:就是ascii的格式
          Unicode格式:采用國際通用的編碼存儲
          Unicode big endian格式:這個和unicode有點區(qū)別,但我也不明太具體的不同
          UTF-8:采用utf-8存儲,看過上面的兩篇文章,你會十分的了解這里介紹的編碼。Utf-8,是unicode的一種實現(xiàn)方式。

          例如我們在記事本里面輸入“連通”兩個字。

          1.我們另存記事本的時候,采用unicode存儲,那么雖然我們看到的字符還是“連通”,但是其實存儲在磁盤上的字節(jié) 確實
          8FDE(連) 901A (通),這個是規(guī)定的,unicode是國際上規(guī)定的,給世界上的每個字符分配的唯一編碼。獲取某個字符的unicode的方法,可以去網(wǎng)上查找,最簡單的方法,就是打開word文檔,輸入字符,把光標移動到字符后面,按alt+x,word會自動把字符轉換成unicode編碼,這里呢我們也可以看到,用unicode存儲漢字啊,每個漢字占用兩個字節(jié)。

          2.我們另存記事本的時候,采用utf-8存儲,雖然我們看到的字符還是“連通”,但是其實存儲在磁盤上的字節(jié) 確實已經(jīng)變化了,這時候存儲的是
          E8 BF 9E (連)E9 80 9A(通)。這就是utf-8的存儲的編碼,至于utf-8為什么這樣存儲,你可以閱讀上面的兩篇文章來了解,可以看到,utf-8使用3個字節(jié)存儲一個漢字。

          另外我們還要知道的就是:電腦怎么區(qū)分一個記事本是用什么存儲的呢?
          換句話說,為什么我用unicode存儲的8FDE(連) 901A (通),電腦就知道這是unicode編碼,從而使用unicode解碼,還原為“連通”呢?電腦又怎么知道E8 BF 9E (連)E9 80 9A(通)這是按照utf-8的存儲方式存儲的呢?

          這里有一點標記,就是在存儲字節(jié)的時候,記事本首先在最前面 標明,這個記事本下面的存儲格式 是utf-8,還是unicode。

          例如,

          1.unicode存儲“連通”。磁盤字節(jié)真實存儲的其實是:

          FF FE 8FDE 901A

          前兩個FF FE是標記,告訴電腦,這個文檔的存儲方式是unicode

          2.utf-8存儲“連通”。磁盤字節(jié)真實存儲的其實是:

          EF BB BF E8 BF 9E E9 80 9A

          前三個EF BB BF 告訴電腦 這個文檔是utf-8存儲的

          根據(jù)不同編碼的特點和標志,對一個文本文件判斷編碼方法如下
          1  .  UTF7  所有字節(jié)的內容不會大于127,也就是不大于&HFF
          2  .  UTF8  起始三個字節(jié)為"0xEF 0xBB 0xBF"
          3  .  UTF-16BE 起始三個字節(jié)為"0xFE  0xFF"
          4  .  UTF-16LE 起始三個字節(jié)為"0xFF  0xFE"


          import java.io.BufferedInputStream;
          import java.io.File;
          import java.io.FileInputStream;

          public class FileEncodeReferee
          {
              
          private File file;
              
              
          public FileEncodeReferee(File file)
              
          {
                  
          this.file = file;
              }

              
              
          public FileEncodeReferee(String path)
              
          {
                  file 
          = new File(path);
              }

              
              
          public String getCharset()
              
          {
                  File file 
          = this.file;
                  
                  String charset 
          = "GBK";
                  
          byte[] first3Bytes = new byte[3];
                  BufferedInputStream bis 
          = null;
                  
          try
                  
          {
                      
          //boolean checked = false;
                      bis = new BufferedInputStream(new FileInputStream(file));
                      bis.mark(
          0);
                      
          int read = bis.read(first3Bytes, 03);
                      
          if (read == -1)
                      
          {
                          
          return charset;
                      }

                      
          if (first3Bytes[0== (byte0xFF && first3Bytes[1== (byte0xFE)
                      
          {
                          charset 
          = "UTF-16LE";
                          
          //checked = true;
                      }

                      
          else if (first3Bytes[0== (byte0xFE
                              
          && first3Bytes[1== (byte0xFF)
                      
          {
                          charset 
          = "UTF-16BE";
                          
          //checked = true;
                      }

                      
          else if (first3Bytes[0== (byte0xEF
                              
          && first3Bytes[1== (byte0xBB
                              
          && first3Bytes[2== (byte0xBF)
                      
          {
                          charset 
          = "UTF-8";
                          
          //checked = true;
                      }

                      
          /** */
                      
          /*******************************************************************
                      * bis.reset(); if (!checked) { int loc = 0; while ((read =
                      * bis.read()) != -1) { loc++; if (read >= 0xF0) { break; } if (0x80 <=
                      * read && read <= 0xBF) // 單獨出現(xiàn)BF以下的,也算是GBK { break; } if (0xC0 <=
                      * read && read <= 0xDF) { read = bis.read(); if (0x80 <= read &&
                      * read <= 0xBF)// 雙字節(jié) (0xC0 - 0xDF) { // (0x80 - 0xBF),也可能在GB編碼內
                      * continue; } else { break; } } else if (0xE0 <= read && read <=
                      * 0xEF) { // 也有可能出錯,但是幾率較小 read = bis.read(); if (0x80 <= read &&
                      * read <= 0xBF) { read = bis.read(); if (0x80 <= read && read <=
                      * 0xBF) { charset = "UTF-8"; break; } else { break; } } else {
                      * break; } } } System.out.println(loc + " " +
                      * Integer.toHexString(read)); }
                      *****************************************************************
          */

                  }

                  
          catch (Exception e)
                  
          {
                      e.printStackTrace();
                  }

                  
          finally
                  
          {
                      
          if (bis != null)
                      
          {
                          
          try
                          
          {
                              bis.close();
                          }

                          
          catch (Exception ex)
                          
          {
                              ex.printStackTrace();
                          }

                      }

                  }

                  
          return charset;
              }

              
              
          public static void main(String[] args)
              
          {
                  FileEncodeReferee fer 
          = new FileEncodeReferee("F://鎖表1.sql");
                  System.out.println(fer.getCharset());
              }

          }

          posted @ 2010-01-10 19:56 潯陽江頭夜送客 閱讀(401) | 評論 (0)編輯 收藏
          在cglib 中 BeanMap的用法

          1.導入cglib-nodep-2.1.3.jar
           
          2.在javaBean 對象中重寫toString()方法  比如是UserManageVo.Java
           public String toString(){
            return BeanTools.getBeanDesc(UserManageVo.this);
           }
          java 代碼
           1package BeanUtils;
           2
           3import net.sf.cglib.beans.BeanMap;
           4
           5public class BeanTools {
           6    private static String LINE = System.getProperty("line.separator""\r\n");
           7
           8    /**
           9     * 對象中重寫toString()方法,在打印日志的時候調用
          10     * @param obj
          11     * @return
          12     * @return String
          13     */

          14    public static String getBeanDesc(Object obj) {
          15        StringBuffer bf = new StringBuffer();
          16        bf.append(LINE + "{" + LINE + "Class = " + obj.getClass().getName()
          17                + LINE);
          18        BeanMap beanMap = BeanMap.create(obj);
          19        for (Object object : beanMap.keySet()) {
          20            Object value = beanMap.get(object);
          21            if (null != value) {
          22                /**
          23                 * 這是定義對象的是時候用到
          24                 */

          25                String className = value.getClass().getName();
          26                if (className.startsWith("test.UserManageEvent")
          27                        || className.startsWith("test.BasicEvent")
          28                        || className.startsWith("test.UserManageVo")) {
          29                    bf.append(object + " = " + getBeanDesc(value) + LINE);
          30                }

          31
          32                /**
          33                 * 這是數(shù)組對象的是時候用到
          34                 */

          35                if (className.startsWith("Ltest.UserManageEvent")
          36                        || className.startsWith("Ltest.BasicEvent")
          37                        || className.startsWith("Ltest.UserManageVo")) {
          38                    Object[] objs = (Object[]) value;
          39                    for (int i = 0; i < objs.length; i++{
          40                        bf.append(object + " = " + getBeanDesc(objs[i]) + LINE);
          41                    }

          42                }

          43                
          44                /**
          45                 * 對String數(shù)組重寫toString()方法
          46                 */

          47                if (className.startsWith("[Ljava.lang.String")) {
          48                    Object[] objs = (Object[]) value;
          49                    for (int i = 0; i < objs.length; i++{
          50                        bf.append(object + "[" + i + "]" + " = " + objs[i]
          51                                + LINE);
          52                    }

          53                }

          54            }

          55            bf.append(object + " = " + value + LINE);
          56        }

          57        bf.append("}");
          58        return bf.toString();
          59    }

          60}

          java代碼
          /Files/yjlongfei/beanUtil.rar
          posted @ 2009-10-19 21:41 潯陽江頭夜送客 閱讀(2546) | 評論 (0)編輯 收藏
          一、簡介:

                  BeanUtils提供對Java反射和自省API的包裝。其主要目的是利用反射機制對JavaBean的屬性進行處理。我們知道,一個JavaBean通常包含了大量的屬性,很多情況下,對JavaBean的處理導致大量get/set代碼堆積,增加了代碼長度和閱讀代碼的難度。

          二、用法:

                  如果你有兩個具有很多相同屬性的JavaBean,我們對一個對象copy 到另外一個對象,可用用下面的方法。

          1. 導入commons-beanutils.jar
          2. 導入commons-logging-1.1.jar
          3. 構建UserManageVo , UserManageEvent 對象 ,這兩個對象的屬性相同
          4. 調用 BeanUtils.copyProperties(UserManageVo, UserManageEvent)
           java 主要代碼

           1import java.lang.reflect.InvocationTargetException;
           2import org.apache.commons.beanutils.BeanUtils;
           3import test.BasicEvent;
           4import test.UserManageEvent;
           5import test.UserManageVo;
           6
           7public class TestCase {
           8    
           9    public static void main(String[] args) {
          10        UserManageEvent event = new UserManageEvent();
          11        event.setName("zhangsan");
          12        event.setUserId("1");
          13        
          14        BasicEvent basicEvt = new BasicEvent();
          15        basicEvt.setEventId("2");
          16        basicEvt.setVersion("version");
          17        
          18        event.setEvent(basicEvt);
          19        UserManageVo vo = new UserManageVo();
          20        try {
          21            BeanUtils.copyProperties(vo, event);
          22            System.out.println(vo.getUserId());
          23            System.out.println(vo.getName());
          24            System.out.println(vo.getEvent());
          25        }
           catch (IllegalAccessException e) {
          26            e.printStackTrace();
          27        }
           catch (InvocationTargetException e) {
          28            e.printStackTrace();
          29        }
           
          30    }

          31}


          java代碼:
          /Files/yjlongfei/test.rar

          posted @ 2009-10-19 21:21 潯陽江頭夜送客 閱讀(1171) | 評論 (0)編輯 收藏
          <2009年10月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          myeclipse6.5上基于JAX-WS開發(fā)Webservice(中文示例)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大竹县| 和顺县| 巴塘县| 丽江市| 闸北区| 青川县| 上杭县| 上高县| 平江县| 西丰县| 临汾市| 成安县| 扶风县| 潍坊市| 娄烦县| 吴川市| 汤原县| 毕节市| 井冈山市| 东乡族自治县| 霍山县| 广州市| 屯门区| 安徽省| 宜宾县| 东乡| 汾西县| 铅山县| 宜昌市| 吉隆县| 类乌齐县| 巨鹿县| 安泽县| 阜新| 定南县| 黄平县| 资中县| 信丰县| 泽州县| 四川省| 日照市|