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();
}
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) {
}
}
}
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");
}
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);
}
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);
}
}
}
Aggregations