Search in sources :

Example 11 with Publisher

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);
}
Also used : Item(io.divolte.server.processing.Item) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) DivolteSchema(io.divolte.server.DivolteSchema) Test(org.junit.Test)

Example 12 with 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());
    }
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) ByteString(com.google.protobuf.ByteString) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 13 with Publisher

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();
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ApiException(com.google.api.gax.rpc.ApiException)

Example 14 with Publisher

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;
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Publisher(com.google.cloud.pubsub.v1.Publisher) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) TopicName(com.google.pubsub.v1.TopicName)

Example 15 with 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();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Subscriber(com.google.cloud.pubsub.v1.Subscriber) ExecutorProvider(com.google.api.gax.core.ExecutorProvider)

Aggregations

Publisher (com.google.cloud.pubsub.v1.Publisher)14 PubsubMessage (com.google.pubsub.v1.PubsubMessage)14 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)9 Test (org.junit.Test)7 ByteString (com.google.protobuf.ByteString)5 TopicName (com.google.pubsub.v1.TopicName)4 IOException (java.io.IOException)4 ApiException (com.google.api.gax.rpc.ApiException)3 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)2 DivolteSchema (io.divolte.server.DivolteSchema)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 ServletException (javax.servlet.ServletException)2 Duration (org.threeten.bp.Duration)2 ApiFuture (com.google.api.core.ApiFuture)1 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1 FlowControlSettings (com.google.api.gax.batching.FlowControlSettings)1 LimitExceededBehavior (com.google.api.gax.batching.FlowController.LimitExceededBehavior)1 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1