I have lots of customers. Each customer has its own namespace. I am using Google dataflow to manage lots of messages. Each namespace might or might not have target output topic for said dataflow in pubsub. The topic name is dynamically put together inside dataflow based on multiple parameters.

Example code:

private static void run(DataflowPipelineOptions options) {
        Pipeline pipeline = Pipeline.create(options);
        pipeline.apply("Read input", PubsubIO.readMessagesWithAttributesAndMessageId().fromSubscription(INPUT_SUBSCRIPTION))
            .apply(...)
            .apply(PubsubIO.writeMessages()
                    .to(in -> in.getValue().getAttribute("namespace") + in.getValue().getAttribute("environment")));
}

The problem I am facing is that PubsubIO throws an exception if topic does not exist. I don't want that to happen. In case of non existing topic I want dataflow to ACK the message. I cannot figure out if it is even possible. Can I somehow catch the exception from PubsubIO.write method to ack the message myself? Is there a better way to publish messages to different dynamic pubsub topics?

1

There are 1 answers

0
XQ Hu On

You can create a new class that inherits PubsubIO but overrides writeMessages with try/catch.