1

Following this example at Bootiful GCP: Integration with Google Cloud Pub/Sub (4/8), I was trying to build a flow where I read from Google Pubsub subscription and write to another topic.

After starting my application on DEBUG mode, I can see that the messages are arriving from Google PubSub but they are not getting "consumed" because of this

o.s.i.dispatcher.BroadcastingDispatcher : No subscribers, default behavior is ignore

Any help on this is much appreciated.

Following is how my major code looks like-

public class PubsubRouteBuilderService {

    private final PubSubTemplate pubSubTemplate; // injected via Spring

    public PubsubRouteBuilderService(PubSubTemplate pubSubTemplate) {
        this.pubSubTemplate = pubSubTemplate;
    }

    public synchronized boolean buildRoute(PubsubRouteModel pubsubRouteModel) {
        log.info("Building route for: {}", pubsubRouteModel);
        buildPubsubRoute(pubsubRouteModel);
        // some unrelated logic
        return true;
    }

    private void buildPubsubRoute(PubsubRouteModel pubsubRouteModel) {

        final StandardIntegrationFlow standardIntegrationFlow = IntegrationFlows.from(
            RouteBuilderFactory
                    .messageChannelAdapter(
                            RouteBuilderFactory.getMessageChannel(),
                            pubSubTemplate,
                            pubsubRouteModel.getFromSub()))
            .handle(
                    message -> {
                        log.info("consumed new message: [" + message.getPayload() + "]");
                        AckReplyConsumer consumer = message.getHeaders()
                                .get(GcpPubSubHeaders.ORIGINAL_MESSAGE, AckReplyConsumer.class);
                        consumer.ack();
                    })
            .get();

        standardIntegrationFlow.start();
    }
}

Here are the other methods from RouteBuilderFactory as follows-

public static MessageChannel getMessageChannel() {
    return MessageChannels.publishSubscribe().get();
}

public static PubSubInboundChannelAdapter messageChannelAdapter(MessageChannel inputChannel, PubSubTemplate pubSubTemplate, String channelName) {
    PubSubInboundChannelAdapter adapter = new PubSubInboundChannelAdapter(pubSubTemplate, channelName);
    adapter.setOutputChannel(inputChannel);
    adapter.setAckMode(AckMode.MANUAL);
    return adapter;
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Abhishek
  • 681
  • 1
  • 6
  • 25

1 Answers1

0

Your code doesn't seem to be based on that blog post at all...

private void buildPubsubRoute(PubsubRouteModel pubsubRouteModel) {

    final StandardIntegrationFlow standardIntegrationFlow = IntegrationFlows.from(
        RouteBuilderFactory
                .messageChannelAdapter(
                        RouteBuilderFactory.getMessageChannel(),
                        pubSubTemplate,
                        pubsubRouteModel.getFromSub()))
        .handle(
                message -> {
                    log.info("consumed new message: [" + message.getPayload() + "]");
                    AckReplyConsumer consumer = message.getHeaders()
                            .get(GcpPubSubHeaders.ORIGINAL_MESSAGE, AckReplyConsumer.class);
                    consumer.ack();
                })
        .get();

    standardIntegrationFlow.start();
}

You can't just "start" some arbitrary IntegrationFlow object - it must be managed by Spring (declared as a @Bean).

There's some infrastructure that the framework builds behind the scenes to make all this work.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I wanted to get this working without the being managed by Spring because of my use case where I have to start and turn off the routes dynamically (very well while the application is running). Do you I would be better off trying this without `spring-integration`? – Abhishek May 14 '20 at 04:49
  • We don't call them "routes" in Spring Integration; they are flows. For dyncmic flow registration, see [the documentation](https://docs.spring.io/spring-integration/docs/5.3.0.RELEASE/reference/html/dsl.html#java-dsl-runtime-flows). – Gary Russell May 14 '20 at 04:53
  • After going through the documentation, can't believe it was that simple! – Abhishek May 14 '20 at 05:59