Search in sources :

Example 31 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project flink by apache.

the class CheckPubSubEmulatorTest method testPull.

@Test
public void testPull() throws Exception {
    Publisher publisher = pubsubHelper.createPublisher(PROJECT_NAME, TOPIC_NAME);
    publisher.publish(PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("Hello World PULL")).build()).get();
    List<ReceivedMessage> receivedMessages = pubsubHelper.pullMessages(PROJECT_NAME, SUBSCRIPTION_NAME, 1);
    assertEquals(1, receivedMessages.size());
    assertEquals("Hello World PULL", receivedMessages.get(0).getMessage().getData().toStringUtf8());
    publisher.shutdown();
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) ReceivedMessage(com.google.pubsub.v1.ReceivedMessage) Test(org.junit.Test)

Example 32 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project flink by apache.

the class PubSubPublisher method publish.

/**
 * Publish messages with as payload a single integer. The integers inside the messages start
 * from 0 and increase by one for each message send.
 *
 * @param amountOfMessages amount of messages to send
 */
void publish(int amountOfMessages) {
    Publisher publisher = null;
    try {
        publisher = Publisher.newBuilder(TopicName.of(projectName, topicName)).build();
        for (int i = 0; i < amountOfMessages; i++) {
            ByteString messageData = ByteString.copyFrom(BigInteger.valueOf(i).toByteArray());
            PubsubMessage message = PubsubMessage.newBuilder().setData(messageData).build();
            publisher.publish(message).get();
            System.out.println("Published message: " + i);
            Thread.sleep(100L);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (publisher != null) {
                publisher.shutdown();
            }
        } catch (Exception e) {
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 33 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project spring-cloud-gcp by spring-cloud.

the class DefaultPublisherFactoryTests method testGetPublisher.

@Test
public void testGetPublisher() {
    DefaultPublisherFactory factory = new DefaultPublisherFactory(() -> "projectId");
    factory.setCredentialsProvider(this.credentialsProvider);
    Publisher publisher = factory.createPublisher("testTopic");
    assertThat(factory.getCache().size()).isEqualTo(1);
    assertThat(publisher).isEqualTo(factory.getCache().get("testTopic"));
    assertThat(((ProjectTopicName) publisher.getTopicName()).getTopic()).isEqualTo("testTopic");
    assertThat(((ProjectTopicName) publisher.getTopicName()).getProject()).isEqualTo("projectId");
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Test(org.junit.Test)

Example 34 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project thingsboard by thingsboard.

the class TbPubSubProducerTemplate method send.

@Override
public void send(TopicPartitionInfo tpi, T msg, TbQueueCallback callback) {
    PubsubMessage.Builder pubsubMessageBuilder = PubsubMessage.newBuilder();
    pubsubMessageBuilder.setData(getMsg(msg));
    Publisher publisher = getOrCreatePublisher(tpi.getFullTopicName());
    ApiFuture<String> future = publisher.publish(pubsubMessageBuilder.build());
    ApiFutures.addCallback(future, new ApiFutureCallback<String>() {

        public void onSuccess(String messageId) {
            if (callback != null) {
                callback.onSuccess(null);
            }
        }

        public void onFailure(Throwable t) {
            if (callback != null) {
                callback.onFailure(t);
            }
        }
    }, pubExecutor);
}
Also used : Publisher(com.google.cloud.pubsub.v1.Publisher) ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 35 with Publisher

use of com.google.cloud.pubsub.v1.Publisher in project thingsboard by thingsboard.

the class TbPubSubProducerTemplate method getOrCreatePublisher.

private Publisher getOrCreatePublisher(String topic) {
    if (publisherMap.containsKey(topic)) {
        return publisherMap.get(topic);
    } else {
        try {
            admin.createTopicIfNotExists(topic);
            ProjectTopicName topicName = ProjectTopicName.of(pubSubSettings.getProjectId(), topic);
            Publisher publisher = Publisher.newBuilder(topicName).setCredentialsProvider(pubSubSettings.getCredentialsProvider()).build();
            publisherMap.put(topic, publisher);
            return publisher;
        } catch (IOException e) {
            log.error("Failed to create Publisher for the topic [{}].", topic, e);
            throw new RuntimeException("Failed to create Publisher for the topic.", e);
        }
    }
}
Also used : ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Publisher(com.google.cloud.pubsub.v1.Publisher) IOException(java.io.IOException)

Aggregations

Publisher (com.google.cloud.pubsub.v1.Publisher)25 PubsubMessage (com.google.pubsub.v1.PubsubMessage)18 ByteString (com.google.protobuf.ByteString)11 Test (org.junit.Test)11 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)9 IOException (java.io.IOException)7 TopicName (com.google.pubsub.v1.TopicName)6 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)4 ArrayList (java.util.ArrayList)4 ApiException (com.google.api.gax.rpc.ApiException)3 Subscriber (com.google.cloud.pubsub.v1.Subscriber)3 ExecutionException (java.util.concurrent.ExecutionException)3 ServletException (javax.servlet.ServletException)3 ApiFuture (com.google.api.core.ApiFuture)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)2 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 FixedTransportChannelProvider (com.google.api.gax.rpc.FixedTransportChannelProvider)2 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)2 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)2 ReceivedMessage (com.google.pubsub.v1.ReceivedMessage)2