6.8 并行读写远程文件

FttpAdapter是通过FttpReadAdapter的tryReadAll方法进行并行读:

  1. FttpAdapter fa = new FttpAdapter("fttp://192.168.0.1/home/log/1.log");
  2. Result<byte[]> rs = fa.getFttpReader().tryReadAll();

调用tryReadAll会立即返回一个Result<byte[]>,但是不能马上获取到结果值,需要轮循检查它的状态是否就绪。

rs.getStatus()有三种状态:

❏ Result.NOTREADY未就绪

❏ Result.READY就绪

❏ Result.EXCEPTION异常

轮循直到状态准备就绪:

  1. while(rs.getStatus()==Result.NOTREADY);

状态就绪就可以通过getResult()获取到读取结果:

  1. byte[] bts = rs.getResult();

可以对一个远程文件的不同部分同时并行读写,也可以对多个远程文件同时并行读写,比如:

  1. String fttppath = "fttp:// 192.168.0.1/home/log/1.log";
  2. FttpAdapter fa0 = new FttpAdapter(fttppath);
  3. FttpAdapter fa1 = new FttpAdapter(fttppath);
  4. FttpAdapter fa2 = new FttpAdapter(fttppath);
  5. Result<byte[]> rs0 = fa0.getFttpReader(0,5).tryReadAll();
  6. Result<byte[]> rs1 = fa1.getFttpReader(5,5).tryReadAll();
  7. Result<byte[]> rs2 = fa2.getFttpReader(10,5).tryReadAll();

上面是3个同时并行的读取,分别从一个文件的0、5、10位置向后读取5个字节。

如果是并行写,则是:

  1. Result<Integer>[] rs0 = fa0.getFttpWriter(0,5).tryWrite("hello".getBytes());
  2. Result<Integer>[] rs1 = fa1.getFttpWriter(5,5).tryWrite("world".getBytes());
  3. Result<Integer>[] rs2 = fa2.getFttpWriter(10,5).tryWrite("fttp!".getBytes());

6.8 并行读写远程文件 - 图1注意

上面代码中的fa0、fa1、fa2是3个不同的FttpAdapter,而不是同一个FttpAdapter,否则后面的getFttpReader指定的读取范围会覆盖前面的范围,导致意外错乱。由于是对同一个远程文件并行读写,因此fa0、fa1、fa2的fttppath相同,如果对多个远程文件并行读写,则fttppath不同。

FttpMulWriteReadDemo演示了并行对三个远程文件进行写,然后再并行进行读,在轮循状态时,如果读取到结果后,将rs[i]设置为null表示不再重复检查。

运行步骤如下:

1)启动ParkServerDemo:

  1. java -cp fourinone.jar; ParkServerDemo

2)在192.168.0.1机器上启动FttpServer:

  1. java -cp fourinone.jar; FttpServer 192.168.0.1

3)在本地运行FttpMulWriteReadDemo,远程并行读写192.168.0.1上的/home/log/1.log文件的内容:

  1. java -cp fourinone.jar; FttpMulWriteReadDemo

完整demo源码如下:

  1. // FttpMulWriteReadDemo
  2. import com.fourinone.FttpAdapter;
  3. import com.fourinone.FttpException;
  4. import com.fourinone.Result;
  5.  
  6. public class FttpMulWriteReadDemo
  7. {
  8. public static void fttpMulWrite(){
  9. try{
  10. String fttppath = "fttp://192.168.0.1/home/log/1.log";
  11. Result<Integer>[] rs = new Result[3];
  12. FttpAdapter fa0 = new FttpAdapter(fttppath);
  13. rs[0]=fa0.getFttpWriter(0,5).tryWrite("hello".getBytes());
  14. FttpAdapter fa1 = new FttpAdapter(fttppath);
  15. rs[1]=fa1.getFttpWriter(5,5).tryWrite("world".getBytes());
  16. FttpAdapter fa2 = new FttpAdapter(fttppath);
  17. rs[2]=fa2.getFttpWriter(10,5).tryWrite("fttp!".getBytes());
  18.  
  19. int n=0;
  20. while(n<3){
  21. for(int i=0;i<rs.length;i++){
  22. if(rs[i]!=null&&rs[i].getStatus()!=Result.NOTREADY){
  23. System.out.println(rs[i].getResult());
  24. rs[i]=null;
  25. n++;
  26. }
  27. }
  28. }
  29.  
  30. fa0.close();
  31. fa1.close();
  32. fa2.close();
  33. }catch(FttpException fe){
  34. fe.printStackTrace();
  35. }
  36. }
  37.  
  38. public static void fttpMulRead(){
  39. try{
  40. Result<byte[]>[] rs = new Result[3];
  41. String fttppath = "fttp://192.168.0.1/home/log/1.log";
  42.  
  43. FttpAdapter fa0 = new FttpAdapter(fttppath);
  44. rs[0]=fa0.getFttpReader(0,5).tryReadAll();
  45. FttpAdapter fa1 = new FttpAdapter(fttppath);
  46. rs[1]=fa1.getFttpReader(5,5).tryReadAll();
  47. FttpAdapter fa2 = new FttpAdapter(fttppath);
  48. rs[2]=fa2.getFttpReader(10,5).tryReadAll();
  49.  
  50. int n=0;
  51. while(n<3){
  52. for(int i=0;i<rs.length;i++){
  53. if(rs[i]!=null&&rs[i].getStatus()!=Result.NOTREADY){
  54. System.out.println(new String(rs[i].getResult()));
  55. rs[i]=null;
  56. n++;
  57. }
  58. }
  59. }
  60.  
  61. fa0.close();
  62. fa1.close();
  63. fa2.close();
  64. }catch(FttpException fe){
  65. fe.printStackTrace();
  66. }
  67. }
  68.  
  69. public static void main(String[] args){
  70. fttpMulWrite();
  71. fttpMulRead();
  72. }
  73. }