Skip to content
bdferris edited this page Jul 26, 2012 · 11 revisions

The onebusaway-gtfs-realtime-exporter module provides a number of methods to assist you in producing and sharing a GTFS-realtime real-time transit data feed. Specific features include:

  • Support for running a simple, embedded webserver to quickly share your GTFS-realtime data.
  • Support for writing GTFS-realtime data to a file.
  • Convenience methods for constructing a GTFS-realtime feed.

Quick Start

The onebusaway-gtfs-realtime-exporter module is provided as a Maven module in the OneBusAway Maven Repository. To include it as a dependency to your Maven project, add the following to the <dependencies/> section of your project's pom.xml file:

<dependency>
  <groupId>org.onebusaway</groupId>
  <artifactId>onebusaway-gtfs-realtime-exporter</artifactId>
  <version>1.1.0</version>
</dependency>     

How do you use the library? The first step is to write a class that has a dependency on the GtfsRealtimeMutableProvider interface.

@Inject
public void setProvider(GtfsRealtimeMutableProvider provider) {
    this.provider = provider;
}

The provider interface defines a contract for providing GTFS-realtime data through three methods:

public interface GtfsRealtimeMutableProvider {
  void setAlerts(FeedMessage message);
  void setTripUpdates(FeedMessage message);
  void setVehiclePositions(FeedMessage message);
}

The three methods correspond to the three GTFS-realtime feed types. The logic to build your GTFS-realtime feed is up to you to provide and will depend on the nature of your underlying real-time system and data-sources.

Once you've built your gtfs-realtime provider class, it's time to wire it up so that we can easily share the data on the web, or optionally write it to a file to share in a more custom way. We make use of the Google Guice dependency-injection framework to wire everything up.

Set<Module> modules = new HashSet<Module>();
GtfsRealtimeExporterModule.addModuleAndDependencies(modules);
Injector injector = Guice.createInjector(modules);

Once we have our Guice injector, we can now configure our application to share GTFS-realtime data via an embedded web-server, a file, or both.

  AlertsServlet servlet = injector.getInstance(AlertsServlet.class);
  servlet.setUrl(new URL("http://localhost:8080/alerts");

  AlertsFileWriter writer = injector.getInstance(AlertsFileWriter.class);
  writer.setPath("/some/path/for/alerts");

You can configure all three feed types in a similar manner:

Once you've got everything wired up and configured, you can start the application:

LifecycleService lifecycleService = injector.getInstance(LifecycleService.class);
lifecycleService.start();

The GTFs-realtime exporter will now continuously poll your GTFS-realtime provider every few seconds and share the resulting GTFS-realtime data via an embedded web-server or via a written file, as configured above.

Tips

Protocol Buffer Debug Mode

By default, when you share a GTFS-realtime feed type through a URL like http://localhost:8080/alerts, the data returned will be binary-encoded Protocol Buffer data, as is expected for a GTFS-realtime feed. However, if you want to see what that data looks like, you can append ?debug to your URL, as in http://localhost:8080/alerts?debug to get a debug dump of the feed that looks something like:

header {
  gtfs_realtime_version: "1.0"
  incrementality: FULL_DATASET
  timestamp: 1330030345776
}
entity {
  id: "154323dc759418a69a9e80da7adbc96f"
...

This can be useful when debugging your feed.

Helper Methods

Be sure to check out the GtfsRealtimeLibrary class, which has a number of helper methods for constructing GTFS-realtime feeds.

Project Documentation

Other GTFS-realtime Resources

Be sure to check out all our GTFS-realtime resources.