@Override public <T> void addSubscriber(String topic, Class<T> streamType, Consumer<T> subscriber) { getTopicHandler(topic, streamType).addSubscriber(subscriber); }
@SuppressWarnings("unchecked") @Override public <T> Consumer<T> getPublishDestination(String topic, Class<? super T> streamType) { return (Consumer<T>) getTopicHandler(topic, streamType); }
@SuppressWarnings("unchecked") private synchronized <T> TopicHandler<T> getTopicHandler(String topic, Class<T> streamType) { TopicHandler<T> topicHandler = (TopicHandler<T>) topicHandlers.get(topic); if (topicHandler == null) { topicHandlers.put(topic, topicHandler = new TopicHandler<T>(streamType)); } else { topicHandler.checkClass(streamType); } return topicHandler; } }
protected void registerPublishSubscribeService() { getServices().addService(PublishSubscribeService.class, new ProviderPubSub()); }
@Override public void removeSubscriber(String topic, Consumer<?> subscriber) { TopicHandler<?> topicHandler; synchronized (this) { topicHandler = topicHandlers.get(topic); } if (topicHandler != null) { topicHandler.removeSubscriber(subscriber); } }
@Override public void accept(Consumer<T> submitter) { PublishSubscribeService pubSub = rts.get().getService(PublishSubscribeService.class); if (pubSub != null) { this.submitter = submitter; pubSub.addSubscriber(topic, streamType, submitter); } } @Override
@Override public void close() throws Exception { PublishSubscribeService pubSub = rts.get().getService(PublishSubscribeService.class); if (pubSub != null) { pubSub.removeSubscriber(topic, submitter); } } }
@Override public void initialize(OpletContext<T, Void> context) { super.initialize(context); PublishSubscribeService pubSub = context.getService(PublishSubscribeService.class); if (pubSub != null) setSinker(pubSub.getPublishDestination(topic, streamType)); } }
protected void registerPublishSubscribeService() { getServices().addService(PublishSubscribeService.class, new ProviderPubSub()); }
@Override public <T> void addSubscriber(String topic, Class<T> streamType, Consumer<T> subscriber) { getTopicHandler(topic, streamType).addSubscriber(subscriber); }
@SuppressWarnings("unchecked") private synchronized <T> TopicHandler<T> getTopicHandler(String topic, Class<T> streamType) { TopicHandler<T> topicHandler = (TopicHandler<T>) topicHandlers.get(topic); if (topicHandler == null) { topicHandlers.put(topic, topicHandler = new TopicHandler<T>(streamType)); } else { topicHandler.checkClass(streamType); } return topicHandler; } }
@SuppressWarnings("unchecked") @Override public <T> Consumer<T> getPublishDestination(String topic, Class<? super T> streamType) { return (Consumer<T>) getTopicHandler(topic, streamType); }
@Override public void removeSubscriber(String topic, Consumer<?> subscriber) { TopicHandler<?> topicHandler; synchronized (this) { topicHandler = topicHandlers.get(topic); } if (topicHandler != null) { topicHandler.removeSubscriber(subscriber); } }
@Override public void accept(Consumer<T> submitter) { PublishSubscribeService pubSub = rts.get().getService(PublishSubscribeService.class); if (pubSub != null) { this.submitter = submitter; pubSub.addSubscriber(topic, streamType, submitter); } } @Override
@Override public void close() throws Exception { PublishSubscribeService pubSub = rts.get().getService(PublishSubscribeService.class); if (pubSub != null) { pubSub.removeSubscriber(topic, submitter); } } }
@Override public void initialize(OpletContext<T, Void> context) { super.initialize(context); PublishSubscribeService pubSub = context.getService(PublishSubscribeService.class); if (pubSub != null) setSinker(pubSub.getPublishDestination(topic, streamType)); } }
DirectProvider dp = new DirectProvider(); dp.getServices().addService(PublishSubscribeService.class, new ProviderPubSub());
@Test(timeout=10000) public void testProviderServiceSingleSubscriber() throws Exception { DirectProvider dp = new DirectProvider(); dp.getServices().addService(PublishSubscribeService.class, new ProviderPubSub()); TStream<String> publishedStream = createPublisher(dp, "t1", String.class, getStrs()); Tester testPub = publishedStream.topology().getTester(); Condition<List<String>> tcPub = testPub.streamContents(publishedStream, getStrs()); TStream<String> subscribedStream = createSubscriber(dp, "t1", String.class); Tester testSub = subscribedStream.topology().getTester(); Condition<List<String>> tcSub = testSub.streamContents(subscribedStream, getStrs()); // Expect all tuples Job js = dp.submit(subscribedStream.topology()).get(); // Give the subscriber a chance to setup. while (js.getCurrentState() != State.RUNNING) Thread.sleep(50); Job jp = dp.submit(publishedStream.topology()).get(); while (!tcSub.valid() || !tcPub.valid()) Thread.sleep(50); assertTrue(tcPub.valid()); assertTrue(tcSub.valid()); js.stateChange(Action.CLOSE); jp.stateChange(Action.CLOSE); }
DirectProvider dp = new DirectProvider(); dp.getServices().addService(PublishSubscribeService.class, new ProviderPubSub());
DirectProvider dp = new DirectProvider(); dp.getServices().addService(PublishSubscribeService.class, new ProviderPubSub());