Back in December we announced the development of a new service on Oracle Cloud Infrastructure called Streaming. The announcement, product page and documentation have a ton of use cases and information on why you might use Streaming in your applications, so let's take a look at the how. The OCI Console allows you to create streams and test them out via the UI dashboard, but here's a simple example of how to both publish and subscribe to a stream in code via the OCI Java SDK.
First you'll need to create a stream. You can do that via the SDK, but it's pretty easy to do via the OCI Console. From the sidebar menu, select Analytics - Streaming and you'll see a list of existing streams in your tenancy and selected compartment.
Click 'Create Stream' and populate the dialog with the information requested:
After your stream has been created you can view the Stream Details page, which looks like this:
As I mentioned above, you can test out stream publishing by clicking 'Produce Test Message' and populating the message and then test receiving by refreshing the list of 'Recent Messages' on the bottom of the Stream Details page.
To get started working with this stream in code, download the Java SDK (link above) and make sure it's on your classpath. After you've got the SDK ready to go, create an instance of a StreamClient which will allow you to make both 'put' and 'get' style requests. Producing a message to the stream looks like so:
Reading the stream requires you to work with a Cursor. I like to work with group cursors, because they handle auto committing so I don't have to manually commit the cursor, and here's how you'd create a group cursor and use it to get the stream messages. In my application I have it in a loop and reassign the cursor that is returned from the call to client.getMessages() so that the cursor always remains open and active.
And that's what it takes to create a stream, produce a message and read the messages from the stream. It's not a difficult feature to implement and the performance is comparable to Apache Kafka in my observations, but it's nice to have a native OCI offering that integrates well into my application. There are also future integration plans for upcoming OCI services that will eventually allow you to publish to a stream, so stay tuned for that.