Java DDS数据读取器缓存中断,不再可访问

Java DDS数据读取器缓存中断,不再可访问,java,distributed-caching,distributed-system,data-distribution-service,Java,Distributed Caching,Distributed System,Data Distribution Service,使用i386上的dds库操作,尝试重复提取样本。我明确地“读取”ing“不”获取”ing“样本”,因此它们永远不会过期或被删除 启动两个黑板应用程序,(1)和(2) 在两个应用程序中执行读取。这将返回“缓存为空” 从(1)写入,传感器id:1,事件id:1,值:1 读取(1),确认数值 读取(2),确认数值 从(2)写入,传感器id:1,事件id:1,值:2 从(2)读取,“缓存为空” 从(1)读取,“缓存为空” 好像是我“弄坏”了它!我相信样本的生命周期应该是有限的(或者我已经理解了……但

使用i386上的dds库操作,尝试重复提取样本。我明确地“读取”ing“不”
获取”
ing“样本”,因此它们永远不会过期或被删除

  • 启动两个黑板应用程序,(1)和(2)
  • 在两个应用程序中执行读取。这将返回“缓存为空”
  • 从(1)写入,传感器id:1,事件id:1,值:1
  • 读取(1),确认数值
  • 读取(2),确认数值
  • 从(2)写入,传感器id:1,事件id:1,值:2
  • 从(2)读取,“缓存为空”
  • 从(1)读取,“缓存为空”
好像是我“弄坏”了它!我相信样本的生命周期应该是有限的(或者我已经理解了……但无法确认!)——但我不能明确地设置它
topicQos.lifespan.duration
的类型为
duration\u t
,但我无法将其设置为“新建
duration\t(duration\u t.duration\u INFINITY\u SEC,duration\u t.duration\u INFINITY\u NSEC)
,因为它已经完成了

public class Main {

  private static final String EVENT_TOPIC_NAME = "EVENTS";
  private static BufferedReader in = null;
  private static PrintStream out = null;
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) throws IOException {
    in = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintStream(new BufferedOutputStream(System.out));

    DomainParticipantFactory factory = DomainParticipantFactory.TheParticipantFactory;
    DomainParticipant participant = factory.create_participant(100,
            DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
            null,
            StatusKind.STATUS_MASK_NONE);

    EventTypeSupport.register_type(participant, EventTypeSupport.get_type_name());

    TopicQos topicQos = new TopicQos();
    topicQos.durability.direct_communication = true;
    topicQos.durability.kind = DurabilityQosPolicyKind.TRANSIENT_DURABILITY_QOS;
    topicQos.reliability.kind = ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS;
    topicQos.resource_limits.max_instances = 100;
    topicQos.resource_limits.max_samples = 100;
    topicQos.resource_limits.max_samples_per_instance = 1;
    topicQos.ownership.kind = OwnershipQosPolicyKind.SHARED_OWNERSHIP_QOS;
    topicQos.history.kind = HistoryQosPolicyKind.KEEP_LAST_HISTORY_QOS;
    topicQos.history.depth = 1;
    topicQos.history.refilter = RefilterQosPolicyKind.ALL_REFILTER_QOS;
    // Since this is on the same computer, and being typed by a human, we can exepct source timestamps to be useful in ordering
    topicQos.destination_order.kind = DestinationOrderQosPolicyKind.BY_SOURCE_TIMESTAMP_DESTINATIONORDER_QOS;

    Topic topic =
            participant.create_topic(EVENT_TOPIC_NAME,
            EventTypeSupport.get_type_name(),
            topicQos,
            new EventTopicListener(),
            StatusKind.STATUS_MASK_ALL);
    exitIfNullBecause(topic, "Could not create topic");

    Subscriber subscriber = participant.create_subscriber(DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
            null,
            StatusKind.STATUS_MASK_NONE);
    exitIfNullBecause(subscriber, "Could not create subscriber");

    DataReader reader = subscriber.create_datareader(participant.lookup_topicdescription(EVENT_TOPIC_NAME),
            subscriber.DATAREADER_QOS_USE_TOPIC_QOS,
            null,
            StatusKind.STATUS_MASK_NONE);
    exitIfNullBecause(reader, "Could not create reader");
    EventDataReader eventReader = (EventDataReader) reader;

    Publisher publisher = participant.create_publisher(DomainParticipant.PUBLISHER_QOS_DEFAULT,
            null,
            StatusKind.STATUS_MASK_NONE);
    exitIfNullBecause(publisher, "Could not create publisher");

    DataWriter writer = publisher.create_datawriter(topic,
            publisher.DATAWRITER_QOS_USE_TOPIC_QOS,
            null,
            StatusKind.STATUS_MASK_NONE);
    exitIfNullBecause(writer, "Could not create writer");
    EventDataWriter eventWriter = (EventDataWriter)writer;

    boolean loop = true;
    byte inputBuffer[] = new byte[1024];
    String command;
    while(loop){
      print("Enter action [read|write|exit]: ");

      command = in.readLine();
      if(command.startsWith("r")){
        dumpCache(eventReader);
      } else if(command.startsWith("w")) {
        writeCache(eventWriter);
      } else if(command.startsWith("e")){
        println("exiting...");
        System.exit(0);
      } else {
        println("Unknown: '" + command + "'");
      }
    }

    System.exit(0);
  }

  private static void print(String output){
    out.print(output);
    out.flush();
  }
  private static void println(String output){
    out.println(output);
    out.flush();
  }

  private static void exitIfNullBecause(Object thing, String string) {
    if (thing == null) {
      println("ERROR: " + string);
      System.exit(1);
    }
  }
  private static void dumpCache(EventDataReader eventReader) {
    // Something interesting here: I can creat it with a collection as a paramter. TODO: Investigate!
    EventSeq eventSeq = new EventSeq();
    SampleInfoSeq infoSeq = new SampleInfoSeq();

    Event event = null;
    SampleInfo info = null;

    try{
      eventReader.read(eventSeq, infoSeq, 100, SampleStateKind.ANY_SAMPLE_STATE, ViewStateKind.ANY_VIEW_STATE, InstanceStateKind.ANY_INSTANCE_STATE);
    } catch (Exception e){
      println("Cache is empty");
      return;
    }

    Iterator<SampleInfo> infoIter = infoSeq.iterator();
    out.printf("| Sensor ID | Event ID | Value |\n");
    for(int i=0; i<infoSeq.size(); i++){
      event = (Event)eventSeq.get(i);
      out.printf("| %9d | %8d | %5d |\n", event.sensor_id, event.event_id, event.value);
    }
    out.flush();
  }

  private static void writeCache(EventDataWriter eventWriter) throws IOException {
    Event event = new Event();

    print("Sensor ID: ");
    String sensor_id_str = in.readLine();
    print("Event ID: ");
    String event_id_str = in.readLine();
    print("Value: ");
    String value_str = in.readLine();

    Event sample = new Event();
    sample.sensor_id = Integer.parseInt(sensor_id_str);
    sample.event_id = Integer.parseInt(event_id_str);
    sample.value = Integer.parseInt(value_str);

    InstanceHandle_t handle = eventWriter.register_instance(sample);
//    eventWriter.write(sample, handle);
    eventWriter.write_w_timestamp(sample, handle, Time_t.now());


    out.printf("SensorID: %s, EventID: %s, Value: %s\n",sensor_id_str,event_id_str,value_str); out.flush();
  }
}
公共类主{
私有静态最终字符串事件\u主题\u NAME=“事件”;
私有静态BufferedReader in=null;
私有静态打印流输出=null;
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发IOException{
in=新的BufferedReader(新的InputStreamReader(System.in));
out=新的打印流(新的缓冲输出流(System.out));
DomainParticipantFactory=DomainParticipantFactory.TheParticipantFactory;
域参与者=工厂。创建参与者(100,
DomainParticipantFactory.PARTICIPANT\u QOS\u默认值,
无效的
StatusKind.STATUS\u MASK\u NONE);
EventTypeSupport.register_type(参与者,EventTypeSupport.get_type_name());
TopicQos TopicQos=新的TopicQos();
topicQos.Dutability.direct_通信=真;
topicQos.耐久性.kind=耐久性Qospolicykind.TRANSIENT\u耐久性\u QOS;
topicQos.reliability.kind=ReliabilityQosPolicyKind.RELIABLE\u reliability\u QOS;
topicQos.resource_limits.max_instances=100;
topicQos.resource_limits.max_samples=100;
topicQos.resource_limits.max_samples_per_instance=1;
topicQos.ownership.kind=OwnershipQosPolicyKind.SHARED\u ownership\u QOS;
topicQos.history.kind=HistoryQosPolicyKind.KEEP_LAST_history_QOS;
topicQos.history.depth=1;
topicQos.history.refilter=RefilterQosPolicyKind.ALL\u refilter\u QOS;
//由于这是在同一台计算机上,并且是由人键入的,因此我们可以将源时间戳exepct用于排序
topicQos.destination\u order.kind=DestinationOrderQosPolicyKind.BY\u SOURCE\u TIMESTAMP\u DESTINATIONORDER\u QOS;
话题=
参与者。创建主题(事件主题名称,
EventTypeSupport.get\u type\u name(),
主题QoS,
新建EventTopicListener(),
StatusKind.STATUS\u MASK\u ALL);
exitIfNullBecause(主题“无法创建主题”);
订户订户=参与者。创建订户(DomainParticipant.Subscriber\u QOS\u默认值,
无效的
StatusKind.STATUS\u MASK\u NONE);
exitIfNullBecause(订阅者,“无法创建订阅者”);
DataReader=subscriber.create\u DataReader(参与者.查找\u主题描述(事件\u主题\u名称),
subscriber.DATAREADER\u QOS\u使用\u主题\u QOS,
无效的
StatusKind.STATUS\u MASK\u NONE);
exitIfNullBecause(读卡器,“无法创建读卡器”);
EventDataReader eventReader=(EventDataReader)读取器;
Publisher Publisher=participant.create\u Publisher(DomainParticipant.Publisher\u QOS\u默认值,
无效的
StatusKind.STATUS\u MASK\u NONE);
exitIfNullBecause(发布服务器,“无法创建发布服务器”);
DataWriter=publisher.create_DataWriter(主题,
publisher.DATAWRITER\u QOS\u使用\u主题\u QOS,
无效的
StatusKind.STATUS\u MASK\u NONE);
exitIfNullBecause(writer,“无法创建writer”);
EventDataWriter eventWriter=(EventDataWriter)编写器;
布尔循环=真;
字节输入缓冲区[]=新字节[1024];
字符串命令;
while(循环){
打印(“输入操作[读取|写入|退出]:”;
command=in.readLine();
if(command.startsWith(“r”)){
dumpCache(eventReader);
}else if(command.startsWith(“w”)){
writeCache(eventWriter);
}else if(command.startsWith(“e”)){
println(“退出…”);
系统出口(0);
}否则{
println(“未知:“+”命令+“””);
}
}
系统出口(0);
}
私有静态无效打印(字符串输出){
输出(输出);
out.flush();
}
私有静态void println(字符串输出){
out.println(输出);
out.flush();
}
私有静态void exitIfNullBecause(对象对象、字符串){
if(thing==null){
println(“错误:+字符串);
系统出口(1);
}
}
私有静态无效转储缓存(EventDataReader eventReader){
//这里有一件有趣的事情:我可以用一个集合作为参数来创建它!
EventSeq EventSeq=新的EventSeq();
SampleInfoSeq infoSeq=新的SampleInfoSeq();
事件=null;
SampleInfo=null;
试一试{
eventReader.read(eventSeq,infoSeq,100,SampleStateKind.ANY_SAMPLE_STATE,ViewStateKind.ANY_VIEW_STATE,InstanceStateKind.ANY_INSTANCE_STATE);
}捕获(例外e){
println(“缓存为空”);
返回;
}
迭代器infoIter=infoSeq.Iterator();
out.printf(“|传感器ID |事件ID |值|\n”);

对于(int i=0;i而言,问题似乎与
寿命
无关

我不确定您使用的是哪种DDS实现,但根据DDS规范,您正在
dumpCache
方法中执行零拷贝操作。如果您忘记归还贷款,则您使用的实现的行为可能与此类似

try{
  eventReader.return_loan(eventSeq, infoSeq);
} catch (Exception e){
  println("Error returning loan");
  return;
}