forcedotcom / EMP-Connector

A simplified cometd connector for Enterprise Messaging Platform
BSD 3-Clause "New" or "Revised" License
185 stars 243 forks source link

Salesforce emp Connector not waiting for event after subscribe in AWS Lambda #57

Open gorugontula opened 5 years ago

gorugontula commented 5 years ago

I have a simple client using salesforce EMP connector and subscribing to a platform event. When I have it run from commandline on windows machine it works fine and subscription and subsequent events work fine. When I deploy the same code as a Java Lambda function in AWS - it successfully subscribes but does not wait/listen for events. Is there some limitation around using java Bayeaux client in AWS Lambda ?

Below is the code in Lambda handler -

public String handleRequest(Object input, Context context) {
        try {
        context.getLogger().log("Input: " + input);

        Consumer<Map<String, Object>> consumer = event -> System.out.println(String.format("Received:\n%s", JSON.toString(event)));

        BearerTokenProvider tokenProvider = new BearerTokenProvider(() -> {
            try {
                return LoginHelper.login(new URL(SFDC_TEST_URL), SFDC_TEST_USERNAME, SFDC_PASS_SEC_KEY);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        BayeuxParameters params = tokenProvider.login();

        EmpConnector connector = new EmpConnector(params);
        LoggingListener loggingListener = new LoggingListener(true, true);

        connector.addListener(META_HANDSHAKE, loggingListener)
                .addListener(META_CONNECT, loggingListener)
                .addListener(META_DISCONNECT, loggingListener)
                .addListener(META_SUBSCRIBE, loggingListener)
                .addListener(META_UNSUBSCRIBE, loggingListener);

        connector.setBearerTokenProvider(tokenProvider);

        connector.setBearerTokenProvider(tokenProvider);

        connector.start().get(5, TimeUnit.SECONDS);

        TopicSubscription subscription = connector.subscribe(SFDC_TOPIC_EVENT_NAME, EmpConnector.REPLAY_FROM_TIP, consumer).get(2, TimeUnit.SECONDS);

        System.out.println(String.format("Subscribed: %s", subscription));

        // TODO: implement your handler
        return "Hello from Lambda!";
        }catch(Exception e) {
            e.printStackTrace();
            return "exited with error";
        }
    }
PCI-API commented 3 years ago

Hello,

I am running into the same issue now with my instance of EMP connector. It will work locally, but when thrown into a lambda function, it will exit as soon as it subscribes. Did you ever come to a solution as to why the code doesn't wait/listen for events? I tried making the return type void or adding a sleep at the end of the handler, but that didn't seem to work.

Any help would be great here - thanks!