Download Diffusion 5.5 User Guide - Documentation

Transcript
9. Use a Thread.sleep() to hold the client open for a minute while the updates are
received and output.
// Wait for a minute while the stream prints updates
Thread.sleep(60000);
10.Compile and run your client.
We recommend that you run your client using the JDK rather than the JRE. The JDK includes
additional diagnostic capabilities that might be useful.
Results
The client outputs the value to the console every time the value of the foo/counter topic is
updated. You can update the value of the foo/counter topic by creating a publishing client to
update the topic. For more information, see Start publishing with Java on page 357.
Full example
The completed SubscribingClient class contains the following code:
import com.pushtechnology.diffusion.client.Diffusion;
import com.pushtechnology.diffusion.client.content.Content;
import com.pushtechnology.diffusion.client.features.Topics;
import
com.pushtechnology.diffusion.client.features.Topics.TopicStream;
import com.pushtechnology.diffusion.client.session.Session;
import
com.pushtechnology.diffusion.client.types.UpdateContext;
public class SubscribingClient {
public static void main(String... arguments) throws
Exception {
// Connect anonymously
// Replace 'host' with your hostname
Session session =
Diffusion.sessions().open("ws://host:port");
// Get the Topics feature to subscribe to topics
final Topics topics = session.feature(Topics.class);
// Add a new topic stream for 'foo/counter'
topics.addTopicStream(">foo/counter", new
TopicStreamPrintLn());
// Subscribe to the topic 'foo/counter'
topics.subscribe("foo/counter",
new Topics.CompletionCallback.Default());
// Wait for a minute while the stream prints updates
Thread.sleep(60000);
}
/**
* A topic stream that prints updates to the console.
*/
private static class TopicStreamPrintLn extends
TopicStream.Default {
@Override
public void onTopicUpdate(String topic, Content
content,
Diffusion | 356