Java Code Examples for com.google.cloud.pubsub.v1.AckReplyConsumer#ack()

The following examples show how to use com.google.cloud.pubsub.v1.AckReplyConsumer#ack() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PubsubIntegrationTest.java    From gcp-ingestion with Mozilla Public License 2.0 7 votes vote down vote up
private List<String> receiveLines(int expectedMessageCount) throws Exception {
  List<String> received = new CopyOnWriteArrayList<>();
  ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId,
      subscriptionId);

  MessageReceiver receiver = ((PubsubMessage message, AckReplyConsumer consumer) -> {
    try {
      String encoded = Json.asString(new org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage(
          message.getData().toByteArray(), message.getAttributesMap()));
      received.add(encoded);
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
    consumer.ack();
  });
  Subscriber subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
  subscriber.startAsync();
  while (received.size() < expectedMessageCount) {
    Thread.sleep(100);
  }
  subscriber.stopAsync();

  return received;
}
 
Example 2
Source File: NotificationReceiver.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
  NotificationMessage.Builder notificationMessageBuilder = NotificationMessage.newBuilder();

  try {
    String jsonString = message.getData().toStringUtf8();
    JsonFormat.parser().merge(jsonString, notificationMessageBuilder);

    NotificationMessage notificationMessage = notificationMessageBuilder.build();
    System.out.println(
        String.format("Config id: %s", notificationMessage.getNotificationConfigName()));
    System.out.println(String.format("Finding: %s", notificationMessage.getFinding()));
  } catch (InvalidProtocolBufferException e) {
    System.out.println("Could not parse message: " + e);
  } finally {
    consumer.ack();
  }
}
 
Example 3
Source File: ExportMessageReceiver.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 5 votes vote down vote up
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
  try {
    MonitoringService.addEvent(Event.REQUEST);
    dicomSender.send(message);
    consumer.ack();
  } catch (Exception e) {
    MonitoringService.addEvent(Event.ERROR);
    e.printStackTrace();
    consumer.nack();
  }
}
 
Example 4
Source File: CPSSubscriberTask.java    From pubsub with Apache License 2.0 5 votes vote down vote up
@Override
public void receiveMessage(final PubsubMessage message, final AckReplyConsumer consumer) {
  this.metricsHandler.add(
      LoadtestProto.MessageIdentifier.newBuilder()
          .setPublisherClientId(Integer.parseInt(message.getAttributesMap().get("clientId")))
          .setSequenceNumber(Integer.parseInt(message.getAttributesMap().get("sequenceNumber")))
          .build(),
      Duration.ofMillis(
          System.currentTimeMillis()
              - Long.parseLong(message.getAttributesMap().get("sendTime"))));
  consumer.ack();
}
 
Example 5
Source File: Subscriptions.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
  // Every time a Pub/Sub message comes in, print it and count it
  System.out.println("Message " + messageCount + ": " + message.getData().toStringUtf8());
  messageCount += 1;
  // Acknowledge the message
  consumer.ack();
}
 
Example 6
Source File: RiskAnalysisLDiversity.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 7
Source File: InspectDatastoreEntity.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 8
Source File: InspectGcsFile.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 9
Source File: RiskAnalysisCategoricalStats.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 10
Source File: InspectBigQueryTableWithSampling.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 11
Source File: InspectGcsFileWithSampling.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 12
Source File: RiskAnalysisKMap.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 13
Source File: RiskAnalysisKAnonymity.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 14
Source File: RiskAnalysisNumericalStats.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 15
Source File: InspectBigQueryTable.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static void handleMessage(
    DlpJob job,
    SettableApiFuture<Boolean> done,
    PubsubMessage pubsubMessage,
    AckReplyConsumer ackReplyConsumer) {
  String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName");
  if (job.getName().equals(messageAttribute)) {
    done.set(true);
    ackReplyConsumer.ack();
  } else {
    ackReplyConsumer.nack();
  }
}
 
Example 16
Source File: PubsubBenchWrapperImpl.java    From google-cloud-java with Apache License 2.0 4 votes vote down vote up
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
  consumer.ack();
}
 
Example 17
Source File: PubSubClient.java    From daq with Apache License 2.0 4 votes vote down vote up
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
  messages.offer(message);
  consumer.ack();
}