public static void main(String[] args) { Subscriber subscriber = new Subscriber(); subscriber.subscribe("FastMovingStockQuotes"); Publisher publisher = new Publisher(); publisher.publishMessages("AllStockQuotes"); subscriber.unsubscribe(); }
@Test public void testBPubSub() throws InterruptedException { String topic = "test"; // Start pubsub server Server server = new Server("127.0.0.1", Settings.CLIENT_SUBSCRIBE_PORT, Settings.CLIENT_PUBLISH_PORT); server.start(); // Create publisher and subscriber Publisher publisher = new Publisher("127.0.0.1", Settings.CLIENT_PUBLISH_PORT); Subscriber subscriber = new Subscriber("127.0.0.1", Settings.CLIENT_SUBSCRIBE_PORT); // Subscribe to test topic TestPubSubCallback cb = new TestPubSubCallback(); subscriber.subscribe(topic, cb); Thread.sleep(1000); // Publish a message System.out.println("Publisher publishing " + "hello" + " on topic " + topic); publisher.publish(topic, PubSubProtos.StringMessage.newBuilder().setMessage("hello").build()); cb.awaitMessage("hello"); // Publish a message System.out.println("Publisher publishing " + "hello" + " on topic " + "badtest"); publisher.publish( "badtest", PubSubProtos.StringMessage.newBuilder().setMessage("hello").build()); cb.awaitNoMessage("hello"); // Publish a message System.out.println("Publisher publishing " + "hello2" + " on topic " + topic); publisher.publish(topic, PubSubProtos.StringMessage.newBuilder().setMessage("hello2").build()); cb.awaitMessage("hello2"); // Unsubscribe subscriber.unsubscribe(topic, cb); Thread.sleep(1000); // Publish a message System.out.println("Publisher publishing " + "hello2" + " on topic " + topic); publisher.publish(topic, PubSubProtos.StringMessage.newBuilder().setMessage("hello2").build()); cb.awaitNoMessage("hello2"); }