Difference between revisions of "Java Examples"

From Open Rail Data Wiki
Jump to navigation Jump to search
m (Added categories)
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Gorizza Stomp library==
 
==Gorizza Stomp library==
  
This code requires the [http://www.germane-software.com/software/Java/Gozirra/ Gozirra Stomp library].
+
This code will connect to the TRAIN_MVT_ALL_TOC topic and display all messages. It requires the [http://www.germane-software.com/software/Java/Gozirra/ Gozirra Stomp library].
  
<code>
+
N.B.
  private static void go() throws Exception
+
* You must ensure your National Rail topic subscription is consistent with the TOPIC variable
    {
+
* You may receive a compiler warning relating to the declaration of the Map variable in the MyListener class. This cannot be removed without a change to the Gorizza library, but does not affect the compilation or functionality.
        System.out.println("Connecting");
+
* The code has been successfully built using the following environment: Eclipse Luna SR1 (4.4.1), java 1.7.0_71, gorizza-0.4.1.jar
        final Client c = new Client(Constants.SERVER, Constants.PORT, Constants.USERNAME, Constants.PASSWORD);
+
 
        System.out.println("Connected");
+
<pre>
        System.out.println("Subscribing");
+
import net.ser1.stomp.Client;
        for (final String topic : Constants.TOPICS)
+
import net.ser1.stomp.Listener;
        {
+
 
            System.out.println(" " + topic);
+
/**
            c.subscribe("/topic/" + topic,
+
  * Example client that connects to the Network Rail ActiveMQ
                    new Listener()
+
* and subscribes a listener to receive real-time messages
                    {
+
*
                        public void message(Map map, String s)
+
* @author Martin.Swanson@blackkitetechnology.com
                        {
+
*/
                            processMessage(map, s);
+
public class MyClient {
                        }
+
 
                    });
+
//Network Rail ActiveMQ server
        }
+
private static final String SERVER = "datafeeds.networkrail.co.uk";
        System.out.println("done subscribing");
+
    }
+
// Server port for STOMP clients
</code>
+
private static final int PORT = 61618;
 +
 +
// Your account username, typically an email address
 +
private static final String USERNAME = "yourUserName";
 +
 +
// Your account password
 +
private static final String PASSWORD = "yourPassword";
 +
 +
// Example topic (this one is for Southern Train Movements)
 +
private static final String TOPIC = "/topic/TRAIN_MVT_ALL_TOC";
 +
 +
public static void main(String[] args) throws Exception {
 +
new MyClient().go();
 +
}
 +
 +
/*
 +
* Connect to a single topic and subscribe a listener
 +
* @throws Exception Too lazy to implement exception handling....
 +
*/
 +
private void go() throws Exception {
 +
System.out.println("| Connecting...");
 +
Client client = new Client(SERVER, PORT, USERNAME, PASSWORD);
 +
if (client.isConnected()) {
 +
System.out.println("| Connected to " + SERVER + ":" + PORT);
 +
} else {
 +
System.out.println("| Could not connect");
 +
return;
 +
}
 +
System.out.println("| Subscribing...");
 +
Listener listener = new MyListener();
 +
client.subscribe(TOPIC , listener);
 +
System.out.println("| Subscribed to " + TOPIC);
 +
System.out.println("| Waiting for message...");
 +
}
 +
}
 +
</pre>
 +
 
 +
<pre>
 +
import net.ser1.stomp.Listener;
 +
import java.util.Map;
 +
 +
/**
 +
* Example listener process that receives messages
 +
* in JSON format from the Network Rail ActiveMQ
 +
*
 +
* @author Martin.Swanson@blackkitetechnology.com
 +
*/
 +
public class MyListener implements Listener {
 +
   
 +
@Override
 +
public void message(Map header, String body) {
 +
System.out.println("| Got header: " + header);
 +
System.out.println("| Got body: " + body);
 +
}
 +
}
 +
</pre>
  
 
{{Navtable-DataFeeds}}
 
{{Navtable-DataFeeds}}

Revision as of 13:55, 9 January 2015

Gorizza Stomp library

This code will connect to the TRAIN_MVT_ALL_TOC topic and display all messages. It requires the Gozirra Stomp library.

N.B.

  • You must ensure your National Rail topic subscription is consistent with the TOPIC variable
  • You may receive a compiler warning relating to the declaration of the Map variable in the MyListener class. This cannot be removed without a change to the Gorizza library, but does not affect the compilation or functionality.
  • The code has been successfully built using the following environment: Eclipse Luna SR1 (4.4.1), java 1.7.0_71, gorizza-0.4.1.jar
import net.ser1.stomp.Client;
import net.ser1.stomp.Listener;

/**
 * Example client that connects to the Network Rail ActiveMQ
 * and subscribes a listener to receive real-time messages
 * 
 * @author Martin.Swanson@blackkitetechnology.com
 */
public class MyClient {

	//Network Rail ActiveMQ server
	private static final String SERVER = "datafeeds.networkrail.co.uk";
	
	// Server port for STOMP clients
	private static final int PORT = 61618;
	
	// Your account username, typically an email address
	private static final String USERNAME = "yourUserName";
	
	// Your account password
	private static final String PASSWORD = "yourPassword";
	
	// Example topic (this one is for Southern Train Movements)
	private static final String TOPIC = "/topic/TRAIN_MVT_ALL_TOC"; 
	
	public static void main(String[] args) throws Exception {
		new MyClient().go();
	}
	
	/*
	 * Connect to a single topic and subscribe a listener
	 * @throws Exception Too lazy to implement exception handling....
	 */
	private void go() throws Exception {
		System.out.println("| Connecting...");
		Client client = new Client(SERVER, PORT, USERNAME, PASSWORD);
		if (client.isConnected()) {
			System.out.println("| Connected to " + SERVER + ":" + PORT);
		} else {
			System.out.println("| Could not connect");
			return;
		}
		System.out.println("| Subscribing...");
		Listener listener = new MyListener();
		client.subscribe(TOPIC , listener);
		System.out.println("| Subscribed to " + TOPIC);
		System.out.println("| Waiting for message...");
	}	
}
import net.ser1.stomp.Listener;
import java.util.Map;
 
/**
 * Example listener process that receives messages 
 * in JSON format from the Network Rail ActiveMQ
 * 
 * @author Martin.Swanson@blackkitetechnology.com
 */
public class MyListener implements Listener {
    
	@Override
	public void message(Map header, String body) {
		System.out.println("| Got header: " + header);
		System.out.println("| Got body: " + body);
	}
}


Network Rail Open Data Feeds
Data Feeds About the Feeds Account States Durable Subscriptions Example Code ( PHP / C# / Java / Ruby / Node.js) • Advanced UsesFAQ Release Notes
RTPPM RTPPM Feed
Train Movements Train Movements Feed Train Activation Train Cancellation Train Movement Train Reinstatement Change of Origin Change of Identity Change of Location TSPEED Field Planned Cancellations Cancellation Codes
TD TD Feed C-Class Messages S-Class Messages Train Describers TD Berths
VSTP VSTP Feed
TSR TSR Feed Route Codes
SCHEDULE SCHEDULE Feed Schedule and Location Records Association Records CIF Codes How Scheduling Works Allowances
Reference Data Reference Data Feed TOC Codes CIF Codes Delay Attribution Codes Identifying Locations (STANOX, TIPLOC, NLC and 3-Alpha Codes) STANOX Geographical Areas Train Planning data