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

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

I've written many blog posts about connecting to an Autonomous DB instance in the past. Best practices evolve as tools, services, and frameworks become...

Sending Email With OCI Email Delivery From Micronaut

Sending Email With OCI Email Delivery From Micronaut

Email delivery is a critical function of most web applications in the world today. I've managed an email server in the past - and trust me - it's not fun...

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

In my last post, we looked at the technical aspects of my Brain to the Cloud project including much of the code that was used to collect and analyze the...

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!