/** * Creates the given topic if it is not yet created. */ private void createTopicIfNotExists(TopicId topicId) throws IOException { try { createTopic(new TopicMetadata(topicId)); LOG.info("System topic created: {}", topicId); } catch (TopicAlreadyExistsException e) { // OK for the topic already created. Just log a debug as it happens on every restart. LOG.debug("System topic already exists: {}", topicId); } }
/** * Creates the given topic if it is not yet created. */ private void createTopicIfNotExists(TopicId topicId) throws IOException { try { createTopic(new TopicMetadata(topicId)); LOG.info("System topic created: {}", topicId); } catch (TopicAlreadyExistsException e) { // OK for the topic already created. Just log a debug as it happens on every restart. LOG.debug("System topic already exists: {}", topicId); } }
@Override public void createTopic(String topic, Map<String, String> properties) throws TopicAlreadyExistsException, IOException { messagingService.createTopic(new TopicMetadata(namespace.topic(topic), properties)); }
@Override public void createTopic(String topic, Map<String, String> properties) throws TopicAlreadyExistsException, IOException { messagingService.createTopic(new TopicMetadata(namespace.topic(topic), properties)); }
@Override public void updateTopic(String topic, Map<String, String> properties) throws TopicNotFoundException, IOException { messagingService.updateTopic(new TopicMetadata(namespace.topic(topic), properties)); }
@Override public void updateTopic(String topic, Map<String, String> properties) throws TopicNotFoundException, IOException { messagingService.updateTopic(new TopicMetadata(namespace.topic(topic), properties)); }
@Override protected TopicMetadata computeNext() { if (closed || (!iterator.hasNext())) { return endOfData(); } Map.Entry<byte[], byte[]> entry = iterator.next(); TopicId topicId = MessagingUtils.toTopicId(entry.getKey()); Map<String, String> properties = GSON.fromJson(Bytes.toString(entry.getValue()), MAP_TYPE); return new TopicMetadata(topicId, properties); }
@PUT @Path("/topics/{topic}") public void createTopic(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception { TopicId topicId = new NamespaceId(namespace).topic(topic); messagingService.createTopic(new TopicMetadata(topicId, decodeTopicProperties(request.content()))); responder.sendStatus(HttpResponseStatus.OK); }
@PUT @Path("/topics/{topic}") public void createTopic(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception { TopicId topicId = new NamespaceId(namespace).topic(topic); messagingService.createTopic(new TopicMetadata(topicId, decodeTopicProperties(request.content()))); responder.sendStatus(HttpResponseStatus.OK); }
@PUT @Path("/topics/{topic}/properties") public void updateTopic(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception { TopicId topicId = new NamespaceId(namespace).topic(topic); messagingService.updateTopic(new TopicMetadata(topicId, decodeTopicProperties(request.content()))); responder.sendStatus(HttpResponseStatus.OK); }
@Override protected TopicMetadata computeNext() { if (closed || (!iterator.hasNext())) { return endOfData(); } Map.Entry<byte[], byte[]> entry = iterator.next(); TopicId topicId = MessagingUtils.toTopicId(entry.getKey()); Map<String, String> properties = GSON.fromJson(Bytes.toString(entry.getValue()), MAP_TYPE); return new TopicMetadata(topicId, properties); }
@PUT @Path("/topics/{topic}/properties") public void updateTopic(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception { TopicId topicId = new NamespaceId(namespace).topic(topic); messagingService.updateTopic(new TopicMetadata(topicId, decodeTopicProperties(request.content()))); responder.sendStatus(HttpResponseStatus.OK); }
@Override public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException { try (MetadataTable metadataTable = createMetadataTable()) { Map<String, String> properties = createDefaultProperties(); properties.putAll(topicMetadata.getProperties()); metadataTable.updateTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true)); topicCache.invalidate(topicMetadata.getTopicId()); } }
@Override public void updateTopic(TopicMetadata topicMetadata) throws TopicNotFoundException, IOException { try (MetadataTable metadataTable = createMetadataTable()) { Map<String, String> properties = createDefaultProperties(); properties.putAll(topicMetadata.getProperties()); metadataTable.updateTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true)); topicCache.invalidate(topicMetadata.getTopicId()); } }
@Override public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException { try (MetadataTable metadataTable = createMetadataTable()) { Map<String, String> properties = createDefaultProperties(); properties.putAll(topicMetadata.getProperties()); metadataTable.createTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true)); } }
@Override public void createTopic(TopicMetadata topicMetadata) throws TopicAlreadyExistsException, IOException { try (MetadataTable metadataTable = createMetadataTable()) { Map<String, String> properties = createDefaultProperties(); properties.putAll(topicMetadata.getProperties()); metadataTable.createTopic(new TopicMetadata(topicMetadata.getTopicId(), properties, true)); } }
@Override public TopicMetadata getMetadata(TopicId topicId) throws IOException, TopicNotFoundException { try { byte[] value = levelDB.get(MessagingUtils.toMetadataRowKey(topicId)); if (value == null) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } Map<String, String> properties = GSON.fromJson(Bytes.toString(value), MAP_TYPE); TopicMetadata topicMetadata = new TopicMetadata(topicId, properties); if (!topicMetadata.exists()) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } return topicMetadata; } catch (DBException e) { // DBException is a RuntimeException. Turn it to IOException so that it forces caller to handle it. throw new IOException(e); } }
@Override public TopicMetadata getTopic(TopicId topicId) throws TopicNotFoundException, IOException { HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createTopicPath(topicId)).build(); HttpResponse response = remoteClient.execute(request); if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } handleError(response, "Failed to update topic " + topicId); Map<String, String> properties = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_PROPERTY_TYPE); return new TopicMetadata(topicId, properties); }
@Override public TopicMetadata getTopic(TopicId topicId) throws TopicNotFoundException, IOException { HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createTopicPath(topicId)).build(); HttpResponse response = remoteClient.execute(request); if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new TopicNotFoundException(topicId.getNamespace(), topicId.getTopic()); } handleError(response, "Failed to update topic " + topicId); Map<String, String> properties = GSON.fromJson(response.getResponseBodyAsString(), TOPIC_PROPERTY_TYPE); return new TopicMetadata(topicId, properties); }
@Test public void testGeMetadata() throws Exception { TopicId topicId = new NamespaceId("ns2").topic("d"); TopicMetadata metadata = new TopicMetadata(topicId, "ttl", "100"); for (int i = 1; i <= 5; i++) { client.createTopic(metadata); TopicMetadata topicMetadata = client.getTopic(topicId); Assert.assertEquals(100, topicMetadata.getTTL()); Assert.assertEquals(i, topicMetadata.getGeneration()); client.deleteTopic(topicId); } }