use of com.google.cloud.pubsub.v1.Publisher in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusherTest method testMessageBatchSentToPublisher.
@Test
public void testMessageBatchSentToPublisher() {
final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
// Process a bunch of messages.
final DivolteSchema schema = new DivolteSchema(MINIMAL_SCHEMA, Optional.empty());
final GoogleCloudPubSubFlusher flusher = new GoogleCloudPubSubFlusher(publisher, schema);
final Queue<Item<AvroRecordBuffer>> items = Stream.generate(this::generateMessage).limit(10).map(this::itemFromAvroRecordBuffer).collect(Collectors.toCollection(() -> new ArrayBlockingQueue<>(10)));
flusher.process(items);
// Check the messages were all forwarded to the publisher.
verify(publisher, times(10)).publish(any(PubsubMessage.class));
verifyNoMoreInteractions(publisher);
}
use of com.google.cloud.pubsub.v1.Publisher in project java-docs-samples by GoogleCloudPlatform.
the class PubSubPublish method doPost.
@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());
}
}
use of com.google.cloud.pubsub.v1.Publisher in project java-docs-samples by GoogleCloudPlatform.
the class PublisherExample method main.
/**
* Publish messages to a topic.
* @param args topic name, number of messages
*/
public static void main(String... args) throws Exception {
// topic id, eg. "my-topic"
String topicId = args[0];
int messageCount = Integer.parseInt(args[1]);
ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId);
Publisher publisher = null;
try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.newBuilder(topicName).build();
for (int i = 0; i < messageCount; i++) {
String message = "message-" + i;
// convert message to bytes
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
// schedule a message to be published, messages are automatically batched
ApiFuture<String> future = publisher.publish(pubsubMessage);
// add an asynchronous callback to handle success / failure
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
@Override
public void onFailure(Throwable throwable) {
if (throwable instanceof ApiException) {
ApiException apiException = ((ApiException) throwable);
// details on the API exception
System.out.println(apiException.getStatusCode().getCode());
System.out.println(apiException.isRetryable());
}
System.out.println("Error publishing message : " + message);
}
@Override
public void onSuccess(String messageId) {
// Once published, returns server-assigned message ids (unique within the topic)
System.out.println(messageId);
}
});
}
} finally {
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
}
}
}
use of com.google.cloud.pubsub.v1.Publisher in project moleculer-java by moleculer-java.
the class GoogleTransporter method getOrCreatePublisher.
protected Publisher getOrCreatePublisher(String channel) throws Exception {
Publisher publisher;
synchronized (publishers) {
publisher = publishers.get(channel);
if (publisher != null) {
return publisher;
}
TopicName topicName = TopicName.of(projectID, channel);
Publisher.Builder builder = Publisher.newBuilder(topicName);
if (batchingSettings != null) {
builder.setBatchingSettings(batchingSettings);
}
if (channelProvider != null) {
builder.setChannelProvider(channelProvider);
}
if (credentialsProvider != null) {
builder.setCredentialsProvider(credentialsProvider);
}
if (executorProvider != null) {
builder.setExecutorProvider(executorProvider);
} else {
builder.setExecutorProvider(new ExecutorProvider() {
@Override
public final boolean shouldAutoClose() {
return false;
}
@Override
public final ScheduledExecutorService getExecutor() {
return scheduler;
}
});
}
if (headerProvider != null) {
builder.setHeaderProvider(headerProvider);
}
if (retrySettings != null) {
builder.setRetrySettings(retrySettings);
}
publisher = builder.build();
publishers.put(channel, publisher);
}
return publisher;
}
use of com.google.cloud.pubsub.v1.Publisher in project moleculer-java by moleculer-java.
the class GoogleTransporter method subscribe.
// --- SUBSCRIBE ---
@Override
public Promise subscribe(String channel) {
try {
// Create publisher
getOrCreatePublisher(channel);
// Create subscriber
synchronized (subscribers) {
if (!subscribers.containsKey(channel)) {
Subscriber.Builder builder = Subscriber.newBuilder(SubscriptionName.of(projectID, channel), (message, consumer) -> {
// Message received
byte[] bytes = message.getData().toByteArray();
received(channel, bytes);
consumer.ack();
});
builder.setChannelProvider(channelProvider);
builder.setCredentialsProvider(credentialsProvider);
if (executorProvider != null) {
builder.setExecutorProvider(executorProvider);
} else {
builder.setExecutorProvider(new ExecutorProvider() {
@Override
public final boolean shouldAutoClose() {
return false;
}
@Override
public final ScheduledExecutorService getExecutor() {
return scheduler;
}
});
}
if (headerProvider != null) {
builder.setHeaderProvider(headerProvider);
}
if (maxAckExtensionPeriod != null) {
builder.setMaxAckExtensionPeriod(maxAckExtensionPeriod);
}
if (parallelPullCount > 0) {
builder.setParallelPullCount(parallelPullCount);
}
if (executorProvider != null) {
builder.setSystemExecutorProvider(executorProvider);
} else {
builder.setSystemExecutorProvider(new ExecutorProvider() {
@Override
public final boolean shouldAutoClose() {
return false;
}
@Override
public final ScheduledExecutorService getExecutor() {
return scheduler;
}
});
}
Subscriber subscriber = builder.build();
subscriber.startAsync();
subscribers.put(channel, subscriber);
}
}
} catch (Exception cause) {
return Promise.reject(cause);
}
return Promise.resolve();
}
Aggregations