Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.p14n.postevent.broker.AsyncExecutor;
import com.p14n.postevent.broker.EventMessageBroker;
import com.p14n.postevent.broker.MessageSubscriber;
import com.p14n.postevent.broker.TransactionalEvent;
import com.p14n.postevent.vertx.codec.EventCodec;
import com.p14n.postevent.data.Event;
import io.opentelemetry.api.OpenTelemetry;
Expand Down Expand Up @@ -140,6 +141,38 @@ public void publish(String topic, Event event) {
throw new RuntimeException("Failed to publish event", e);
}
}
public void publish(String topic, TransactionalEvent event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the repeated logging and error handling in publish methods into a helper to simplify the code.

Consider extracting the repeated logging + try/catch into a small helper. For example:

@FunctionalInterface
private interface ThrowingRunnable {
  void run() throws Exception;
}

private void doPublish(String topic, String eventId, ThrowingRunnable action) {
  logger.atDebug()
        .addArgument(topic)
        .addArgument(eventId)
        .log("Publishing event to topic {} with id {}");
  try {
    action.run();
    logger.atDebug()
          .addArgument(topic)
          .addArgument(eventId)
          .log("Successfully published event to topic {} with id {}");
  } catch (Exception e) {
    logger.atError()
          .addArgument(topic)
          .addArgument(eventId)
          .setCause(e)
          .log("Failed to publish event to topic {} with id {}");
    throw new RuntimeException("Failed to publish event", e);
  }
}

Then your two publish overloads become:

public void publish(String topic, Event event) {
  doPublish(topic, event.id(), () -> {
    Publisher.publish(event, topic);
  });
}

public void publish(String topic, TransactionalEvent txEvent) {
  doPublish(topic, txEvent.id(), () -> {
    Publisher.publish(txEvent.event(), txEvent.connection(), topic);
    eventBus.publish("events." + topic, txEvent);
  });
}

This removes the duplicated logger invocations, nested try/catch, and the stray comment, while keeping all behavior intact.


logger.atDebug()
.addArgument(topic)
.addArgument(event.id())
.log("Publishing event to topic {} with id {}");

try {

Publisher.publish(event.event(), event.connection(), topic);

// Then, publish to EventBus for real-time distribution
String eventBusAddress = "events." + topic;
eventBus.publish(eventBusAddress, event);

logger.atDebug()
.addArgument(topic)
.addArgument(event.id())
.log("Successfully published event to topic {} with id {}");

} catch (Exception e) {
logger.atError()
.addArgument(topic)
.addArgument(event.id())
.setCause(e)
.log("Failed to publish event to topic {} with id {}");

}

// First, persist to database using existing Publisher
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Comment placement is misleading after the code block.

Move the comment above the Publisher.publish call to better reflect the actual sequence of operations.


}

/**
* Subscribes to events on a specific topic via the EventBus.
Expand Down
Loading