recursive.codes


The Personal Blog of Todd Sharp

Getting Your Feet Wet With OCI Streams

Posted By: Todd Sharp on 3/14/2019 7:35 GMT
Tagged: Cloud, Developers, Java

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.



Related Posts

Enterprise Messaging via Oracle Advanced Queuing with Autonomous DB & Micronaut

Enterprise Messaging via Oracle Advanced Queuing with Autonomous DB & Micronaut

I’ve written about messaging many, many times on this blog. And for good reason, too. It’s a popular subject that developers can’t seem to get enough of...

How to Securely Connect to Private Resources (for Free!) via the OCI Bastion Service

How to Securely Connect to Private Resources (for Free!) via the OCI Bastion Service

When working in the cloud, there are often times when your servers and services are not exposed to the public internet. Private virtual cloud networks ...

How To Set Up and Run a (Really Powerful) Free Minecraft Server in the Cloud

How To Set Up and Run a (Really Powerful) Free Minecraft Server in the Cloud

In this post, I’m going to show you how to set up and run your very own private, dedicated Minecraft server in the cloud. I have blogged about this before...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!