Learn how to create a working service callout that can read from and store to a MongoDB instance using Corticon's flexible datasource management API.
Progress Corticon 5.7 includes a unified datasource configuration file allowing users to configure connections to external sources of data. While this file is mostly used for connecting to external databases (either using ADC or EDC), it’s exposed in the service callout framework so users can connect Corticon Server to a customized datasource and handle the data appropriately even if the database isn’t natively supported. This guide explains how to connect to a MongoDB instance using a REST API.
Before starting, make sure you have all the prerequisite software:
Follow the instructions on the MongoDB website to install and start MongoDB. The website offers installation guides for all major desktop operating systems.
The default connection settings should allow you to connect.
Use “Corticon” as the name for both. (You don’t need to create a document; you’ll be doing that with the REST API).
Start RESTHeart, and you should see output like the following:
Test the endpoint using your favorite browser or HTTP client.
Now the REST API connected to MongoDB is up and running.
Open Corticon Studio and make a simple Vocabulary. You can follow this example of a mileage calculator. It will take in information about a car and calculate how many more miles it can drive before needing fuel.
Create a Rulesheet to do the calculation.
Make a Ruletest and add some sample data to ensure the Decision Service is working.
Run the test and check your results.
Add an ADC configuration to your Vocabulary and choose any database type. (You won’t be using that for the service callout.)
Replace the default URL with the path to the RESTHeart API and append the name of the document to the URL. In this example, the document name is “cars.”
Create a service callout. As a shortcut, import the Corticon 5.7/Samples/Weather Callout file into your project; it already contains all the necessary libraries.
After importing, delete the Java files and, optionally, rename the project.
Write the code to create a class for your callout. Start by storing the data. (In this practice example, don’t worry too much about error handling.)
Export the JAR File and Test the New Service Callout
To export the JAR file, right-click on the project, select Export in the menu and select Java/JAR File from the wizard options.
Add the exported JAR to the project.
Create a Ruleflow to test the new service callout.
Make a new Testsheet in the Ruletest and select the Ruleflow as the test subject.
Run the test.
If you successfully hit the REST API, you should see the new document populated with data.
Now, you have stored data in MongoDB.
Go back to the service callout and update it to read the information from the REST API instead of storing it.
Add the following method to your service callout.
Export the JAR file again and restart Corticon Studio.
Create a new Ruleflow to handle retrieval from the database. Put the calculator Rulesheet in the Ruleflow to use the data retrieved from the database.
Add a new Testsheet with the new Ruleflow as test subject to try out the new service. Run the test.
If the retrieval succeeds, bring everything together by adding the first service callout, which saves information to the database, to the Ruleflow.
Hit the API and see the updated MilesTillEmpty property.
With that, we're done with this example. If you’re feeling adventurous, you can also export the datasource configuration file and build a Decision Service for deploying to the server. Now, you know how to create a working service callout that can read from and store to a MongoDB instance using Corticon. And you can use this approach to create interesting and useful callouts with Corticon’s flexible datasource management API.
Ben is a software engineer working on Corticon. Along with the other members of the team, Ben designs, implements, and maintains various aspects of the studio, server, and web console modules. In his discretionary time Ben likes to dabble with new web technologies, and his GitHub account can be found here.
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
You have the right to request deletion of your Personal Information at any time.
You can also ask us not to pass your Personal Information to third parties here: Do Not Sell My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.Learn More