OneBusAway / onebusaway-gtfs-realtime-visualizer

A visualizer for GTFS-realtime transit data.
https://github.com/OneBusAway/onebusaway-gtfs-realtime-visualizer/wiki
Other
56 stars 18 forks source link

Run on custom port number #13

Open answerquest opened 5 years ago

answerquest commented 5 years ago

This pull request : https://github.com/OneBusAway/onebusaway-gtfs-realtime-visualizer/pull/2 had made the JS part friendly for working on any URL that the application would get launched on. That's great. But I'm unable to find a way to launch the application on a port number different from 8080.

I tried the typical --port=5050, "portnum", "portnumber" etc and none seem to work. I am able to launch the application successfully of course; now I need to launch multiple instances of it to see multiple feeds.

answerquest commented 5 years ago

I think this is the operative code:

https://github.com/OneBusAway/onebusaway-gtfs-realtime-visualizer/blob/master/src/main/java/org/onebusaway/gtfs_realtime/visualizer/VisualizerServer.java

...
public class VisualizerServer {

  private static final Logger _log = LoggerFactory.getLogger(VisualizerServer.class);

  private DataServlet _dataServlet;

  private int _port = 8080;

  private Server _server;

  @Inject
  public void setDataServlet(DataServlet dataServlet) {
    _dataServlet = dataServlet;
  }

  public void setPort(int port) {
    _port = port;
  }
...

What must I do from here? The setPort method looks promising, but I don't see it getting used anywhere else. And if I edit this, I'm guessing it needs compiling to make it to the .jar on top. How to do that?

CC: @barbeau

barbeau commented 5 years ago

@answerquest It's been a while since I worked with this project, but take a look at VisualizerMain. To pass the port in as a command line parameter you'll want to handle the port number similarly to ARG_VEHICLE_POSITIONS_URL.

Alternately, you can just edit the default port in VisualizerServer.

Then, after editing the code run mvn package and you should get a new JAR file.

answerquest commented 5 years ago

@barbeau I'd started working in the same direction! I now have 10 wonderful little .jar files sitting on my server, ready to output to a different port number each :+1:

Changes needed to some source files

Sharing some experience notes for folks who like me are looking at java from the outside in:

  1. The new .jar file we compile/build will be completely stand-alone - treat it as you would treat a .exe. So you can move it out and drop it anywhere without needing any accompanying files.
  2. A side-effect is that the API key and all other tweaks are now fully baked into the .jar file. You can't change them later.
  3. So you need to do all your tweaks before running the mvn package command mentioned, and in case of any changes, re-build a new .jar file.

These are the changes I needed to do at my end:

  1. Change port
    src/main/java/org/onebusaway/gtfs_realtime/visualizer/VisualizerMain.java line 38:

    private int _port = 8081;

    So change this portnum between successive builds, and you'll get .jar files configured for different port numbers.

  2. Change the Google Maps API key:
    src/main/resources/org/onebusaway/gtfs_realtime/visualizer/index.html line 30:

    src="https://maps.googleapis.com/maps/api/js?key=[[your API key]]">
  3. Fix https:// path for the bus icon
    src/main/resources/org/onebusaway/gtfs_realtime/visualizer/index.js line 90:

    'http://' + hostandport + '/WhiteCircle8.png', null, null,

Setup Java 8, Maven

Once these changes are done, I ~run the~ wait! Gotta install these things first!
I found there is a complication regarding Java : Since Java 11, a critical component needed by this app called java.xml.ws.annotation has been deprecated. In Java 9/10 it could still be roped but needed mentioning in the command line (inconvenient!). Java 8 was the last version where everything was fine. For this reason, Java 8 is still installed alongside the latest stuff as a legacy fallback in most online web servers. So that's what we'll do. Following commands work for Ubuntu/Linux based OS's. And mind, I arrived here after a lot of other dead ends.

sudo apt update
sudo apt install openjdk-8-jdk-headless
sudo apt install maven

After installation, set the "default" java to be java 8 instead of java 11:

sudo update-alternatives --config java
sudo update-alternatives --config javac

After entering both commands you'll be asked by terminal to choose between java versions. Choose Java 8. If you're worried about the impact on other stuff then come back to this step after everything is over and change the "default" java back to what it was.

Ok, now we're set.

Build your new standalone .jar application

This was the easiest. Just run this in terminal at the top folder:

mvn package

At my end, I've created these:

onebusaway_8080.jar
onebusaway_8081.jar
onebusaway_8082.jar
onebusaway_8083.jar
onebusaway_8084.jar
onebusaway_8085.jar
onebusaway_8086.jar
onebusaway_8087.jar
onebusaway_8088.jar
onebusaway_8089.jar

Each .jar launches OneBusAway GTFS-RT Visualizer on the corresponding port number. I'd share these online, but unfortunately since my API key is "baked in" as I mentioned above, gotta keep 'em in-house for now.

Extra: Running on server.

Including this as I think this is what folks seriously working on this would be chasing ultimately. To run it on a linux-based SSH-accessed web server in an always-on way, here's what we do:

nohup java -jar /fullpath/onebusaway_8080.jar --vehiclePositionsUrl=[[Feed URL]] >> /fullpath/onebusaway.log 2>&1 &

Also, there is an error-condition that happens at least once a day in the feed at my end and prevents the app from working further : see details here: #12 I've posted there a workaround I'm using to keep it always-on.

Please feel free to close this issue if nothing else is needed.

barbeau commented 5 years ago

@answerquest Thanks for the details! Ultimately we'd want to pass the port number as a command-line parameter so I'll leave this issue open until that's implemented. That way we only need one JAR file.