当前位置: 首页>>代码示例>>Java>>正文


Java Topic类代码示例

本文整理汇总了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;
}
 
开发者ID:RapturePlatform,项目名称:Rapture,代码行数:24,代码来源:PubsubPipeline2Handler.java

示例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);
}
 
开发者ID:RapturePlatform,项目名称:Rapture,代码行数:17,代码来源:PubsubPipeline2Handler.java

示例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;
}
 
开发者ID:apache,项目名称:beam,代码行数:24,代码来源:PubsubGrpcClient.java

示例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;
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:23,代码来源:DeviceRegistryExample.java

示例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;
	}
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-gcp,代码行数:21,代码来源:PubSubAdmin.java

示例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());
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-gcp,代码行数:12,代码来源:PubSubAdmin.java

示例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);
    }
}
 
开发者ID:RapturePlatform,项目名称:Rapture,代码行数:31,代码来源:PubsubPipeline2Handler.java

示例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.
}
 
开发者ID:apache,项目名称:beam,代码行数:8,代码来源:PubsubGrpcClient.java

示例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());
        }
    }
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-pubsub-samples-java,代码行数:31,代码来源:Main.java

示例10: createQueue

import com.google.pubsub.v1.Topic; //导入依赖的package包/类
@Override
public void createQueue(String queueIdentifier) {
    Topic topic = getTopic(queueIdentifier);
}
 
开发者ID:RapturePlatform,项目名称:Rapture,代码行数:5,代码来源:PubsubPipeline2Handler.java

示例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);
	}
  }
 
开发者ID:salrashid123,项目名称:gcpsamples,代码行数:57,代码来源:TestApp.java

示例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));
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-gcp,代码行数:12,代码来源:PubSubAdmin.java


注:本文中的com.google.pubsub.v1.Topic类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。