Java Code Examples for com.google.cloud.pubsub.v1.Publisher#publish()
The following examples show how to use
com.google.cloud.pubsub.v1.Publisher#publish() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Demo.java From kafka-pubsub-emulator with Apache License 2.0 | 6 votes |
private void publish(String topic) throws IOException, InterruptedException { System.out.println("Publishing messages to " + topic + "...Press Ctrl-C to exit"); Publisher publisher = Publisher.newBuilder(topic) .setCredentialsProvider(new NoCredentialsProvider()) .setChannelProvider(getChannelProvider()) .build(); int messageNo = 1; while (true) { String message = "Message #" + messageNo++; ApiFuture<String> publishFuture = publisher.publish(PubsubMessage.newBuilder() .setData(ByteString.copyFromUtf8(message)) .build()); ApiFutures.addCallback(publishFuture, new ApiFutureCallback<String>() { @Override public void onFailure(Throwable throwable) { System.err.println("Error publishing " + message); } @Override public void onSuccess(String messageId) { System.out.println("Published " + message + " with message-id " + messageId); } }); Thread.sleep(SLEEP_1S); } }
Example 2
Source File: PubSubPublish.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { Publisher publisher = this.publisher; try { String topicId = System.getenv("PUBSUB_TOPIC"); // create a publisher on the topic if (publisher == null) { publisher = Publisher.newBuilder( ProjectTopicName.of(ServiceOptions.getDefaultProjectId(), topicId)) .build(); } // construct a pubsub message from the payload final String payload = req.getParameter("payload"); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).build(); publisher.publish(pubsubMessage); // redirect to home page resp.sendRedirect("/"); } catch (Exception e) { resp.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } }
Example 3
Source File: PubsubTopics.java From gcp-ingestion with Mozilla Public License 2.0 | 5 votes |
/** * Publish a {@link PubsubMessage} to the topic indicated by {@code index}. */ public String publish(int index, PubsubMessage message) { Publisher publisher = publishers.get(index); ApiFuture<String> future = publisher.publish(message); publisher.publishAllOutstanding(); try { return future.get(); } catch (Exception e) { throw new RuntimeException(e); } }
Example 4
Source File: PubSubPublish.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { Publisher publisher = this.publisher; try { String topicId = System.getenv("PUBSUB_TOPIC"); // create a publisher on the topic if (publisher == null) { ProjectTopicName topicName = ProjectTopicName.newBuilder() .setProject(ServiceOptions.getDefaultProjectId()) .setTopic(topicId) .build(); publisher = Publisher.newBuilder(topicName).build(); } // construct a pubsub message from the payload final String payload = req.getParameter("payload"); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).build(); publisher.publish(pubsubMessage); // redirect to home page resp.sendRedirect("/"); } catch (Exception e) { resp.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } }
Example 5
Source File: PubSubPublish.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { Publisher publisher = this.publisher; // construct a pubsub message from the payload final String payload = req.getParameter("payload"); Message message = new Message(null); message.setData(payload); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)) .putAttributes("sourceLang", req.getParameter("sourceLang")) .putAttributes("targetLang", req.getParameter("targetLang")) .build(); String topicId = System.getenv("PUBSUB_TOPIC"); // create a publisher on the topic if (publisher == null) { this.publisher = publisher = Publisher.newBuilder( ProjectTopicName.newBuilder() .setProject(ServiceOptions.getDefaultProjectId()) .setTopic(topicId) .build()) .build(); } publisher.publish(pubsubMessage); // redirect to home page resp.sendRedirect("/"); }