PrintWriter out = response.getWriter(); String msg = "Topic is required, Message is optional.\nValid topics: " + PubSub.getInstance().getTopics(); if (topic != null) { LOGGER.info("Submitting topic " + topic + ", message " + message); try { PubSub.getInstance().publish(PubSub.Topic.fromString(topic), message); msg = "Submitted topic " + topic + ", message " + message; } catch (Exception e) { for (PubSub.Topic t : PubSub.getInstance().getTopics()) { topics.append("<option>").append(t.getName()).append("</option>");
@Override @Before public void before() throws Exception { // Subscribe to all defined topics. // After each successfull HTTP-GET, testSubscriber.onMessage() // will be called and "topic" and "message" fields will be // set according to HTTP-GET parameters. testSubscriber.count = 0; for (Topic topic : topics) { PubSub.getInstance().subscribe(topic, testSubscriber); } Assert.assertEquals("Unexpected number of registered topics", topics.length, PubSub.getInstance().getTopics().size()); }
/** * Public factory method to guarantee singleton */ public static synchronized PubSub getInstance() { if (instance != null) { LOGGER.info(instance + " requested"); } else { instance = new PubSub(); LOGGER.info(instance + " created"); } return instance; }
@Override @After public void after() { // unsubscribe for (Topic topic : topics) { PubSub.getInstance().unsubscribe(topic, testSubscriber); } testSubscriber.count = 0; Assert.assertTrue("Topics should have been removed after unsubscribe()", PubSub.getInstance().getTopics().isEmpty()); }
/** * Submit topic that has no subscribers. * @throws Exception */ @Test public void testPubSubNonSubscribedTopic() throws Exception { // At this point all valid Topic values have been subscribed to // in before() method. // Unsubscribe from one topic and then try publishing to it. PubSub.getInstance().unsubscribe(Topic.HDFS_SYNC_HMS, testSubscriber); Assert.assertEquals("Unexpected number of registered topics", topics.length-1, PubSub.getInstance().getTopics().size()); URL url = new URL(String.format(REQUEST_URL, Topic.HDFS_SYNC_HMS.getName(), "message")); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); Assert.assertEquals("Unexpected response code", HttpURLConnection.HTTP_BAD_REQUEST, conn.getResponseCode()); } finally { safeClose(conn); } // re-subscribe, not to upset after() method which expects all topics to be subscribed to PubSub.getInstance().subscribe(Topic.HDFS_SYNC_HMS, testSubscriber); }
PubSub.getInstance().subscribe(PubSub.Topic.HDFS_SYNC_HMS, this);
PubSub.getInstance().publish(PubSub.Topic.HDFS_SYNC_HMS, "message");