Skip to content

Commit a0c1d9c

Browse files
committed
gtfs: move realtime init code to where the rest is
1 parent 18bfcc9 commit a0c1d9c

File tree

7 files changed

+69
-129
lines changed

7 files changed

+69
-129
lines changed

web-bundle/src/main/java/com/graphhopper/http/GraphHopperBundle.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@
3636
import com.graphhopper.util.PMap;
3737
import com.graphhopper.util.TranslationMap;
3838
import com.graphhopper.util.details.PathDetailsBuilderFactory;
39+
import io.dropwizard.client.HttpClientBuilder;
3940
import io.dropwizard.core.ConfiguredBundle;
4041
import io.dropwizard.core.setup.Bootstrap;
4142
import io.dropwizard.core.setup.Environment;
43+
import org.apache.hc.client5.http.classic.HttpClient;
4244
import org.glassfish.hk2.api.Factory;
4345
import org.glassfish.hk2.utilities.binding.AbstractBinder;
4446

4547
import javax.inject.Inject;
48+
import javax.inject.Singleton;
4649

4750
public class GraphHopperBundle implements ConfiguredBundle<GraphHopperBundleConfiguration> {
4851

@@ -205,6 +208,26 @@ public void dispose(Boolean instance) {
205208
}
206209
}
207210

211+
private static class EmptyRealtimeFeedFactory implements Factory<RealtimeFeed> {
212+
213+
private final GtfsStorage staticGtfs;
214+
215+
@Inject
216+
EmptyRealtimeFeedFactory(GtfsStorage staticGtfs) {
217+
this.staticGtfs = staticGtfs;
218+
}
219+
220+
@Override
221+
public RealtimeFeed provide() {
222+
return RealtimeFeed.empty();
223+
}
224+
225+
@Override
226+
public void dispose(RealtimeFeed realtimeFeed) {
227+
228+
}
229+
}
230+
208231
@Override
209232
public void initialize(Bootstrap<?> bootstrap) {
210233
// See #1440: avoids warning regarding com.fasterxml.jackson.module.afterburner.util.MyClassLoader
@@ -307,5 +330,27 @@ protected void configure() {
307330
environment.healthChecks().register("graphhopper", new GraphHopperHealthCheck(graphHopper));
308331
environment.jersey().register(environment.healthChecks());
309332
environment.jersey().register(HealthCheckResource.class);
333+
334+
if (configuration.gtfsrealtime().getFeeds().isEmpty()) {
335+
environment.jersey().register(new AbstractBinder() {
336+
@Override
337+
protected void configure() {
338+
bindFactory(EmptyRealtimeFeedFactory.class).to(RealtimeFeed.class).in(Singleton.class);
339+
}
340+
});
341+
} else {
342+
final HttpClient httpClient = new HttpClientBuilder(environment)
343+
.using(configuration.gtfsrealtime().getHttpClientConfiguration())
344+
.build("gtfs-realtime-feed-loader");
345+
RealtimeFeedLoadingCache realtimeFeedLoadingCache = new RealtimeFeedLoadingCache(((GraphHopperGtfs) graphHopper), httpClient, configuration);
346+
environment.lifecycle().manage(realtimeFeedLoadingCache);
347+
environment.jersey().register(new AbstractBinder() {
348+
@Override
349+
protected void configure() {
350+
bind(httpClient).to(HttpClient.class);
351+
bindFactory(realtimeFeedLoadingCache).to(RealtimeFeed.class);
352+
}
353+
});
354+
}
310355
}
311356
}

web-bundle/src/main/java/com/graphhopper/http/GraphHopperBundleConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public interface GraphHopperBundleConfiguration {
2424

2525
GraphHopperConfig getGraphHopperConfiguration();
2626

27+
RealtimeConfiguration gtfsrealtime();
28+
2729
}

web-bundle/src/main/java/com/graphhopper/http/RealtimeBundle.java

Lines changed: 0 additions & 83 deletions
This file was deleted.

web-bundle/src/main/java/com/graphhopper/http/RealtimeBundleConfiguration.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

web-bundle/src/main/java/com/graphhopper/http/RealtimeFeedLoadingCache.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import com.google.common.util.concurrent.ListenableFuture;
2626
import com.google.common.util.concurrent.ListenableFutureTask;
2727
import com.google.transit.realtime.GtfsRealtime;
28-
import com.graphhopper.gtfs.GtfsStorage;
28+
import com.graphhopper.gtfs.GraphHopperGtfs;
2929
import com.graphhopper.gtfs.RealtimeFeed;
3030
import com.graphhopper.gtfs.Transfers;
31-
import com.graphhopper.routing.util.EncodingManager;
32-
import com.graphhopper.storage.BaseGraph;
3331
import io.dropwizard.lifecycle.Managed;
3432
import org.apache.hc.client5.http.classic.HttpClient;
3533
import org.apache.hc.client5.http.classic.methods.HttpGet;
@@ -48,27 +46,24 @@
4846
public class RealtimeFeedLoadingCache implements Factory<RealtimeFeed>, Managed {
4947

5048
private final HttpClient httpClient;
51-
private final BaseGraph baseGraph;
52-
private final EncodingManager encodingManager;
53-
private final GtfsStorage gtfsStorage;
54-
private final RealtimeBundleConfiguration bundleConfiguration;
49+
private final GraphHopperGtfs graphHopper;
50+
private final GraphHopperBundleConfiguration bundleConfiguration;
5551
private ExecutorService executor;
5652
private LoadingCache<String, RealtimeFeed> cache;
5753
private Map<String, Transfers> transfers;
5854

5955
@Inject
60-
RealtimeFeedLoadingCache(BaseGraph baseGraph, EncodingManager encodingManager, GtfsStorage gtfsStorage, HttpClient httpClient, RealtimeBundleConfiguration bundleConfiguration) {
61-
this.baseGraph = baseGraph;
62-
this.encodingManager = encodingManager;
63-
this.gtfsStorage = gtfsStorage;
56+
RealtimeFeedLoadingCache(GraphHopperGtfs graphHopper, HttpClient httpClient, GraphHopperBundleConfiguration bundleConfiguration) {
57+
this.graphHopper = graphHopper;
6458
this.bundleConfiguration = bundleConfiguration;
6559
this.httpClient = httpClient;
6660
}
6761

6862
@Override
6963
public void start() {
64+
System.out.println("Starting RealtimeFeedLoadingCache");
7065
this.transfers = new HashMap<>();
71-
for (Map.Entry<String, GTFSFeed> entry : this.gtfsStorage.getGtfsFeeds().entrySet()) {
66+
for (Map.Entry<String, GTFSFeed> entry : this.graphHopper.getGtfsStorage().getGtfsFeeds().entrySet()) {
7267
this.transfers.put(entry.getKey(), new Transfers(entry.getValue()));
7368
}
7469
this.executor = Executors.newSingleThreadExecutor();
@@ -112,14 +107,24 @@ private RealtimeFeed fetchFeedsAndCreateGraph() {
112107
Map<String, GtfsRealtime.FeedMessage> feedMessageMap = new HashMap<>();
113108
for (FeedConfiguration configuration : bundleConfiguration.gtfsrealtime().getFeeds()) {
114109
try {
115-
GtfsRealtime.FeedMessage feedMessage = httpClient.execute(new HttpGet(configuration.getUrl().toURI()),
116-
response -> GtfsRealtime.FeedMessage.parseFrom(response.getEntity().getContent()));
117-
feedMessageMap.put(configuration.getFeedId(), feedMessage);
110+
switch (configuration.getUrl().getProtocol()) {
111+
case "http": {
112+
GtfsRealtime.FeedMessage feedMessage = httpClient.execute(new HttpGet(configuration.getUrl().toURI()),
113+
response -> GtfsRealtime.FeedMessage.parseFrom(response.getEntity().getContent()));
114+
feedMessageMap.put(configuration.getFeedId(), feedMessage);
115+
break;
116+
}
117+
case "file": {
118+
GtfsRealtime.FeedMessage feedMessage = GtfsRealtime.FeedMessage.parseFrom(configuration.getUrl().openStream());
119+
feedMessageMap.put(configuration.getFeedId(), feedMessage);
120+
break;
121+
}
122+
}
118123
} catch (IOException | URISyntaxException e) {
119124
throw new RuntimeException(e);
120125
}
121126
}
122-
return RealtimeFeed.fromProtobuf(gtfsStorage, this.transfers, feedMessageMap);
127+
return RealtimeFeed.fromProtobuf(graphHopper.getGtfsStorage(), this.transfers, feedMessageMap);
123128
}
124129

125130
}

web/src/main/java/com/graphhopper/application/GraphHopperApplication.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.graphhopper.application.resources.RootResource;
2323
import com.graphhopper.http.CORSFilter;
2424
import com.graphhopper.http.GraphHopperBundle;
25-
import com.graphhopper.http.RealtimeBundle;
2625
import com.graphhopper.navigation.NavigateResource;
2726
import io.dropwizard.assets.AssetsBundle;
2827
import io.dropwizard.core.Application;
@@ -41,7 +40,6 @@ public static void main(String[] args) throws Exception {
4140
@Override
4241
public void initialize(Bootstrap<GraphHopperServerConfiguration> bootstrap) {
4342
bootstrap.addBundle(new GraphHopperBundle());
44-
bootstrap.addBundle(new RealtimeBundle());
4543
bootstrap.addCommand(new ImportCommand());
4644
bootstrap.addCommand(new MatchCommand());
4745
bootstrap.addBundle(new AssetsBundle("/com/graphhopper/maps/", "/maps/", "index.html"));

web/src/main/java/com/graphhopper/application/GraphHopperServerConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
import com.fasterxml.jackson.annotation.JsonProperty;
2121
import com.graphhopper.GraphHopperConfig;
2222
import com.graphhopper.http.GraphHopperBundleConfiguration;
23-
import com.graphhopper.http.RealtimeBundleConfiguration;
2423
import com.graphhopper.http.RealtimeConfiguration;
2524
import io.dropwizard.core.Configuration;
2625

2726
import javax.validation.constraints.NotNull;
2827

29-
public class GraphHopperServerConfiguration extends Configuration implements GraphHopperBundleConfiguration, RealtimeBundleConfiguration {
28+
public class GraphHopperServerConfiguration extends Configuration implements GraphHopperBundleConfiguration {
3029

3130
@NotNull
3231
@JsonProperty
@@ -43,7 +42,6 @@ public GraphHopperConfig getGraphHopperConfiguration() {
4342
return graphhopper;
4443
}
4544

46-
@Override
4745
public RealtimeConfiguration gtfsrealtime() {
4846
return gtfsRealtime;
4947
}

0 commit comments

Comments
 (0)