The Personal Blog of Todd Sharp

Oracle Functions - Invoking Functions With The OCI SDK

Posted By: Todd Sharp on 8/5/2019 8:00 GMT
Tagged: Cloud, Developers, Java

In my last few posts we took a look at how to create serverless functions which interact with an Autonomous Transaction Processing (ATP) instance - first with Java, then with Node. We invoked those functions using the Fn CLI, which was handy for testing, but obviously not so helpful when it comes to integrating these functions into our microservice applications. There are in fact several ways to invoke Oracle Functions, but in this post we'll focus on calling them via the OCI Java SDK.

To get started, we'll need to include the OCI Java SDK in our project. I'm using Gradle this time, so my dependency looks like so (notice line 6):

To invoke our function, we'll need to know two things:  the function 'invokeEndpoint' and the function OCID. There's an easy way to grab both of these using the Fn CLI by calling 'fn inspect': 

Which will result in output similar to this:

Using the example above, The endpoint variable would be: `` and the function OCID would be the value within the "id" attribute (ocid1.fnfunc....). Now that we have these two values, we can invoke our function with the OCI SDK. The examples below will show invoking a few of the Node.JS serverless functions that persist JSON data as shown in my last post. 

First, let's set some variables for our endpoint and function ID, create an AuthenticationDetailsProvider and generate a JSON payload to be sent to the insert function. These examples will use Groovy, but this should look extremely familiar to Java and could very easily be ported to Java with minimal effort:

We have two options for invoking the functions via the SDK: synchronous or asynchronous. First, let's look at invoking them synchronously. We'll do that by creating a FunctionsInvokeClient which accepts our AuthenticationDetailsProvider instance. FunctionsInvokeClient implements AutoCloseable, so we'll use Groovy's withCloseable to make sure things are cleaned up when we're done. The withCloseable closure will receive the client as its only argument and we can use that from within the closure.

To break down the code below, we'll do the following steps:

  1. Set the client endpoint to our function's invokeEndpoint
  2. Invoke the 'read' function to establish the initial size of our dataset
  3. Invoke the 'insert' function to insert a new record
  4. Invoke the 'read' function to retrieve the dataset and visualize our newly inserted record

Which results in the following output:

To invoke the functions asynchronously is nearly identical, but we now use the FunctionsInvokeAsyncClient and the invokeFunction methods now receive an instance of AsyncHandler to handle the response. 

The async invocation produces the following output. Take note of the order and timestamps on the logging output:

Using the SDK and the Fn CLI is not the only way to invoke Oracle Functions from your application. You can also call the invokeEndpoint directly, but you'll need to sign the HTTP request as you would any other REST call to the OCI API.

Photo by Pukpik on Unsplash

Related Posts

Securely Connecting to Autonomous DB Without a Wallet (Using TLS)

Securely Connecting to Autonomous DB Without a Wallet (Using TLS)

I talk to a lot of developers in my job as a Developer Advocate. Sometimes they've been using the products in the Oracle Cloud for a long time, and sometimes...

Send and Receive Messages via REST with Advanced Queuing and ORDS

Send and Receive Messages via REST with Advanced Queuing and ORDS

A while back, I blogged about using Oracle Advanced Queuing (AQ) for messaging within your applications. It's a great option for durable and reliable...

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...

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!