|
| 1 | +/* |
| 2 | + * Copyright 2017 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package pubsub; |
| 18 | + |
| 19 | +import com.google.api.gax.core.CredentialsProvider; |
| 20 | +import com.google.api.gax.core.NoCredentialsProvider; |
| 21 | +import com.google.api.gax.grpc.GrpcTransportChannel; |
| 22 | +import com.google.api.gax.rpc.FixedTransportChannelProvider; |
| 23 | +import com.google.api.gax.rpc.TransportChannelProvider; |
| 24 | +import com.google.cloud.pubsub.v1.Publisher; |
| 25 | +import com.google.cloud.pubsub.v1.TopicAdminClient; |
| 26 | +import com.google.cloud.pubsub.v1.TopicAdminSettings; |
| 27 | +import com.google.pubsub.v1.TopicName; |
| 28 | +import io.grpc.ManagedChannel; |
| 29 | +import io.grpc.ManagedChannelBuilder; |
| 30 | +import java.io.IOException; |
| 31 | + |
| 32 | +/** |
| 33 | + * Snippet that demonstrates creating Pub/Sub clients using the Google Cloud Pub/Sub emulator. |
| 34 | + * |
| 35 | + * <p>Note: clients cannot start/stop the emulator. |
| 36 | + */ |
| 37 | +public class UsePubSubEmulatorExample { |
| 38 | + |
| 39 | + public static void main(String... args) throws IOException { |
| 40 | + // [START pubsub_use_emulator] |
| 41 | + String hostport = System.getenv("PUBSUB_EMULATOR_HOST"); |
| 42 | + ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build(); |
| 43 | + try { |
| 44 | + TransportChannelProvider channelProvider = |
| 45 | + FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel)); |
| 46 | + CredentialsProvider credentialsProvider = NoCredentialsProvider.create(); |
| 47 | + |
| 48 | + // Set the channel and credentials provider when creating a `TopicAdminClient`. |
| 49 | + // Similarly for SubscriptionAdminClient |
| 50 | + TopicAdminClient topicClient = |
| 51 | + TopicAdminClient.create( |
| 52 | + TopicAdminSettings.newBuilder() |
| 53 | + .setTransportChannelProvider(channelProvider) |
| 54 | + .setCredentialsProvider(credentialsProvider) |
| 55 | + .build()); |
| 56 | + |
| 57 | + TopicName topicName = TopicName.of("my-project-id", "my-topic-id"); |
| 58 | + // Set the channel and credentials provider when creating a `Publisher`. |
| 59 | + // Similarly for Subscriber |
| 60 | + Publisher publisher = |
| 61 | + Publisher.newBuilder(topicName) |
| 62 | + .setChannelProvider(channelProvider) |
| 63 | + .setCredentialsProvider(credentialsProvider) |
| 64 | + .build(); |
| 65 | + } finally { |
| 66 | + channel.shutdown(); |
| 67 | + } |
| 68 | + // [END pubsub_use_emulator] |
| 69 | + } |
| 70 | +} |
0 commit comments