iht commented on code in PR #32060:
URL: https://github.com/apache/beam/pull/32060#discussion_r1758966515


##########
sdks/java/io/solace/src/main/java/org/apache/beam/sdk/io/solace/broker/PublishResultHandler.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.solace.broker;
+
+import com.solacesystems.jcsmp.JCSMPException;
+import com.solacesystems.jcsmp.JCSMPStreamingPublishCorrelatingEventHandler;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import org.apache.beam.sdk.io.solace.data.Solace;
+import org.apache.beam.sdk.io.solace.data.Solace.PublishResult;
+import org.apache.beam.sdk.io.solace.write.PublishResultsReceiver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class is required to handle callbacks from Solace, to find out if 
messages were actually
+ * published or there were any kind of error.
+ *
+ * <p>This class is also used to calculate the latency of the publication. The 
correlation key
+ * contains the original timestamp of when the message was sent from the 
pipeline to Solace. The
+ * comparison of that value with the clock now, using a monotonic clock, is 
understood as the
+ * latency of the publication
+ */
+public final class PublishResultHandler implements 
JCSMPStreamingPublishCorrelatingEventHandler {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(PublishResultHandler.class);
+
+  @Override
+  public void handleErrorEx(Object key, JCSMPException cause, long timestamp) {
+    processKey(key, false, Optional.ofNullable(cause));
+  }
+
+  @Override
+  public void responseReceivedEx(Object key) {
+    processKey(key, true, Optional.empty());
+  }
+
+  private void processKey(Object key, boolean isPublished, 
Optional<JCSMPException> cause) {
+    PublishResult.Builder resultBuilder = PublishResult.builder();
+    String messageId;
+    if (key == null) {
+      messageId = "NULL";
+    } else if (key instanceof Solace.CorrelationKey) {
+      messageId = ((Solace.CorrelationKey) key).getMessageId();
+      long latencyMillis = calculateLatency((Solace.CorrelationKey) key);
+      resultBuilder = resultBuilder.setLatencyMilliseconds(latencyMillis);
+    } else {
+      messageId = key.toString();
+    }

Review Comment:
   `Object` :).
   
   Notice that I am overrideing the `responseReceivedEx` method, and that `key` 
is an `Object`. That´s all we know. 
   
   The documentation of Solace has this example (TL;DR: what I did above, 
except that they always use a single class for all the keys): 
https://tutorials.solace.dev/jcsmp/confirmed-delivery/



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@beam.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to