@Override public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { // handle incoming message, then ack/nack the received message System.out.println("Id : " + message.getMessageId()); System.out.println("Data : " + message.getData().toStringUtf8()); consumer.ack(); } };
/** * * * <pre> * ID of this message, assigned by the server when the message is published. * Guaranteed to be unique within the topic. This value may be read by a * subscriber that receives a `PubsubMessage` via a `Pull` call or a push * delivery. It must not be populated by the publisher in a `Publish` call. * </pre> * * <code>string message_id = 3;</code> */ public Builder clearMessageId() { messageId_ = getDefaultInstance().getMessageId(); onChanged(); return this; } /**
@java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.google.pubsub.v1.PubsubMessage)) { return super.equals(obj); } com.google.pubsub.v1.PubsubMessage other = (com.google.pubsub.v1.PubsubMessage) obj; boolean result = true; result = result && getData().equals(other.getData()); result = result && internalGetAttributes().equals(other.internalGetAttributes()); result = result && getMessageId().equals(other.getMessageId()); result = result && (hasPublishTime() == other.hasPublishTime()); if (hasPublishTime()) { result = result && getPublishTime().equals(other.getPublishTime()); } result = result && unknownFields.equals(other.unknownFields); return result; }
@java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptor().hashCode(); hash = (37 * hash) + DATA_FIELD_NUMBER; hash = (53 * hash) + getData().hashCode(); if (!internalGetAttributes().getMap().isEmpty()) { hash = (37 * hash) + ATTRIBUTES_FIELD_NUMBER; hash = (53 * hash) + internalGetAttributes().hashCode(); } hash = (37 * hash) + MESSAGE_ID_FIELD_NUMBER; hash = (53 * hash) + getMessageId().hashCode(); if (hasPublishTime()) { hash = (37 * hash) + PUBLISH_TIME_FIELD_NUMBER; hash = (53 * hash) + getPublishTime().hashCode(); } hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; }
attributes.put(MESSAGE_ID_ATTRIBUTE, message.getMessage().getMessageId()); attributes.put(MSG_ATTRIBUTES_COUNT_ATTRIBUTE, String.valueOf(message.getMessage().getAttributesCount())); attributes.put(MSG_PUBLISH_TIME_ATTRIBUTE, String.valueOf(message.getMessage().getPublishTime().getSeconds()));
public Builder mergeFrom(com.google.pubsub.v1.PubsubMessage other) { if (other == com.google.pubsub.v1.PubsubMessage.getDefaultInstance()) return this; if (other.getData() != com.google.protobuf.ByteString.EMPTY) { setData(other.getData()); } internalGetMutableAttributes().mergeFrom(other.internalGetAttributes()); if (!other.getMessageId().isEmpty()) { messageId_ = other.messageId_; onChanged(); } if (other.hasPublishTime()) { mergePublishTime(other.getPublishTime()); } this.mergeUnknownFields(other.unknownFields); onChanged(); return this; }
/** Receive messages over a subscription. */ public static void main(String... args) throws Exception { // set subscriber id, eg. my-sub String subscriptionId = args[0]; ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of( PROJECT_ID, subscriptionId); Subscriber subscriber = null; try { // create a subscriber bound to the asynchronous message receiver subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample()).build(); subscriber.startAsync().awaitRunning(); // Continue to listen to messages while (true) { PubsubMessage message = messages.take(); System.out.println("Message Id: " + message.getMessageId()); System.out.println("Data: " + message.getData().toStringUtf8()); } } finally { if (subscriber != null) { subscriber.stopAsync(); } } } }
/** * <code>optional string message_id = 3;</code> * * <pre> * ID of this message, assigned by the server when the message is published. * Guaranteed to be unique within the topic. This value may be read by a * subscriber that receives a `PubsubMessage` via a `Pull` call or a push * delivery. It must not be populated by the publisher in a `Publish` call. * </pre> */ public Builder clearMessageId() { messageId_ = getDefaultInstance().getMessageId(); onChanged(); return this; } /**
/** * <pre> * ID of this message, assigned by the server when the message is published. * Guaranteed to be unique within the topic. This value may be read by a * subscriber that receives a `PubsubMessage` via a `Pull` call or a push * delivery. It must not be populated by the publisher in a `Publish` call. * </pre> * * <code>optional string message_id = 3;</code> */ public Builder clearMessageId() { messageId_ = getDefaultInstance().getMessageId(); onChanged(); return this; } /**
/** * * * <pre> * ID of this message, assigned by the server when the message is published. * Guaranteed to be unique within the topic. This value may be read by a * subscriber that receives a `PubsubMessage` via a `Pull` call or a push * delivery. It must not be populated by the publisher in a `Publish` call. * </pre> * * <code>string message_id = 3;</code> */ public Builder clearMessageId() { messageId_ = getDefaultInstance().getMessageId(); onChanged(); return this; } /**
@java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.google.pubsub.v1.PubsubMessage)) { return super.equals(obj); } com.google.pubsub.v1.PubsubMessage other = (com.google.pubsub.v1.PubsubMessage) obj; boolean result = true; result = result && getData().equals(other.getData()); result = result && internalGetAttributes().equals(other.internalGetAttributes()); result = result && getMessageId().equals(other.getMessageId()); result = result && (hasPublishTime() == other.hasPublishTime()); if (hasPublishTime()) { result = result && getPublishTime().equals(other.getPublishTime()); } result = result && unknownFields.equals(other.unknownFields); return result; }
@java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptorForType().hashCode(); hash = (37 * hash) + DATA_FIELD_NUMBER; hash = (53 * hash) + getData().hashCode(); if (!internalGetAttributes().getMap().isEmpty()) { hash = (37 * hash) + ATTRIBUTES_FIELD_NUMBER; hash = (53 * hash) + internalGetAttributes().hashCode(); } hash = (37 * hash) + MESSAGE_ID_FIELD_NUMBER; hash = (53 * hash) + getMessageId().hashCode(); if (hasPublishTime()) { hash = (37 * hash) + PUBLISH_TIME_FIELD_NUMBER; hash = (53 * hash) + getPublishTime().hashCode(); } hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; }
@java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptor().hashCode(); hash = (37 * hash) + DATA_FIELD_NUMBER; hash = (53 * hash) + getData().hashCode(); if (!internalGetAttributes().getMap().isEmpty()) { hash = (37 * hash) + ATTRIBUTES_FIELD_NUMBER; hash = (53 * hash) + internalGetAttributes().hashCode(); } hash = (37 * hash) + MESSAGE_ID_FIELD_NUMBER; hash = (53 * hash) + getMessageId().hashCode(); if (hasPublishTime()) { hash = (37 * hash) + PUBLISH_TIME_FIELD_NUMBER; hash = (53 * hash) + getPublishTime().hashCode(); } hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; }
@Override public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { String messagePayload = message.getData().toStringUtf8(); String messageId = message.getMessageId(); Map messageAttributes = message.getAttributesMap() == null ? new HashMap<>() : message.getAttributesMap(); log.debug("Received Google pub/sub message with payload: {}\n and attributes: {}", messagePayload, messageAttributes); MessageDescription description = MessageDescription.builder() .subscriptionName(subscriptionName) .messagePayload(messagePayload) .messageAttributes(messageAttributes) .pubsubSystem(pubsubSystem) .ackDeadlineMillis(5 * TimeUnit.SECONDS.toMillis(ackDeadlineSeconds)) // Set a high upper bound on message processing time. .retentionDeadlineMillis(TimeUnit.DAYS.toMillis(7)) // Expire key after max retention time, which is 7 days. .build(); GoogleMessageAcknowledger acknowledger = new GoogleMessageAcknowledger(consumer); try { List<Artifact> artifacts = messageArtifactTranslator.parseArtifacts(messagePayload); description.setArtifacts(artifacts); log.info("artifacts {}", String.join(", ", artifacts.stream().map(Artifact::toString).collect(Collectors.toList()))); } catch (Exception e) { log.error("Failed to process artifacts: {}", e.getMessage(), e); pubsubMessageHandler.handleFailedMessage(description, acknowledger, identity.getIdentity(), messageId); return; } pubsubMessageHandler.handleMessage(description, acknowledger, identity.getIdentity(), messageId); } }
@java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.google.pubsub.v1.PubsubMessage)) { return super.equals(obj); } com.google.pubsub.v1.PubsubMessage other = (com.google.pubsub.v1.PubsubMessage) obj; boolean result = true; result = result && getData() .equals(other.getData()); result = result && internalGetAttributes().equals( other.internalGetAttributes()); result = result && getMessageId() .equals(other.getMessageId()); result = result && (hasPublishTime() == other.hasPublishTime()); if (hasPublishTime()) { result = result && getPublishTime() .equals(other.getPublishTime()); } return result; }
@Override public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { String messagePayload = message.getData().toStringUtf8(); String messageId = message.getMessageId(); Map messageAttributes = message.getAttributesMap() == null ? new HashMap<>() : message.getAttributesMap(); log.debug("Received Google pub/sub message with payload: {}\n and attributes: {}", messagePayload, messageAttributes); MessageDescription description = MessageDescription.builder() .subscriptionName(subscriptionName) .messagePayload(messagePayload) .messageAttributes(messageAttributes) .pubsubSystem(pubsubSystem) .ackDeadlineSeconds(5 * ackDeadlineSeconds) // Set a high upper bound on message processing time. .retentionDeadlineSeconds(7 * 24 * 60 * 60) // Expire key after max retention time, which is 7 days. .build(); GoogleMessageAcknowledger acknowledger = new GoogleMessageAcknowledger(consumer); try { List<Artifact> artifacts = messageArtifactTranslator.parseArtifacts(messagePayload); description.setArtifacts(artifacts); log.info("artifacts {}", String.join(", ", artifacts.stream().map(Artifact::toString).collect(Collectors.toList()))); } catch (Exception e) { log.error("Failed to process artifacts: {}", e.getMessage(), e); pubsubMessageHandler.handleFailedMessage(description, acknowledger, identity.getIdentity(), messageId); return; } pubsubMessageHandler.handleMessage(description, acknowledger, identity.getIdentity(), messageId); } }
recordId = pubsubMessage.getMessageId();
public Builder mergeFrom(com.google.pubsub.v1.PubsubMessage other) { if (other == com.google.pubsub.v1.PubsubMessage.getDefaultInstance()) return this; if (other.getData() != com.google.protobuf.ByteString.EMPTY) { setData(other.getData()); } internalGetMutableAttributes().mergeFrom( other.internalGetAttributes()); if (!other.getMessageId().isEmpty()) { messageId_ = other.messageId_; onChanged(); } if (other.hasPublishTime()) { mergePublishTime(other.getPublishTime()); } onChanged(); return this; }
public Builder mergeFrom(com.google.pubsub.v1.PubsubMessage other) { if (other == com.google.pubsub.v1.PubsubMessage.getDefaultInstance()) return this; if (other.getData() != com.google.protobuf.ByteString.EMPTY) { setData(other.getData()); } internalGetMutableAttributes().mergeFrom( other.internalGetAttributes()); if (!other.getMessageId().isEmpty()) { messageId_ = other.messageId_; onChanged(); } if (other.hasPublishTime()) { mergePublishTime(other.getPublishTime()); } onChanged(); return this; }
public Builder mergeFrom(com.google.pubsub.v1.PubsubMessage other) { if (other == com.google.pubsub.v1.PubsubMessage.getDefaultInstance()) return this; if (other.getData() != com.google.protobuf.ByteString.EMPTY) { setData(other.getData()); } internalGetMutableAttributes().mergeFrom(other.internalGetAttributes()); if (!other.getMessageId().isEmpty()) { messageId_ = other.messageId_; onChanged(); } if (other.hasPublishTime()) { mergePublishTime(other.getPublishTime()); } this.mergeUnknownFields(other.unknownFields); onChanged(); return this; }