LinkedIn

Subscribing to Salesforce Platform Events using External Java Client - CometD

Platform Events follow event based Architecture, where multiple subscribers can subscribe to a published event in real time manner. 

Below diagram shows the basic Architecture of the event-based communication Model:


Event - This is a change in state for a business process

Event Message - Data about the event

Producer - Publisher of the event

Event Bus - A communication and storage service for published events

Event Consumer - A subscriber to the channel

Platform events can be subscribed internally and also externally

Platfrom events  - Internal Subscription

Below are some scenarios in which you can consume platform events internally:

1. When we need to execute functionalities asynchronously to save govornor limits

2. To build a proper exception logging framework - You can find more details here 

How to subscribe Internally?


we can see more details about this in a different blogpost.

Platfrom events  - External Consumption

Many Organizations are moving to event-based integration approach and Salesforce Platform Events redefines and make the process easy,  when we need to communicate with multiple external systems with same content.

Platform events are useful for external communication in the below scenarios:

  • We are looking for real time data delivery
  • We need to send same data to multiple subscribers
  • Only changed data needs to be send and not entire record

Subscription Using CometD Client

CometD is a web messaging framework/library which supports pub-subscribe model. You can see more details here.

We can build our own CometD client or there is an opensource, community supported client - EMPConnector available, which we can use.

Why I tried EMPConnector?

In our current project, we used platform events as a messaging mechanism to communicate to an external system, whenever an order record is changed in Salesforce. We used Dell BOOMI as a middleware to subscribe to this event, but they started to receive/consume duplicate events. To verify the issue is on Salesoforce side or BOOMI side, we wanted to verify the event subscription using some other client and EMP Connector helped to verify this.

Steps involved to set up and execute EMP Connector:



  1. Set up Java Developer Kit 8
Make sure you download and install JDK 8 from here.

By default this will be installed under program Files:




Also make sure the path is set correct under enviornment variables as shown below:


You can verify if Java set up is correct or not by running java -version command in the command line:


2. Set up Apache Maven

You can run sample EMP Connector using eclipse or command line. I used command line, and for this we need to install Apache Maven from this link.

We can download the zip version available and extract it and save to local folder.



Once extracted, set PATH for the bin folder:


We can verify set up using the command: mvn -v, and it should look like below:


3. Create a Custom Platform Event

Once the basic set up is done, we can create a sample platfrom event in our org.

I created a sample Platfrom event called Member with below details:


4. Download and Build EMP Connector Project

EMP Connector project is available here.

You can clone this project using gitcommand:



This will copy the project to your local repository:

Now build the jar file using mvn clean package command:

Once it is success, you can see a message like this:



And executable jar files will be created downloading all the dependencies:

5. Subscribe to Platform event channel from command line

Once the set up is completed successfully,  we can subscribe to the platform event using below command:

java -classpath target/emp-connector-0.0.1-SNAPSHOT-phat.jar com.salesforce.emp.connector.example.LoginExample <username> <password+token> /event/Member__e

If the login and subscription is success, we can see a message like below:



6. Publish Event and Verify Subscription


Once the subscription is success, we can publish an event from salesforce, and verify it from command line.

Publish an event using Apex Execute from developer console:


You can see that EMP Connector has received the published event.



We can replay events published in the past 24 hours usng the replay ID .

When we specify a replay ID, this client will replay all events with a replayID greater than the specified ID, as shown in the below screenshot:


How to Connect to Sandbox

To connect to a sandbox, we need to update endpoint URL in LoginHelper.java class as test.salesforce.com instead of login.salesforce.com as shown below:




And then build the package again using mvn clean package command.

Then you can run subscribe to the channel.

References:

https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_intro.htm 

Comments

  1. Nice and Informative Article. Can you tell me where was the duplicate issue? And how did you mitigate it?

    ReplyDelete
    Replies
    1. It was a middle ware issue, where they had multiple listeners added for parallel processing, and listeners receiving same events and processing it. They changed the model on their side to fix this.

      Delete
  2. Hi Meera,

    Exactly what I was looking for :)
    Its works like a charm in Trailhead playground but getting invalid username in my Sandbox, any idea why?

    Thanks,
    Ram

    ReplyDelete
    Replies
    1. Hi Ram,

      I updated post with steps required to connect to a sandbox. Basically you need to update Loginhelper.java class to point endpoint url to test.salesforce.com

      Delete
    2. Hi Meera,

      Thanks a lot for your quick reply. It worked :)
      Congratulations! for your Salesforce MVP

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks for this Meera.. Very helpful..

    ReplyDelete
  5. Hi Meera,
    I am having a requirement like this.
    Connect to Java app from salesforce app using rest API and insert records to salesforce custom object on daily basis. How can we achieve this? If yes in how many ways and what are those?
    Can you pls help me on this ?

    ReplyDelete
  6. Hi Meera, thanks for this article.As you said Boomi could be one of the MW which could be used to subscribe to the platform events in SF.
    Was there any other evaluatation made on alternate tools, if yes are there any, please advise.

    ReplyDelete
  7. I am honestly turned on by way of this wonderful issues. I'd favor to consider high quality material which often position I stumbled onto in just a particular person write-up. Cheers intended for providing. Printed Articles

    ReplyDelete
    Replies
    1. Hi Meera
      can we send event data to kinesis data stream from Emp connectore?

      Delete
  8. I was looking for an easy way to subscribe to platform events. This really helped.
    Thank you for publishing this

    ReplyDelete
  9. Hi Meera
    can we send event data to kinesis data stream from Emp connectore?

    ReplyDelete
  10. Hi Meera, wonderful article, how does the external system like oracle cloud subscribe to salesforce platform event

    ReplyDelete

Post a Comment

Popular posts from this blog

Salesforce Security - Restriction Rules and Scoping Rules

How to develop reusable Invocable Apex methods for Flows