Skip to main content

Create a Channel

The example-5 script authenticates an Integration Service client to manage Identities using the Admin identity created in example-0 and then performs the following tasks:

  1. Creates a channel.
  2. Writes data on the channel.
  3. Reads data from the channel

You can run the example with the following command:

mvn exec:_java -Dexec.mainClass=net.gradbase.how_tos.CreateChannel

Example source code: Example-5

Create Channel

You can create a channel using the following script:

Map<String, String> topics = new HashMap<String, String>();
topics.put("type", "example-data");
topics.put("source", "data-creator");

List<Map<String, String>> allTopics = new ArrayList<Map<String, String>>();
allTopics.add(topics);

// Create a new channel for example data
JSONObject newChannelDetails = channelClient.create(null, allTopics, null, null, null);

// The channel address is used to read and write to channels
String channelAddress = newChannelDetails.getString("channelAddress");
System.out.println(channelAddress);

Write on a Channel

You can write on a channel using the following script:

// Writing 3 data packets to channel
for (int i = 0; i < 3; i++) {
System.out.println("writing data to channel no " + i);
channelClient.write(channelAddress, "log", null, new JSONObject().put("log", "This is log number " + i));
}

Read from a Channel

You can read from a channel using the following script:

List<ChannelData> datas = channelClient.read(channelAddress, null, null, null, null, null);

for (ChannelData data : datas) {
System.out.println(data.toString());
}