Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将已发布的DDS内容附加到订阅服务器端的现有文件中?_Java_Data Distribution Service - Fatal编程技术网

Java 如何将已发布的DDS内容附加到订阅服务器端的现有文件中?

Java 如何将已发布的DDS内容附加到订阅服务器端的现有文件中?,java,data-distribution-service,Java,Data Distribution Service,我已经创建了使用java实现的普通发布者和订阅者,它的作用是按1MB大小读取内容,总大小为5MB,并每1MB向订阅者发布一次。数据已成功发布。。现在我面临将内容附加到现有文件的问题。。最后,我只能找到文件中最后1MB的数据。因此,请告诉我如何解决此问题问题我还附上了发布者和订阅者的源代码 Publisher: public class MessageDataPublisher { static StringBuffer fileContent; static RandomAcc

我已经创建了使用java实现的普通发布者和订阅者,它的作用是按1MB大小读取内容,总大小为5MB,并每1MB向订阅者发布一次。数据已成功发布。。现在我面临将内容附加到现有文件的问题。。最后,我只能找到文件中最后1MB的数据。因此,请告诉我如何解决此问题问题我还附上了发布者和订阅者的源代码

Publisher:

public class MessageDataPublisher {
    static StringBuffer fileContent;
    static RandomAccessFile randomAccessFile ;

    public static void main(String[] args) throws IOException {
        MessageDataPublisher msgObj=new MessageDataPublisher();

        String fileToWrite="test.txt";
        msgObj.towriteDDS(fileToWrite);
    }


    public void towriteDDS(String fileName) throws IOException{

        DDSEntityManager mgr=new DDSEntityManager();
        String partitionName="PARTICIPANT";



        // create Domain Participant
        mgr.createParticipant(partitionName);

        // create Type
        BinaryFileTypeSupport binary=new BinaryFileTypeSupport();
        mgr.registerType(binary);


        // create Topic
        mgr.createTopic("Serials");

        // create Publisher
        mgr.createPublisher();

        // create DataWriter
        mgr.createWriter();

        // Publish Events

        DataWriter dwriter = mgr.getWriter();
        BinaryFileDataWriter binaryWriter=BinaryFileDataWriterHelper.narrow(dwriter);


        int bufferSize=1024*1024;


        File readfile=new File(fileName);
        FileInputStream is = new FileInputStream(readfile);
        byte[] totalbytes = new byte[is.available()];
        is.read(totalbytes);
        byte[] readbyte = new byte[bufferSize];
        BinaryFile binaryInstance;

        int k=0;
        for(int i=0;i<totalbytes.length;i++){
            readbyte[k]=totalbytes[i];
            k++;
            if(k>(bufferSize-1)){
                binaryInstance=new BinaryFile();
                binaryInstance.name="sendpublisher.txt";
                binaryInstance.contents=readbyte;
                int status = binaryWriter.write(binaryInstance, HANDLE_NIL.value);
                ErrorHandler.checkStatus(status, "MsgDataWriter.write");

                ErrorHandler.checkStatus(status, "MsgDataWriter.write");

                k=0;
                }

        }
        if(k < (bufferSize-1)){
            byte[] remaingbyte = new byte[k];                   
            for(int j=0;j<(k-1);j++){
                remaingbyte[j]=readbyte[j];
            }
            binaryInstance=new BinaryFile();
            binaryInstance.name="sendpublisher.txt";
            binaryInstance.contents=remaingbyte;
            int status = binaryWriter.write(binaryInstance, HANDLE_NIL.value);
            ErrorHandler.checkStatus(status, "MsgDataWriter.write");

        }       
        is.close();


        try {
            Thread.sleep(4000);

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

        // clean up
        mgr.getPublisher().delete_datawriter(binaryWriter);
        mgr.deletePublisher();
        mgr.deleteTopic();
        mgr.deleteParticipant();

    }




}


Subscriber:


public class MessageDataSubscriber {
    static RandomAccessFile randomAccessFile ;
    public static void main(String[] args) throws IOException {
        DDSEntityManager mgr = new DDSEntityManager();
        String partitionName = "PARTICIPANT";

        // create Domain Participant
        mgr.createParticipant(partitionName);

        // create Type
        BinaryFileTypeSupport msgTS = new BinaryFileTypeSupport();
        mgr.registerType(msgTS);

        // create Topic
        mgr.createTopic("Serials");

        // create Subscriber
        mgr.createSubscriber();

        // create DataReader
        mgr.createReader();

        // Read Events
        DataReader dreader = mgr.getReader();
        BinaryFileDataReader binaryReader=BinaryFileDataReaderHelper.narrow(dreader);
        BinaryFileSeqHolder binaryseq=new BinaryFileSeqHolder();
        SampleInfoSeqHolder infoSeq = new SampleInfoSeqHolder();
        boolean terminate = false;
        int count = 0;

        while (!terminate && count < 1500) {
             // To run undefinitely
            binaryReader.take(binaryseq, infoSeq, 10,
                    ANY_SAMPLE_STATE.value, ANY_VIEW_STATE.value,ANY_INSTANCE_STATE.value);
                for (int i = 0; i < binaryseq.value.length; i++) {
                    toWrtieXML(binaryseq.value[i].contents);
                    terminate = true;
            }

            try
            {
                Thread.sleep(200);
            }
            catch(InterruptedException ie)
            {
            }
            ++count;

        }
            binaryReader.return_loan(binaryseq,infoSeq);

        // clean up

        mgr.getSubscriber().delete_datareader(binaryReader);
        mgr.deleteSubscriber();
        mgr.deleteTopic();
        mgr.deleteParticipant();

    }

    private static void toWrtieXML(byte[] bytes) throws IOException {
        // TODO Auto-generated method stub
        File Writefile=new File("samplesubscriber.txt");
        if(!Writefile.exists()){
            randomAccessFile = new RandomAccessFile(Writefile, "rw");
            randomAccessFile.write(bytes, 0, bytes.length);
            randomAccessFile.close();
            }
            else{
                randomAccessFile = new RandomAccessFile(Writefile, "rw");
                long i=Writefile.length();
                randomAccessFile.seek(i);
                randomAccessFile.write(bytes, 0, bytes.length);
                randomAccessFile.close();
            }


    }
}
发布者:
公共类MessageDataPublisher{
静态字符串缓冲文件内容;
静态随机存取文件;
公共静态void main(字符串[]args)引发IOException{
MessageDataPublisher msgObj=新建MessageDataPublisher();
字符串fileToWrite=“test.txt”;
msgObj.towriteDDS(fileToWrite);
}
public void towriteDDS(字符串文件名)引发IOException{
DDSEntityManager mgr=新的DDSEntityManager();
String partitionName=“参与者”;
//创建域参与者
创建参与者经理(分区名称);
//创建类型
BinaryFileTypeSupport binary=新的BinaryFileTypeSupport();
经理注册表类型(二进制);
//创建主题
createTopic经理(“连载”);
//创建发布者
经理createPublisher();
//创建数据编写器
经理createWriter();
//发布事件
DataWriter dwriter=mgr.getWriter();
BinaryFileDataWriter binaryWriter=BinaryFileDataWriterHelper.窄带(dwriter);
int bufferSize=1024*1024;
File readfile=新文件(文件名);
FileInputStream is=新的FileInputStream(readfile);
byte[]totalbytes=新字节[is.available()];
is.read(总字节数);
字节[]读字节=新字节[bufferSize];
二进制文件二进制实例;
int k=0;
对于(int i=0;i(bufferSize-1)){
binaryInstance=新的二进制文件();
binaryInstance.name=“sendpublisher.txt”;
binaryInstance.contents=readbyte;
int status=binaryWriter.write(binaryInstance,HANDLE\u NIL.value);
ErrorHandler.checkStatus(状态为“MsgDataWriter.write”);
ErrorHandler.checkStatus(状态为“MsgDataWriter.write”);
k=0;
}
}
如果(k<(缓冲区大小-1)){
字节[]剩余字节=新字节[k];

对于(int j=0;j来说,很难对您的问题给出结论性的答案,因为您的问题可能是由几个不同的原因造成的。此外,一旦确定了问题的原因,您可能会有多种选择来缓解它

首先要看的是读卡器端
在循环中,每次拍摄之间有200毫秒的暂停。根据您在DataReader上的QoS设置,您可能会遇到这样的情况,即当您的应用程序休眠200毫秒时,您的样本在DataReader中被覆盖。如果您是通过千兆以太网进行此操作,则典型的DDS产品将能够在睡眠时间内执行这5块1兆字节的操作,这意味着默认的一位缓冲区将在睡眠期间被覆盖4次

如果对
BinaryFileDataReader
使用默认的历史QoS设置,则可能出现这种情况,这意味着
history.kind=KEEP_LAST
history.depth=1
。如果将后者增加到更大的值,例如增加到20,则会产生一个队列,该队列能够容纳20个文件块睡懒觉,现在应该足够了


如果这不能解决您的问题,则可以探讨其他可能的原因。

很难对您的问题给出结论性的答案,因为您的问题可能是多个不同原因造成的。此外,一旦确定了问题的原因,您可能会有多种选择来缓解问题

首先要看的是读卡器端在循环中,每次拍摄之间有200毫秒的暂停。根据您在DataReader上的QoS设置,您可能会遇到这样的情况,即当您的应用程序休眠200毫秒时,您的样本在DataReader中被覆盖。如果您是通过千兆以太网进行此操作,则典型的DDS产品将能够在睡眠时间内执行这5块1兆字节的操作,这意味着默认的一位缓冲区将在睡眠期间被覆盖4次

如果对
BinaryFileDataReader
使用默认的历史QoS设置,则可能出现这种情况,这意味着
history.kind=KEEP_LAST
history.depth=1
。如果将后者增加到更大的值,例如增加到20,则会产生一个队列,该队列能够容纳20个文件块睡懒觉,现在应该足够了


如果这不能解决您的问题,可以探讨其他可能的原因。

您将源代码附加到了哪里?感谢您的回复Brain先生。我已经为发布者和订阅者添加了源代码。如果您知道此问题,请与我分享。您将源代码附加到了哪里?感谢您的回复Brain先生。我已经添加了s发布者和订阅者的源代码。如果您知道此问题,请与我共享。