import java.util.Objects;
public class HelloWorldPublisher extends Application implements AutoCloseable {
private DomainParticipant participant = null;
private void runApplication() {
participant = Objects.requireNonNull(
DomainParticipantFactory.get_instance().create_participant(
getDomainId(),
DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null,
StatusKind.STATUS_MASK_NONE));
Publisher publisher = Objects.requireNonNull(
participant.create_publisher(
DomainParticipant.PUBLISHER_QOS_DEFAULT,
null,
StatusKind.STATUS_MASK_NONE));
HelloWorldTypeSupport.register_type(participant, typeName);
Topic topic = Objects.requireNonNull(
participant.create_topic(
"Example HelloWorld",
typeName,
DomainParticipant.TOPIC_QOS_DEFAULT,
null,
StatusKind.STATUS_MASK_NONE));
HelloWorldDataWriter writer = (HelloWorldDataWriter) Objects.requireNonNull(
publisher.create_datawriter(
topic,
Publisher.DATAWRITER_QOS_DEFAULT,
null,
StatusKind.STATUS_MASK_NONE));
HelloWorld data = new HelloWorld();
for (int samplesWritten = 0; !isShutdownRequested()
&& samplesWritten < getMaxSampleCount(); samplesWritten++) {
System.out.println("Writing HelloWorld, count " + samplesWritten);
writer.write(data, InstanceHandle_t.HANDLE_NIL);
try {
final long sendPeriodMillis = 1000;
Thread.sleep(sendPeriodMillis);
} catch (InterruptedException ix) {
System.err.println("INTERRUPTED");
break;
}
}
}
@Override
public void close() {
if (participant != null) {
participant.delete_contained_entities();
DomainParticipantFactory.get_instance()
.delete_participant(participant);
}
}
public static void main(String[] args) {
try (HelloWorldPublisher publisherApplication = new HelloWorldPublisher()) {
publisherApplication.parseArguments(args);
publisherApplication.addShutdownHook();
publisherApplication.runApplication();
}
DomainParticipantFactory.finalize_instance();
}
}