Search in sources :

Example 1 with CredentialsProvider

use of com.google.api.gax.core.CredentialsProvider in project google-cloud-java by GoogleCloudPlatform.

the class SubscriberSnippets method createSubscriberWithCustomCredentials.

private Subscriber createSubscriberWithCustomCredentials() throws Exception {
    // [START pubsub_subscriber_custom_credentials]
    CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("credentials.json")));
    ChannelProvider channelProvider = TopicAdminSettings.defaultChannelProviderBuilder().setCredentialsProvider(credentialsProvider).build();
    Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).setChannelProvider(channelProvider).build();
    // [START pubsub_subscriber_custom_credentials]
    return subscriber;
}
Also used : Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) ChannelProvider(com.google.api.gax.grpc.ChannelProvider) FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) FileInputStream(java.io.FileInputStream)

Example 2 with CredentialsProvider

use of com.google.api.gax.core.CredentialsProvider in project google-cloud-java by GoogleCloudPlatform.

the class PublisherSnippets method createPublisherWithCustomCredentials.

private Publisher createPublisherWithCustomCredentials(TopicName topicName) throws Exception {
    // [START pubsub_publisher_custom_credentials]
    // read service account credentials from file
    CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("credentials.json")));
    ChannelProvider channelProvider = TopicAdminSettings.defaultChannelProviderBuilder().setCredentialsProvider(credentialsProvider).build();
    Publisher publisher = Publisher.defaultBuilder(topicName).setChannelProvider(channelProvider).build();
    // [END pubsub_publisher_custom_credentials]
    return publisher;
}
Also used : ChannelProvider(com.google.api.gax.grpc.ChannelProvider) FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) FileInputStream(java.io.FileInputStream)

Example 3 with CredentialsProvider

use of com.google.api.gax.core.CredentialsProvider in project spring-cloud-gcp by spring-cloud.

the class GcpPubSubEmulatorConfigurationTests method testEmulatorConfig.

@Test
public void testEmulatorConfig() {
    this.contextRunner.run(context -> {
        CredentialsProvider credentialsProvider = context.getBean(CredentialsProvider.class);
        Assert.assertTrue("CredentialsProvider is not correct", credentialsProvider instanceof NoCredentialsProvider);
        TransportChannelProvider transportChannelProvider = context.getBean(TransportChannelProvider.class);
        Assert.assertTrue("TransportChannelProvider is not correct", transportChannelProvider instanceof FixedTransportChannelProvider);
    });
}
Also used : NoCredentialsProvider(com.google.api.gax.core.NoCredentialsProvider) FixedTransportChannelProvider(com.google.api.gax.rpc.FixedTransportChannelProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) NoCredentialsProvider(com.google.api.gax.core.NoCredentialsProvider) FixedTransportChannelProvider(com.google.api.gax.rpc.FixedTransportChannelProvider) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) Test(org.junit.Test)

Example 4 with CredentialsProvider

use of com.google.api.gax.core.CredentialsProvider in project google-cloud-java by GoogleCloudPlatform.

the class UsePubSubEmulatorSnippet method main.

public static void main(String... args) throws IOException {
    // [START pubsub_use_emulator]
    String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
    ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
    try {
        TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
        CredentialsProvider credentialsProvider = NoCredentialsProvider.create();
        // Set the channel and credentials provider when creating a `TopicAdminClient`.
        // Similarly for SubscriptionAdminClient
        TopicAdminClient topicClient = TopicAdminClient.create(TopicAdminSettings.newBuilder().setTransportChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider).build());
        TopicName topicName = TopicName.of("my-project-id", "my-topic-id");
        // Set the channel and credentials provider when creating a `Publisher`.
        // Similarly for Subscriber
        Publisher publisher = Publisher.newBuilder(topicName).setChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider).build();
    } finally {
        channel.shutdown();
    }
// [END pubsub_use_emulator]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) ManagedChannel(io.grpc.ManagedChannel) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) NoCredentialsProvider(com.google.api.gax.core.NoCredentialsProvider) Publisher(com.google.cloud.pubsub.v1.Publisher) FixedTransportChannelProvider(com.google.api.gax.rpc.FixedTransportChannelProvider) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) TopicName(com.google.pubsub.v1.TopicName)

Aggregations

CredentialsProvider (com.google.api.gax.core.CredentialsProvider)4 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)2 NoCredentialsProvider (com.google.api.gax.core.NoCredentialsProvider)2 ChannelProvider (com.google.api.gax.grpc.ChannelProvider)2 FixedTransportChannelProvider (com.google.api.gax.rpc.FixedTransportChannelProvider)2 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)2 FileInputStream (java.io.FileInputStream)2 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)1 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)1 Publisher (com.google.cloud.pubsub.v1.Publisher)1 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)1 TopicName (com.google.pubsub.v1.TopicName)1 ManagedChannel (io.grpc.ManagedChannel)1 Test (org.junit.Test)1