Skip to content

Instantly share code, notes, and snippets.

@gavsmi
Created February 5, 2014 12:24
Show Gist options
  • Save gavsmi/8822579 to your computer and use it in GitHub Desktop.
Save gavsmi/8822579 to your computer and use it in GitHub Desktop.
Orchestra R5 CometD Java client example
package com.qmatic.qp.cometd.client;
import java.util.HashMap;
import java.util.Map;
import org.cometd.bayeux.Channel;
import org.cometd.bayeux.Message;
import org.cometd.bayeux.client.ClientSession;
import org.cometd.bayeux.client.ClientSessionChannel;
import org.cometd.client.BayeuxClient;
import org.cometd.client.transport.ClientTransport;
import org.cometd.client.transport.LongPollingTransport;
import org.eclipse.jetty.client.HttpClient;
import org.json.simple.JSONObject;
public class CometdClient {
private static final String CHANNEL_PREFIX = "/events/";
private final ClientSessionChannel.MessageListener eventListener = new EventListener();
// DeviceType represents the type of device / unit we are subscribing to events for
private final String deviceType = "67";
private ClientSession client;
private String branchPrefix = "";
private String unitId = "";
public static void main(String[] args) {
// URL to Orchestra qAgent cometD endpoint
String endpoint = "http://oas-host:8080/cometd";
// Prefix of branch subscribing to events for
String branchPrefix = "BRA";
// ID of unit subscribing to events for
String unitId = "WebPresentationPoint1080";
CometdClient cometd = new CometdClient(branchPrefix, unitId);
try {
cometd.connect(endpoint);
while(true) {}
} catch(Exception x) {
x.printStackTrace();
cometd.disconnect();
}
}
public CometdClient(String branchPrefix, String unitId) {
this.branchPrefix = branchPrefix;
this.unitId = unitId;
}
public void connect(String endpoint) throws Exception {
// Jetty HTTP client
HttpClient httpClient = new HttpClient();
httpClient.start();
// Prepare the transport
Map<String, Object> options = new HashMap<String, Object>();
ClientTransport transport = LongPollingTransport.create(options, httpClient);
// Create the Bayeux client
client = new BayeuxClient(endpoint, transport);
// Register a listener for the initial handshake
client.getChannel(Channel.META_HANDSHAKE).addListener(new ClientSessionChannel.MessageListener() {
public void onMessage(ClientSessionChannel channel, Message message) {
if (message.isSuccessful()) {
System.out.println("Handshake - OK :)");
// Register the event listener for the unit channel
// Channel name = '/events/{branch prefix}/{unit id}'
String channelName = CHANNEL_PREFIX + branchPrefix + "/" + unitId;
client.getChannel(channelName).subscribe(eventListener);
// We must send an INIT command in order to start receiving events
initConnection();
} else {
System.out.println("Something went wrong :(");
disconnect();
}
}
});
// Do handshake
client.handshake();
}
public void disconnect() {
if(client.isConnected())
client.disconnect();
}
@SuppressWarnings("unchecked")
private void initConnection() {
// Create the JSON payload for the INIT command
JSONObject prm = new JSONObject();
prm.put("uid", branchPrefix + ":" + unitId);
prm.put("type", this.deviceType);
prm.put("encoding", "QP_JSON");
JSONObject c = new JSONObject();
c.put("CMD", "INIT");
c.put("TGT", "CFM");
c.put("PRM", prm);
JSONObject data = new JSONObject();
data.put("M:", "C");
data.put("C", c);
data.put("N", "0");
// Send the INIT command JSON on the INIT channel '/events/INIT'
client.getChannel(CHANNEL_PREFIX + "INIT").publish(data);
}
private static class EventListener implements ClientSessionChannel.MessageListener {
public void onMessage(ClientSessionChannel channel, Message message) {
System.out.println("Message received: " + message.getJSON());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment