0

まず、相互にサポートする 2 つのプロセスが同時に実行されています。1 つのプロセスは、タイムスタンプで区切られたデータのスナップショットを含む単純なフラットファイルを読み取ります。このアプリケーションは、このファイルを (ファイル ロックなしで) 開き、スナップショットを読み取り、別のファイルに配置topology.netvizします (ファイル ロックあり)。2 番目のアプリケーションtopology.netzivは、データを (ファイル ロックを使用して) 読み取り、一時ファイルに転送して、他のプロセス間でロックを保持するプログラミングの待機時間を短縮します。

私の問題は、述べたように単純です。2 番目のプロセスでデータを一時ファイルに転送すると、奇妙な文字/破損したデータが転送されます。何が問題なのかを感じていただけるように、以下にいくつかのコードを提供しました。

プロセス 1:

try {
    // Determine if File Exists
    topologyFile = new File(Settings.NODE_TOPOLOGY_PATH);
    if (!topologyFile.exists())
        topologyFile.createNewFile();

    // FileChannel Gives the Ability to Create a File Lock
    FileChannel channel =
        new RandomAccessFile(topologyFile, "rwd").getChannel();

    // Use the FileChannel to Create a Lock on the 'distance.dat' (Blocking Method)
    FileLock lock = channel.lock();

    // Delete Files Contents
    channel.truncate(0);

    // Convert 'data' into ByteBuffer
    ByteBuffer buffer = ByteBuffer.wrap(data);

    // Write 'buffer' to 'channel'
    channel.write(buffer, 0);

    // Release Lock
    lock.release();

    // Close Channel
    channel.close();
}

catch(IOException error)
{
    System.out.println("Topology Thread: FileChannel; I/O Error Occured");
}

catch(NonWritableChannelException error)
{
    System.
        out.println("Topology Thread: FileChannel; File is not Writeable");
}

プロセス 2:

try {
    // Determine if File Exists
    topologyFileTemp = new File("tmp/topology.dat");
    if (topologyFileTemp.exists())
        topologyFileTemp.delete();  // Should Never Occur Unless Program Crashes

    // Recreate 'topologyFileTemp'
    topologyFileTemp = new File("tmp/topology.dat");
    topologyFileTemp.createNewFile();

    // Determine if File Exists
    topologyFile = new File("topology.netviz");
    if (!topologyFile.exists())
        topologyFile.createNewFile();   // Should Never Occur

    // Initialize Data Container from 'topology.netviz' in the Form of Bytes
    ByteBuffer topologyData =
        ByteBuffer.allocate((int)topologyFile.length());

    // FileChannel Gives the Ability to Create a File Lock for 'topology.netviz'
    FileChannel rChannel =
        new RandomAccessFile(topologyFile, "rwd").getChannel();

    // Use the FileChannel to Create a Lock on the 'distance.dat' (Blocking Method)
    FileLock lock = rChannel.lock();

    // Grab Data from 'topology.netviz'
    rChannel.read(topologyData);

    // Release Lock
    lock.release();

    // Close Channel
    rChannel.close();

    // FileChannel Gives the Ability to Create a File Lock for 'tmp/topology.dat'
    FileChannel wChannel =
        new RandomAccessFile(topologyFileTemp, "rw").getChannel();

    // Reset Buffers Position
    topologyData.position(0);

    // Write 'topologyData' to 'tmp/topology.dat'
    wChannel.write(topologyData);

    // Close the file
    wChannel.close();
}

catch(IOException error)
{
    System.out.println("Topology Thread: FileChannel; I/O Error Occured");
}

catch(NonWritableChannelException error)
{
    System.out.
        println("Topology Thread: FileChannel; File is not Writeable");
}
4

1 に答える 1

0

潜在的な問題の 1 つは、プロセス #1 がチャネルを閉じる前にロックを解放していることです。チャネルを閉じると未処理の書き込みがフラッシュされるため、ロックが解除された後にデータが書き込まれている可能性があり、破損の原因となる可能性があります。

別の可能性としては、バイトが書き込まれる前に破損している可能性があります...または、ライターとリーダーで使用されるフォーマットが一致していない可能性があります。

于 2011-06-30T03:41:11.937 に答える