use of com.google.pubsub.v1.PubsubMessage in project beam by apache.
the class TestPubsub method publish.
/**
* Publish messages to {@link #topicPath()}.
*/
public void publish(List<PubsubMessage> messages) {
Preconditions.checkNotNull(eventsTopicPath);
Publisher eventPublisher;
try {
eventPublisher = Publisher.newBuilder(eventsTopicPath.getPath()).setCredentialsProvider(pipelineOptions::getGcpCredential).setChannelProvider(channelProvider).setEndpoint(pubsubEndpoint).build();
} catch (IOException e) {
throw new RuntimeException("Error creating event publisher", e);
}
List<ApiFuture<String>> futures = messages.stream().map((message) -> {
com.google.pubsub.v1.PubsubMessage.Builder builder = com.google.pubsub.v1.PubsubMessage.newBuilder().setData(ByteString.copyFrom(message.getPayload())).putAllAttributes(message.getAttributeMap());
return eventPublisher.publish(builder.build());
}).collect(Collectors.toList());
try {
ApiFutures.allAsList(futures).get();
} catch (ExecutionException e) {
throw new RuntimeException("Error publishing a test message", e);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for messages to publish", e);
}
eventPublisher.shutdown();
}
use of com.google.pubsub.v1.PubsubMessage in project beam by apache.
the class PubsubTestClientTest method testPullThenPublish.
@Test
public void testPullThenPublish() throws IOException {
AtomicLong now = new AtomicLong();
Clock clock = now::get;
PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(DATA)).build();
IncomingMessage expectedIncomingMessage = IncomingMessage.of(message, MESSAGE_TIME, REQ_TIME, ACK_ID, MESSAGE_ID);
OutgoingMessage expectedOutgoingMessage = OutgoingMessage.of(message, MESSAGE_TIME, MESSAGE_ID);
try (PubsubTestClientFactory factory = PubsubTestClient.createFactoryForPullAndPublish(SUBSCRIPTION, TOPIC, clock, ACK_TIMEOUT_S, ImmutableList.of(expectedIncomingMessage), ImmutableList.of(expectedOutgoingMessage), ImmutableList.of())) {
try (PubsubTestClient client = (PubsubTestClient) factory.newClient(null, null, null)) {
// Pull
now.set(REQ_TIME);
client.advance();
List<IncomingMessage> actualIncomingMessages = client.pull(now.get(), SUBSCRIPTION, 1, true);
now.addAndGet(ACK_TIMEOUT_S - 10);
client.advance();
client.acknowledge(SUBSCRIPTION, ImmutableList.of(ACK_ID));
assertEquals(1, actualIncomingMessages.size());
assertEquals(expectedIncomingMessage, actualIncomingMessages.get(0));
// Publish
IncomingMessage incomingMessage = actualIncomingMessages.get(0);
OutgoingMessage actualOutgoingMessage = OutgoingMessage.of(incomingMessage.message(), incomingMessage.timestampMsSinceEpoch(), incomingMessage.recordId());
client.publish(TOPIC, ImmutableList.of(actualOutgoingMessage));
}
}
}
use of com.google.pubsub.v1.PubsubMessage in project curiostack by curioswitch.
the class Publisher method publish.
public ListenableFuture<String> publish(PubsubMessage message) {
Span span = tracer.currentSpan();
if (span != null) {
PubsubMessage.Builder messageBuilder = message.toBuilder();
traceInjector.inject(span.context(), messageBuilder);
message = messageBuilder.build();
}
PublishRequest request = PublishRequest.newBuilder().setTopic(options.getTopic()).addMessages(message).build();
return Futures.transform(stub.publish(request), (response) -> {
if (response.getMessageIdsCount() != 1) {
throw new IllegalStateException(String.format("The publish result count %s does not match " + "the expected 1 result. Please contact Cloud Pub/Sub support " + "if this frequently occurs", response.getMessageIdsCount()));
}
return response.getMessageIds(0);
}, MoreExecutors.directExecutor());
}
use of com.google.pubsub.v1.PubsubMessage in project beam by apache.
the class PubsubGrpcClientTest method publishOneMessage.
@Test
public void publishOneMessage() throws IOException {
initializeClient(TIMESTAMP_ATTRIBUTE, ID_ATTRIBUTE);
String expectedTopic = TOPIC.getPath();
PubsubMessage expectedPubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFrom(DATA.getBytes(StandardCharsets.UTF_8))).putAllAttributes(ATTRIBUTES).putAllAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME_MS), ID_ATTRIBUTE, RECORD_ID)).build();
final PublishRequest expectedRequest = PublishRequest.newBuilder().setTopic(expectedTopic).addAllMessages(ImmutableList.of(expectedPubsubMessage)).build();
final PublishResponse response = PublishResponse.newBuilder().addAllMessageIds(ImmutableList.of(MESSAGE_ID)).build();
final List<PublishRequest> requestsReceived = new ArrayList<>();
PublisherImplBase publisherImplBase = new PublisherImplBase() {
@Override
public void publish(PublishRequest request, StreamObserver<PublishResponse> responseObserver) {
requestsReceived.add(request);
responseObserver.onNext(response);
responseObserver.onCompleted();
}
};
Server server = InProcessServerBuilder.forName(channelName).addService(publisherImplBase).build().start();
try {
OutgoingMessage actualMessage = OutgoingMessage.of(com.google.pubsub.v1.PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(DATA)).putAllAttributes(ATTRIBUTES).build(), MESSAGE_TIME_MS, RECORD_ID);
int n = client.publish(TOPIC, ImmutableList.of(actualMessage));
assertEquals(1, n);
assertEquals(expectedRequest, Iterables.getOnlyElement(requestsReceived));
} finally {
server.shutdownNow();
}
}
use of com.google.pubsub.v1.PubsubMessage 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) {
}
}
}
Aggregations