Skip to content
This repository was archived by the owner on Aug 8, 2020. It is now read-only.

HowTo: Subscription

michaelwittig edited this page Oct 26, 2014 · 6 revisions

What is a subscription?

kdb+tick brings a mechanism to publish/subscribe data. Mostly you will publish parts of a table. Other Q processes can open a connection to the publisher and subscribe for interessting data. More information is given here.

Code

  1. Define the listener.
// static table definition
final MyTable table = MyTable.get();

QConnectorListener listener = new QConnectorListener() {
	@Override
	public void exception(QConnectorException e) {
		// ...
	}
	@Override
	public void error(QConnectorError e) {
		// ...
	}
	@Override
	public void resultReceived(String handle, Result result) {
		for (final TableRow row : table.read(result)) {
			System.out.println(row.get(table.time()));
			System.out.println(row.get(table.sym()));
			System.out.println(row.get(table.price()));
			System.out.println(row.get(table.size()));
		}
	}
};		
  1. Create and start the connector
QConnectorAsync c = QConnectorFactory.create(listener , "localhost", 5010, true);
c.connect();
  1. Subscribe to trades and quotes for the Smbols FGBL, FGBM and FGBS.
c.subscribe("MyFirstSub", new String[] {"mytable"}, new String[] {"FGBL", "FGBM", "FGBS"});
  1. Just wait a couple of seconds and then close the connection.
Thread.sleep(1000 * 1000);
c.disconnect();

Clone this wiki locally