-
Notifications
You must be signed in to change notification settings - Fork 134
feat(java): Release new version of the gtfs realtime bindings jar #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jcpitre
merged 8 commits into
master
from
160-release-new-version-of-the-gtfs-realtime-bindings-jars
May 25, 2026
+25,276
−6,379
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e6c3a63
Upgraded gtfs-realtime.proto. Temporary since it's done in another br…
jcpitre e78c6a5
Support manual staging via workflow_dispatch by splitting publish con…
jcpitre 7a40f82
Remove publishing profile split, keep single ci-cd profile
jcpitre 9e67e2c
Merge branch 'master' into 160-release-new-version-of-the-gtfs-realti…
jcpitre 455cf96
Generated GtfsRealtime.java from new version of gtfs-realtime.proto
jcpitre 1de522c
Add Java example project demonstrating building, serializing and pars…
jcpitre c9a342d
Updated version to 0.1.0
jcpitre 68e1dce
Removed comments
jcpitre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>org.mobilitydata</groupId> | ||
| <artifactId>gtfs-realtime-bindings-example</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <name>gtfs-realtime-bindings-example</name> | ||
| <description>Example showing how to use the gtfs-realtime-bindings Java library.</description> | ||
|
|
||
| <properties> | ||
| <!-- Change this to the desired release version, e.g. 0.1.0, to use a release build. | ||
| Keep the -SNAPSHOT suffix to use the latest snapshot. --> | ||
| <gtfs-realtime-bindings.version>0.1.0</gtfs-realtime-bindings.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.mobilitydata</groupId> | ||
| <artifactId>gtfs-realtime-bindings</artifactId> | ||
| <version>${gtfs-realtime-bindings.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <!-- Snapshot repository — only needed when using a -SNAPSHOT version above. | ||
| Safe to remove when pointing to a release. --> | ||
| <repositories> | ||
| <repository> | ||
| <id>central-snapshots</id> | ||
| <url>https://central.sonatype.com/repository/maven-snapshots</url> | ||
| <snapshots><enabled>true</enabled></snapshots> | ||
| <releases><enabled>false</enabled></releases> | ||
| </repository> | ||
| </repositories> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.5.1</version> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </configuration> | ||
| </plugin> | ||
| <!-- Run with: mvn exec:java --> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| <configuration> | ||
| <mainClass>org.mobilitydata.example.GtfsRealtimeExample</mainClass> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
123 changes: 123 additions & 0 deletions
123
java/example/src/main/java/org/mobilitydata/example/GtfsRealtimeExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package org.mobilitydata.example; | ||
|
|
||
| import com.google.transit.realtime.GtfsRealtime.*; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
|
|
||
| /** | ||
| * Minimal example demonstrating how to build, serialize, and parse a GTFS-Realtime feed | ||
| * using the gtfs-realtime-bindings Java library. | ||
| * | ||
| * <p>This example builds a feed message containing three entity types: | ||
| * <ul> | ||
| * <li>A vehicle position update</li> | ||
| * <li>A trip update with a stop time update</li> | ||
| * <li>A service alert</li> | ||
| * </ul> | ||
| * | ||
| * <p>Run with: {@code mvn exec:java} from the {@code example/} directory. | ||
| */ | ||
| public class GtfsRealtimeExample { | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
|
|
||
| // --- Build a FeedMessage --- | ||
|
|
||
| FeedMessage feed = FeedMessage.newBuilder() | ||
| .setHeader(FeedHeader.newBuilder() | ||
| .setGtfsRealtimeVersion("2.0") | ||
| .setIncrementality(FeedHeader.Incrementality.FULL_DATASET) | ||
| .setTimestamp(System.currentTimeMillis() / 1000)) | ||
|
|
||
| // Entity 1: vehicle position | ||
| .addEntity(FeedEntity.newBuilder() | ||
| .setId("vehicle-1") | ||
| .setVehicle(VehiclePosition.newBuilder() | ||
| .setTrip(TripDescriptor.newBuilder() | ||
| .setTripId("trip-42") | ||
| .setRouteId("route-7")) | ||
| .setPosition(Position.newBuilder() | ||
| .setLatitude(45.5017f) | ||
| .setLongitude(-73.5673f) | ||
| .setBearing(180.0f) | ||
| .setSpeed(12.5f)) | ||
| .setVehicle(VehicleDescriptor.newBuilder() | ||
| .setId("bus-101") | ||
| .setLabel("101")) | ||
| .setCurrentStatus(VehiclePosition.VehicleStopStatus.IN_TRANSIT_TO) | ||
| .setCurrentStopSequence(5) | ||
| .setOccupancyStatus(VehiclePosition.OccupancyStatus.MANY_SEATS_AVAILABLE))) | ||
|
|
||
| // Entity 2: trip update with a stop time update | ||
| .addEntity(FeedEntity.newBuilder() | ||
| .setId("trip-update-1") | ||
| .setTripUpdate(TripUpdate.newBuilder() | ||
| .setTrip(TripDescriptor.newBuilder() | ||
| .setTripId("trip-42") | ||
| .setRouteId("route-7")) | ||
| .addStopTimeUpdate(TripUpdate.StopTimeUpdate.newBuilder() | ||
| .setStopSequence(5) | ||
| .setStopId("stop-99") | ||
| .setArrival(TripUpdate.StopTimeEvent.newBuilder() | ||
| .setDelay(120)) // 2 minutes late | ||
| .setDeparture(TripUpdate.StopTimeEvent.newBuilder() | ||
| .setDelay(120))))) | ||
|
|
||
| // Entity 3: service alert | ||
| .addEntity(FeedEntity.newBuilder() | ||
| .setId("alert-1") | ||
| .setAlert(Alert.newBuilder() | ||
| .addInformedEntity(EntitySelector.newBuilder() | ||
| .setRouteId("route-7")) | ||
| .setHeaderText(TranslatedString.newBuilder() | ||
| .addTranslation(TranslatedString.Translation.newBuilder() | ||
| .setLanguage("en") | ||
| .setText("Route 7 delay"))) | ||
| .setDescriptionText(TranslatedString.newBuilder() | ||
| .addTranslation(TranslatedString.Translation.newBuilder() | ||
| .setLanguage("en") | ||
| .setText("Route 7 is experiencing delays due to traffic."))) | ||
| .setCause(Alert.Cause.TECHNICAL_PROBLEM) | ||
| .setEffect(Alert.Effect.SIGNIFICANT_DELAYS))) | ||
|
|
||
| .build(); | ||
|
|
||
| System.out.println("Built feed with " + feed.getEntityCount() + " entities."); | ||
|
|
||
| // --- Serialize to bytes (as you would when writing to a file or HTTP response) --- | ||
|
|
||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| feed.writeTo(out); | ||
| byte[] bytes = out.toByteArray(); | ||
| System.out.println("Serialized feed: " + bytes.length + " bytes."); | ||
|
|
||
| // --- Parse back from bytes (as a consumer would after fetching a GTFS-RT URL) --- | ||
|
|
||
| FeedMessage parsed = FeedMessage.parseFrom(new ByteArrayInputStream(bytes)); | ||
| System.out.println("Parsed feed version : " + parsed.getHeader().getGtfsRealtimeVersion()); | ||
| System.out.println("Parsed entity count : " + parsed.getEntityCount()); | ||
|
|
||
| for (FeedEntity entity : parsed.getEntityList()) { | ||
| if (entity.hasVehicle()) { | ||
| VehiclePosition vp = entity.getVehicle(); | ||
| System.out.printf(" Vehicle id=%-12s lat=%.4f lon=%.4f speed=%.1f m/s%n", | ||
| vp.getVehicle().getId(), | ||
| vp.getPosition().getLatitude(), | ||
| vp.getPosition().getLongitude(), | ||
| vp.getPosition().getSpeed()); | ||
| } else if (entity.hasTripUpdate()) { | ||
| TripUpdate tu = entity.getTripUpdate(); | ||
| System.out.printf(" TripUpd trip=%-10s stops=%d delay=%ds%n", | ||
| tu.getTrip().getTripId(), | ||
| tu.getStopTimeUpdateCount(), | ||
| tu.getStopTimeUpdate(0).getArrival().getDelay()); | ||
| } else if (entity.hasAlert()) { | ||
| Alert a = entity.getAlert(); | ||
| System.out.printf(" Alert cause=%-20s \"%s\"%n", | ||
| a.getCause(), | ||
| a.getHeaderText().getTranslation(0).getText()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍