posts - 5,  comments - 6,  trackbacks - 0
  2010年1月10日

Inotify 是文件系统事件监控机制,计划包含在即将发布的 Linux 内核中作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。下面让我们一起学习如何安装 inotify 和如何构建一个示例用户空间应用程序来响应文件系统事件。

1.1同步工具安装
1、输入命令:su root,切换到超级用户。
2、先查看linux的内核是否支持inotify,支持inotify的内核最小为2.6.13,输入命令:uname –a。如下图所示,内核为2.6.27,支持inotify:
 
注:如果内核低于2.6.13,请升级内核或重新安装内核版本更高的linux系统。
3、建立同步ssh信任关系,输入命令:cd $HOME,进入用户根目录。
输入命令:ssh-keygen -t rsa (会出现几个提示信息,一直按回车即可)。
会在 cd $HOME/.ssh/目录下生成2个文件id_rsa、id_rsa.pub。
输入命令:cp  id_rsa.pub  authorized_keys,将id_rsa.pub拷贝成authorized_keys。
将授权密钥分发到iEPG服务器(192.168.100.101)上,输入命令:
scp  ~/.ssh/authorized_keys root@192.168.100.101:/root/.ssh/
如果有多台下载服务器,每台都须运行一次上面的密钥下发命令。
4、通过如下命令查看系统是否支持inotify:ll /proc/sys/fs/inotify
如果有如下输出,表示系统内核已经支持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、在系统下执行命令:man inotify、 man inotifywait、 man inotifywatch即可得到相应的帮助信息,表示inotify安装成功。
12、输入命令:rsync,查看rsync是否安装。
rsync一般是系统默认安装,如果没有安装就取得软件包,安装方法同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,给文件赋可执行权限。
4、输入命令:./syncapps.sh start,启动同步工具。
启动同步工具的输入命令:./syncapps.sh start
停止同步工具的输入命令:./syncapps.sh stop
重启同步工具的输入命令:./syncapps.sh restart
查看同步工具状态的输入命令:./syncapps.sh status
link
posted @ 2010-01-10 20:07 浔阳江头夜送客 阅读(1356) | 评论 (0)编辑 收藏

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

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

其次还要知道一点常识,就是我们在记事本等一些文本工具中 写的都是字符,没有谁会去写字节(可以写字节,但是要用具特殊的编辑器),但是其实,我们的写的是字符,但磁盘上真实存储的是字节。

这里就出现了转换的问题,当然,这些问题记事本本身会帮助我们解决。我们打开一个记事本,然后文件--另存为,你会发现有几种存储格式供您选择,
ANSI格式:就是ascii的格式
Unicode格式:采用国际通用的编码存储
Unicode big endian格式:这个和unicode有点区别,但我也不明太具体的不同
UTF-8:采用utf-8存储,看过上面的两篇文章,你会十分的了解这里介绍的编码。Utf-8,是unicode的一种实现方式。

例如我们在记事本里面输入“连通”两个字。

1.我们另存记事本的时候,采用unicode存储,那么虽然我们看到的字符还是“连通”,但是其实存储在磁盘上的字节 确实
8FDE(连) 901A (通),这个是规定的,unicode是国际上规定的,给世界上的每个字符分配的唯一编码。获取某个字符的unicode的方法,可以去网上查找,最简单的方法,就是打开word文档,输入字符,把光标移动到字符后面,按alt+x,word会自动把字符转换成unicode编码,这里呢我们也可以看到,用unicode存储汉字啊,每个汉字占用两个字节。

2.我们另存记事本的时候,采用utf-8存储,虽然我们看到的字符还是“连通”,但是其实存储在磁盘上的字节 确实已经变化了,这时候存储的是
E8 BF 9E (连)E9 80 9A(通)。这就是utf-8的存储的编码,至于utf-8为什么这样存储,你可以阅读上面的两篇文章来了解,可以看到,utf-8使用3个字节存储一个汉字。

另外我们还要知道的就是:电脑怎么区分一个记事本是用什么存储的呢?
换句话说,为什么我用unicode存储的8FDE(连) 901A (通),电脑就知道这是unicode编码,从而使用unicode解码,还原为“连通”呢?电脑又怎么知道E8 BF 9E (连)E9 80 9A(通)这是按照utf-8的存储方式存储的呢?

这里有一点标记,就是在存储字节的时候,记事本首先在最前面 标明,这个记事本下面的存储格式 是utf-8,还是unicode。

例如,

1.unicode存储“连通”。磁盘字节真实存储的其实是:

FF FE 8FDE 901A

前两个FF FE是标记,告诉电脑,这个文档的存储方式是unicode

2.utf-8存储“连通”。磁盘字节真实存储的其实是:

EF BB BF E8 BF 9E E9 80 9A

前三个EF BB BF 告诉电脑 这个文档是utf-8存储的

根据不同编码的特点和标志,对一个文本文件判断编码方法如下
1  .  UTF7  所有字节的内容不会大于127,也就是不大于&HFF
2  .  UTF8  起始三个字节为"0xEF 0xBB 0xBF"
3  .  UTF-16BE 起始三个字节为"0xFE  0xFF"
4  .  UTF-16LE 起始三个字节为"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) // 单独出现BF以下的,也算是GBK { break; } if (0xC0 <=
            * read && read <= 0xDF) { read = bis.read(); if (0x80 <= read &&
            * read <= 0xBF)// 双字节 (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 浔阳江头夜送客 阅读(389) | 评论 (0)编辑 收藏
  2009年10月19日
在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                 * 这是数组对象的是时候用到
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数组重写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 浔阳江头夜送客 阅读(2524) | 评论 (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 浔阳江头夜送客 阅读(1158) | 评论 (0)编辑 收藏
  2009年10月17日

关键字: webservice

1. Introduction
This document will outline the process of developing a JAX-WS web service and deploying it using MyEclipse 6.5 to the internal MyEclipse Tomcat server. The web service used in this tutorial will be a very simple calculator service that provides add, subtract, multiply and divide operations to the caller.

MyEclipse also supports developing web services using the existing XFire framework from previous MyEclipse releases. For folks needing to develop and deploy WebSphere JAX-RPC or WebSphere JAX-WS web services, please take a look at our MyEclipse Blue Edition of the MyEclipse IDE.

Additional resources covering web service creation using JAX-RPC, JAX-WS or XFire are included in the Resources section of this document.


2. System Requirements
This tutorial was created with MyEclipse 6.5. If you are using another version of MyEclipse (possibly newer), most of these screens and instructions should still be very similar.

If you are using a newer version of MyEclipse and notice portions of this tutorial looking different than the screens you are seeing, please let us know and we will make sure to resolve any inconsistencies.

3.新建一个工程
开始我们新建一个Web Service Project工程File->New->Web Service Project(Optional Maven Support)

 
Note:A JAX-WS web service can also be generated in any existing Java EE 5 web project.

我们给这个工程取名为WebServiceProject.注意JAX-WS支持只在javaEE5或更高版本的工程中是可行的。如果你需要使用低版本的工程类型(java1.4或者1.3),那么只能使用XFire Web Service代替JAX-WS。

 

这里我们使用上面的JAX—WS。
4.创建服务类
服务类就是一个普通的java类,负责提供我们想要发布的执行方法。这里我们写一个简单的计算器类,实现几个典型的计算器应用方法,如加减乘除。
首先我们先建一个包,WebServiceProject->src->new->package,取名com.myeclipseide.ws

让后我们在这个包下建一个类,Calculator.java.


根据上面提到的,这个计算器类实现计算器的加减乘除算法,简单实现:
Java代码

package com.myeclipseide.ws; 

public class Calculator 
public int add(int a, int b) 
return (a + b); 
}
 

public int subtract(int a, int b) 
return (a - b); 
}
 

public int multiply(int a, int b) 
return (a * b); 
}
 

public int divide(int a, int b) 
return (a / b); 
}
 
}
 


可以看出,这个类中的方法是非常简单的,没有用到特殊的注释还有接口,父类之类的东西。
5.创建一个Web Service
在上面的工具条中点击新建Web Service

Note:如果没有的话可以File->New->others->Myeclipse->WebService->webService
点击之后出现的屏幕,在Strategy中选择Bottom-up scenario,因为我们已经建立好了Calculator类而且想根据它建立JAX-WS服务。

下面是创建的最后一个屏幕,你需要选择提供webService方法的javaBean,在我们这个例子中就是我们已经建立好的Calculator类。

填好之后,Myeclipse会自动帮我们填满其他的项,Select Generate WSDL in project and hit Finish.


点击完成之后,Myeclipse会自动生成CalculatorDelegate代理类,还有一些必须的JAX-WS描述符,而且会自动在服务器目录下的web.xml中配置WebService的一些mappings,方便将webService部署到服务器中。

到此web service已经建立好了,我们开始部署它然后进行测试。
6.部署和测试webService。
这里我们不使用用Myeclipse自带的tomcat服务器,使用自己应经在电脑中部署好的tomcat5.5。
在server面板中右击,选择configure


部署自己的tomcat注意选择jdk要跟项目中的相同。

现在要向工程中导入JAX-WS的jar包
在项目名称上右击->properties->Add Library->Myeclipse Libraries->最后面的两个。

点击完成,导入成功。
Note:Myeclipse自带的tomcat中有自带的这两个jar包,可以不用导入。

6.1部署
在部署好的tomcat服务器上右击选择Add Deployment


点击完成。
6.2测试
运行tomcat服务器,在工具栏中点击launch WebService Explorer

打开后,点击右上角的WSDL视图,可以看到下面的屏幕

在WSDL URL中填写路径:http://localhost:8888/WebServiceProject/CalculatorPort?WSDL
解释下路径组成:
http://localhost:8888/是服务器的路径,我的端口号是8888,可以根据自己的更改,一般都是8080。
/WebServiceProject = We know by default the Web Context-root that is used to deploy(部署) web projects matches the name of the projects. (因为我们没有为这个工程自定义我们的Web Context-root,所以他就是这个工程的名字)
/CalculatorPort = As we saw from the last screenshot in Section #5, when our JAX-WS web service was generated, it was bound using a servlet-mapping in the web.xml file to the /CalculatorPort path.
XML代码
  

<servlet>     
    
<description>JAX-WS endpoint - CalculatorService</description>     
    
<display-name>CalculatorService</display-name>     
    
<servlet-name>CalculatorService</servlet-name>     
    
<servlet-class>     
        com.sun.xml.ws.transport.http.servlet.WSServlet      
    
</servlet-class>     
    
<load-on-startup>1</load-on-startup>     
  
</servlet>     
  
<servlet-mapping>     
    
<servlet-name>CalculatorService</servlet-name>     
    
<url-pattern>/CalculatorPort</url-pattern>     
  
</servlet-mapping>    
<servlet>  
    
<description>JAX-WS endpoint - CalculatorService</description>  
    
<display-name>CalculatorService</display-name>  
    
<servlet-name>CalculatorService</servlet-name>  
    
<servlet-class>  
        com.sun.xml.ws.transport.http.servlet.WSServlet   
    
</servlet-class>  
    
<load-on-startup>1</load-on-startup>  
  
</servlet>  
  
<servlet-mapping>  
    
<servlet-name>CalculatorService</servlet-name>  
    
<url-pattern>/CalculatorPort</url-pattern>  
  
</servlet-mapping>

 WSDL = This is a universal query string argument that can be added to the end of any web service which will tell the web service to return it's full WSDL to the caller. In this case, the WSDL is returned to our Web Services Explorer tool which loads it up, and displays the web services exposed operations to us.
弄清楚之后,我们开始测试,比如我们选择add方法:
填写args,点击go,在status中就会显示结果。  

结果是正确的。
7.创建Webservice Client
现在我们已经部署好Webservice,而且应经测试过了,那我们新建一个Webservice client,来调用Webservice提供的方法。
7.1新建一个java project,给他取个名字。比如我们叫它ClientofWebService


在工具条中点击new Web Service Client

然后按照以下步骤操作:


The last step of the web service client creation is to specify either a WSDL File or a WSDL URL for the wizard to retrieve the web service WSDL from. In our case we are using the URL and generate the client into the new package com.myeclipseide.ws.client:

http://localhost:8888/WebServiceProject/CalculatorPort?WSDL


点击Next知道完成。
可以看到在新建的java project ClientofWebService中,src文件夹下产生了许多的文件,根据名称我们大体可以了解其意思,可以打开看一下源代码,其实不难理解。比如add文件,就是Calculator类中add方法的两个参数的get和set方法。其他类似。
我们在文件夹下见一个类test.java写一个main函数测试
Java代码     

public static void main(String[] args) {    
    
/* Create the service instance */    
    CalculatorService service 
= new CalculatorService();    
    CalculatorDelegate delegate 
= service.getCalculatorPort();    
  
    
/* Using the web service, perform the 4 calculations */    
    System.out.println( 
"1. 3+7=" + delegate.add(37));    
    System.out.println( 
"2. 12-2=" + delegate.subtract(122));    
    System.out.println( 
"3. 9*9=" + delegate.multiply(99));    
    System.out.println( 
"4. 40/2=" + delegate.divide(402));    
}
   

 运行得到如下结果:
1. 3+7=10
2. 12-2=10
3. 9*9=81
4. 40/2=20
测试完成。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/foart/archive/2009/06/21/4287515.aspx

posted @ 2009-10-17 22:50 浔阳江头夜送客 阅读(3004) | 评论 (6)编辑 收藏
仅列出标题  
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔分类

随笔档案

myeclipse6.5上基于JAX-WS开发Webservice(中文示例)

搜索

  •  

最新评论

阅读排行榜

评论排行榜