Search in sources :

Example 1 with ExecutorProvider

use of com.google.api.gax.core.ExecutorProvider 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 2 with ExecutorProvider

use of com.google.api.gax.core.ExecutorProvider 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

ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Publisher (com.google.cloud.pubsub.v1.Publisher)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 TopicName (com.google.pubsub.v1.TopicName)1