Java Examples: Difference between revisions

From Open Rail Data Wiki
EvelynSnow (talk | contribs)
Remove old sample, Gozirra is a horrid STOMP library, but also no longer works on the feeds
Tag: Replaced
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Gorizza Stomp library==
This page left intentionally blank.
 
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].
 
<pre>
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...");
}
}
</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}}

Latest revision as of 15:43, 26 October 2023