6.8 并行读写远程文件
FttpAdapter是通过FttpReadAdapter的tryReadAll方法进行并行读:
- FttpAdapter fa = new FttpAdapter("fttp://192.168.0.1/home/log/1.log");
- Result<byte[]> rs = fa.getFttpReader().tryReadAll();
调用tryReadAll会立即返回一个Result<byte[]>,但是不能马上获取到结果值,需要轮循检查它的状态是否就绪。
rs.getStatus()有三种状态:
❏ Result.NOTREADY未就绪
❏ Result.READY就绪
❏ Result.EXCEPTION异常
轮循直到状态准备就绪:
- while(rs.getStatus()==Result.NOTREADY);
状态就绪就可以通过getResult()获取到读取结果:
- byte[] bts = rs.getResult();
可以对一个远程文件的不同部分同时并行读写,也可以对多个远程文件同时并行读写,比如:
- String fttppath = "fttp:// 192.168.0.1/home/log/1.log";
- FttpAdapter fa0 = new FttpAdapter(fttppath);
- FttpAdapter fa1 = new FttpAdapter(fttppath);
- FttpAdapter fa2 = new FttpAdapter(fttppath);
- Result<byte[]> rs0 = fa0.getFttpReader(0,5).tryReadAll();
- Result<byte[]> rs1 = fa1.getFttpReader(5,5).tryReadAll();
- Result<byte[]> rs2 = fa2.getFttpReader(10,5).tryReadAll();
上面是3个同时并行的读取,分别从一个文件的0、5、10位置向后读取5个字节。
如果是并行写,则是:
- Result<Integer>[] rs0 = fa0.getFttpWriter(0,5).tryWrite("hello".getBytes());
- Result<Integer>[] rs1 = fa1.getFttpWriter(5,5).tryWrite("world".getBytes());
- Result<Integer>[] rs2 = fa2.getFttpWriter(10,5).tryWrite("fttp!".getBytes());
注意
上面代码中的fa0、fa1、fa2是3个不同的FttpAdapter,而不是同一个FttpAdapter,否则后面的getFttpReader指定的读取范围会覆盖前面的范围,导致意外错乱。由于是对同一个远程文件并行读写,因此fa0、fa1、fa2的fttppath相同,如果对多个远程文件并行读写,则fttppath不同。
FttpMulWriteReadDemo演示了并行对三个远程文件进行写,然后再并行进行读,在轮循状态时,如果读取到结果后,将rs[i]设置为null表示不再重复检查。
运行步骤如下:
1)启动ParkServerDemo:
- java -cp fourinone.jar; ParkServerDemo
2)在192.168.0.1机器上启动FttpServer:
- java -cp fourinone.jar; FttpServer 192.168.0.1
3)在本地运行FttpMulWriteReadDemo,远程并行读写192.168.0.1上的/home/log/1.log文件的内容:
- java -cp fourinone.jar; FttpMulWriteReadDemo
完整demo源码如下:
- // FttpMulWriteReadDemo
- import com.fourinone.FttpAdapter;
- import com.fourinone.FttpException;
- import com.fourinone.Result;
- public class FttpMulWriteReadDemo
- {
- public static void fttpMulWrite(){
- try{
- String fttppath = "fttp://192.168.0.1/home/log/1.log";
- Result<Integer>[] rs = new Result[3];
- FttpAdapter fa0 = new FttpAdapter(fttppath);
- rs[0]=fa0.getFttpWriter(0,5).tryWrite("hello".getBytes());
- FttpAdapter fa1 = new FttpAdapter(fttppath);
- rs[1]=fa1.getFttpWriter(5,5).tryWrite("world".getBytes());
- FttpAdapter fa2 = new FttpAdapter(fttppath);
- rs[2]=fa2.getFttpWriter(10,5).tryWrite("fttp!".getBytes());
- int n=0;
- while(n<3){
- for(int i=0;i<rs.length;i++){
- if(rs[i]!=null&&rs[i].getStatus()!=Result.NOTREADY){
- System.out.println(rs[i].getResult());
- rs[i]=null;
- n++;
- }
- }
- }
- fa0.close();
- fa1.close();
- fa2.close();
- }catch(FttpException fe){
- fe.printStackTrace();
- }
- }
- public static void fttpMulRead(){
- try{
- Result<byte[]>[] rs = new Result[3];
- String fttppath = "fttp://192.168.0.1/home/log/1.log";
- FttpAdapter fa0 = new FttpAdapter(fttppath);
- rs[0]=fa0.getFttpReader(0,5).tryReadAll();
- FttpAdapter fa1 = new FttpAdapter(fttppath);
- rs[1]=fa1.getFttpReader(5,5).tryReadAll();
- FttpAdapter fa2 = new FttpAdapter(fttppath);
- rs[2]=fa2.getFttpReader(10,5).tryReadAll();
- int n=0;
- while(n<3){
- for(int i=0;i<rs.length;i++){
- if(rs[i]!=null&&rs[i].getStatus()!=Result.NOTREADY){
- System.out.println(new String(rs[i].getResult()));
- rs[i]=null;
- n++;
- }
- }
- }
- fa0.close();
- fa1.close();
- fa2.close();
- }catch(FttpException fe){
- fe.printStackTrace();
- }
- }
- public static void main(String[] args){
- fttpMulWrite();
- fttpMulRead();
- }
- }