spring-guides / gs-messaging-gcp-pubsub

Messaging with Google Cloud Pub/Sub :: Learn how to exchange messages using Spring Integration channel adapters and Google Cloud Pub/Sub
https://spring.io/guides/gs/messaging-gcp-pubsub
Apache License 2.0
18 stars 32 forks source link

How to subsribe to pubsub emulator messages? #9

Open muzuro opened 6 years ago

muzuro commented 6 years ago

I am trying to create subscriber application, which use subsrciption to emulator. I am unsure how to create Channel connected to emulator instead cloud connection. In link there is code to set emulator host:port which will be used to get messages:

String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext(true).build();

I wonder how i can do same with gcp? I have found this doc, however it seems like it missing examples(links) in Create an inbound channel adapter part.

joaoandremartins commented 6 years ago

@muzuro thanks a lot for your question. For future reference, feel free to also file issues with out project at https://github.com/spring-cloud/spring-cloud-gcp/issues

Our project supports the GCP Pub/Sub emulator. You should manually start the emulator and then start your application with the spring.cloud.gcp.pubsub.emulatorHost property set to the emulator hostname. For example, localhost:8085.

muzuro commented 6 years ago

@joaoandremartins Thanks for answer! Will issue in spring-cloud-gcp project in future. About emulator - have added application.properties file with content:

spring.cloud.gcp.pubsub.emulatorHost=localhost:8381

But app don`t connects to emulator host: App code is very compact:

@SpringBootApplication
public class PubSubApplication {
    private static final Log LOGGER = LogFactory.getLog(PubSubApplication.class);
    public static void main(String[] args) throws IOException {
        System.setProperty("spring.cloud.gcp.pubsub.emulatorHost", "localhost:8381");
        new SpringApplicationBuilder(PubSubApplication.class)
                .web(WebApplicationType.NONE)
                .run(args);
    }
    @Bean
    public MessageChannel pubsubInputChannel(PubSubOperations pubSubTemplate) {
        return new DirectChannel();
    }
    @Bean
    public PubSubInboundChannelAdapter messageChannelAdapter(
            @Qualifier("pubsubInputChannel") MessageChannel inputChannel,
            PubSubOperations pubSubTemplate) {
        PubSubInboundChannelAdapter adapter =
                new PubSubInboundChannelAdapter(pubSubTemplate, MessagingConstants.COMMUNICATION_BROADCAST_SUBSCRIPTION_NAME);
        adapter.setOutputChannel(inputChannel);
        adapter.setAckMode(AckMode.MANUAL);
        return adapter;
    }
    @Bean
    @ServiceActivator(inputChannel = "pubsubInputChannel")
    public MessageHandler messageReceiver() {
        return message -> {
            LOGGER.info("Message arrived! Payload: " + message.getPayload());
            AckReplyConsumer consumer =
                    (AckReplyConsumer) message.getHeaders().get(GcpHeaders.ACKNOWLEDGEMENT);
            consumer.ack();
        };
    }
}

Do i have mistake somewhere?

joaoandremartins commented 6 years ago

I just realised that the emulator support didn't make it on time for the M2 release.

If you use our snapshot version instead (1.0.0.BUILD-SNAPSHOT), having that setting in application.properties along with our Pub/Sub starter in your build file (spring-cloud-gcp-starter-pubsub) should do it. Let me know if it works for you!

renannprado commented 6 years ago

Works in latest version 1.1.0.M1