The Personal Blog of Todd Sharp

Oracle Functions - Invoking Functions With The OCI SDK

Posted By: Todd Sharp on 8/5/2019 4:36 GMT
Tagged: Java

In my last post we took a look at how to create serverless functions which interact with an Autonomous Transaction Processing (ATP) instance. 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.

Image by pixel2013 from Pixabay

Related Posts

Automatic Autonomous Wallet Download & Configuration with Micronaut

Automatic Autonomous Wallet Download & Configuration with Micronaut

Back in September, we announced the Micronaut Oracle Cloud Infrastructure module to help developers easily integrate with the OCI SDK as well as use Micronaut...

Message Driven Microservices & Monoliths with Micronaut - Part 4: Using RabbitMQ for Messaging

Message Driven Microservices & Monoliths with Micronaut - Part 4: Using RabbitMQ for Messaging

Previously, we looked in-depth at messaging for your microservice and monoliths by using an e-commerce example. We first set up a Kafka broker and created...

Message Driven Microservices & Monoliths with Micronaut - Part 3: Switching to Oracle Streaming Service

Message Driven Microservices & Monoliths with Micronaut - Part 3: Switching to Oracle Streaming Service

So far in this series (part 1, part 2), we’ve covered both the “how” and the “why” as it relates to messaging in your modern web applications. We used ...

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!