本文整理汇总了Java中com.google.pubsub.v1.Topic类的典型用法代码示例。如果您正苦于以下问题:Java Topic类的具体用法?Java Topic怎么用?Java Topic使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Topic类属于com.google.pubsub.v1包,在下文中一共展示了Topic类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTopic
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
public Topic getTopic(String topicId) {
if (topicId == null) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Illegal Argument: topic Id is null");
}
String topId = (topicId.toLowerCase().startsWith("rapture")) ? topicId : topicId + "rapture";
Topic topic = topics.get(topId);
if (topic == null) {
try (TopicAdminClient topicAdminClient = topicAdminClientCreate()) {
TopicName topicName = TopicName.create(projectId, topId);
try {
topic = topicAdminClient.getTopic(topicName);
} catch (Exception e) {
if (topic == null) {
topic = topicAdminClient.createTopic(topicName);
topics.put(topId, topic);
}
}
} catch (Exception ioe) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Cannot create or get topic " + topicId, ioe);
}
}
return topic;
}
示例2: deleteTopic
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
public void deleteTopic(String topicId) {
if (topicId == null) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Illegal Argument: topic Id is null");
}
String topId = (topicId.toLowerCase().startsWith("rapture")) ? topicId : topicId + "rapture";
Topic topic = topics.get(topId);
if (topic != null) {
try (TopicAdminClient topicAdminClient = topicAdminClientCreate()) {
TopicName topicName = TopicName.create(projectId, topId);
topicAdminClient.deleteTopic(topicName);
} catch (Exception ioe) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Cannot delete topic " + topicId, ioe);
}
}
topics.remove(topicId);
}
示例3: listTopics
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
@Override
public List<TopicPath> listTopics(ProjectPath project) throws IOException {
ListTopicsRequest.Builder request =
ListTopicsRequest.newBuilder()
.setProject(project.getPath())
.setPageSize(LIST_BATCH_SIZE);
ListTopicsResponse response = publisherStub().listTopics(request.build());
if (response.getTopicsCount() == 0) {
return ImmutableList.of();
}
List<TopicPath> topics = new ArrayList<>(response.getTopicsCount());
while (true) {
for (Topic topic : response.getTopicsList()) {
topics.add(topicPathFromPath(topic.getName()));
}
if (response.getNextPageToken().isEmpty()) {
break;
}
request.setPageToken(response.getNextPageToken());
response = publisherStub().listTopics(request.build());
}
return topics;
}
示例4: createIotTopic
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
/** Creates a topic and grants the IoT service account access. */
public static Topic createIotTopic(String projectId, String topicId) throws Exception {
// Create a new topic
final TopicName topicName = TopicName.create(projectId, topicId);
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
final Topic topic = topicAdminClient.createTopic(topicName);
Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
// add role -> members binding
Binding binding =
Binding.newBuilder()
.addMembers("serviceAccount:[email protected]")
.setRole(Role.owner().toString())
.build();
// create updated policy
Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build();
topicAdminClient.setIamPolicy(topicName.toString(), updatedPolicy);
System.out.println("Setup topic / policy for: " + topic.getName());
return topic;
}
}
示例5: getTopic
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
/**
* Get the configuration of a Google Cloud Pub/Sub topic.
*
* @param topicName canonical topic name, e.g., "topicName"
* @return topic configuration or {@code null} if topic doesn't exist
*/
public Topic getTopic(String topicName) {
Assert.hasText(topicName, "No topic name was specified.");
try {
return this.topicAdminClient.getTopic(TopicName.create(this.projectId, topicName));
}
catch (ApiException aex) {
if (aex.getStatusCode().getCode() == StatusCode.Code.NOT_FOUND) {
return null;
}
throw aex;
}
}
示例6: listTopics
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
/**
* Return every topic in a project.
*
* <p>If there are multiple pages, they will all be merged into the same result.
*/
public List<Topic> listTopics() {
PagedResponseWrappers.ListTopicsPagedResponse topicListPage =
this.topicAdminClient.listTopics(ProjectName.create(this.projectId));
return Lists.newArrayList(topicListPage.iterateAll());
}
示例7: publishTask
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
@Override
public void publishTask(final String queue, final String task) {
Topic topic = getTopic(queue);
ByteString data = ByteString.copyFromUtf8(task);
TopicName topicName = topic.getNameAsTopicName();
try {
PubsubMessage psmessage = PubsubMessage.newBuilder().setData(data).build();
Publisher publisher = randomHouse.get(topicName);
if (publisher == null) {
logger.trace("No publisher found for " + topicName + " - creating");
Builder builder = Publisher.defaultBuilder(topicName);
// The default executor provider creates an insane number of threads.
if (executor != null) builder.setExecutorProvider(executor);
publisher = builder.build();
randomHouse.put(topicName, publisher);
} else {
logger.trace("Existing publisher found for " + topicName);
}
ApiFuture<String> messageIdFuture = publisher.publish(psmessage);
if (executor != null) messageIdFuture.addListener(listener, executor.getExecutor());
} catch (IOException e) {
String error = String.format("Cannot send message to topic %s:\n%s", topic.getName(), ExceptionToString.format(e));
logger.error(error);
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, error, e);
}
}
示例8: createTopic
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
@Override
public void createTopic(TopicPath topic) throws IOException {
Topic request = Topic.newBuilder()
.setName(topic.getPath())
.build();
publisherStub().createTopic(request); // ignore Topic result.
}
示例9: main
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Please specify your project name.");
System.exit(1);
}
final String project = args[0];
ManagedChannelImpl channelImpl = NettyChannelBuilder
.forAddress("pubsub.googleapis.com", 443)
.negotiationType(NegotiationType.TLS)
.build();
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
// Down-scope the credential to just the scopes required by the service
creds = creds.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
// Intercept the channel to bind the credential
ExecutorService executor = Executors.newSingleThreadExecutor();
ClientAuthInterceptor interceptor = new ClientAuthInterceptor(creds, executor);
Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
// Create a stub using the channel that has the bound credential
PublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
ListTopicsRequest request = ListTopicsRequest.newBuilder()
.setPageSize(10)
.setProject("projects/" + project)
.build();
ListTopicsResponse resp = publisherStub.listTopics(request);
System.out.println("Found " + resp.getTopicsCount() + " topics.");
for (Topic topic : resp.getTopicsList()) {
System.out.println(topic.getName());
}
}
示例10: createQueue
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
@Override
public void createQueue(String queueIdentifier) {
Topic topic = getTopic(queueIdentifier);
}
示例11: TestApp
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
public TestApp() {
String projectId = ServiceOptions.getDefaultProjectId();
try {
//export GRPC_PROXY_EXP=localhost:3128
HttpHost proxy = new HttpHost("127.0.0.1",3128);
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
httpClient.addRequestInterceptor(new HttpRequestInterceptor(){
@Override
public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException {
//if (request.getRequestLine().getMethod().equals("CONNECT"))
// request.addHeader(new BasicHeader("Proxy-Authorization","Basic dXNlcjE6dXNlcjE="));
}
});
mHttpTransport = new ApacheHttpTransport(httpClient);
HttpTransportFactory hf = new HttpTransportFactory(){
@Override
public HttpTransport create() {
return mHttpTransport;
}
};
credential = GoogleCredentials.getApplicationDefault(hf);
CredentialsProvider credentialsProvider = new GoogleCredentialsProvider(){
public List<String> getScopesToApply(){
return Arrays.asList("https://www.googleapis.com/auth/pubsub");
}
public Credentials getCredentials() {
return credential;
}
};
TopicAdminSettings topicAdminSettings =
TopicAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider)
.build();
TopicAdminClient topicAdminClient =
TopicAdminClient.create(topicAdminSettings);
//TopicAdminClient topicAdminClient = TopicAdminClient.create();
ProjectName project = ProjectName.create(projectId);
for (Topic element : topicAdminClient.listTopics(project).iterateAll())
System.out.println(element.getName());
} catch (Exception ex)
{
System.out.println("ERROR " + ex);
}
}
示例12: createTopic
import com.google.pubsub.v1.Topic; //导入依赖的package包/类
/**
* Create a new topic on Google Cloud Pub/Sub.
*
* @param topicName the name for the new topic
* @return the created topic
*/
public Topic createTopic(String topicName) {
Assert.hasText(topicName, "No topic name was specified.");
return this.topicAdminClient.createTopic(TopicName.create(this.projectId, topicName));
}