Database based config property in WebMethods Part-4

Check out the full project :


Overview :


This is an additional or bonus part of the Database based config property project where we will write utility services to load the config property from the database to the cache and we will also write utility services to fetch one or more properties from config instead of calling the JDBC adapter directly.

We strongly recommend you to check out our previous articles first and then come back to this article for a better understanding by clicking the link given above.

Load Config to Cache :


  • Create a folder with the name ‘property’ under the ‘util’ folder in the ‘HGUtil’ package.
  • Create a flow service with the name ‘loadConfigProperty’ and in the output create a variable of type string with the name ‘result’.
  • Invoke the ‘HGUtil.v1.adapter:selectConfigProperty’ adapter service we created earlier to fetch config properties from the database.
  • Invoke the Branch step and insert ‘selectConfigPropertyOutput/count’ into the switch property. This will allow us to check whether the database returned any rows or not. Based on this information, we can decide whether to load the data into the Cache or not.
    • Inside the Branch step, invoke a Map step. Set the label of the Map step to ‘0’ and hard code the ‘result’ variable with the message ‘No property Database that needs to be loaded to Cache.’ You can customize this message further as needed.
    • Inside the Branch step insert the Sequence step and set the label to ‘$default’, inside the sequence we will write logic to insert the config properties retrieved from the database to Cache.
    • Invoke the ‘pub.cache:removeAll’ service from the ‘WmPublic’ package, and follow the mapping instructions below to clear the Cache.
    • cacheManagerName -> hard code it with ‘%cacheManagerName%’ and check the ‘perform Global variable substitution’ option as we have stored the cache manager name in the global variable.
    • cacheName -> hard code it with ‘%cacheName%’ and check the ‘perform Global variable substitution’ option.
    • Invoke a Map step and initialize the ‘elements’ document list with two nested string children, ‘key’ and ‘value’ We will use these to store key-value pairs from the database and map them while storing in the Cache.
    • Invoke Loop step and set the Input array to ‘selectConfigPropertyOutput/results’ and Output array ‘elements’.
      • Invoke one Map step inside the loop and follow below Mapping.
        • ‘selectConfigPropertyOutput/results/CONFIG_KEY’ -> ‘elements/key’.
        • ‘selectConfigPropertyOutput/results/CONFIG_VALUE’ -> ‘elements/value’.
    • Invoke ‘pub.cache:putAll’, here everything will be mapped implicitly because of the same variable name.
    • Invoke one Map step and hardcode ‘result’ variable with ‘Config Properties has been loaded to Cache’.
  • Invoke one Map step and drop all unnecessary variables except the ‘result’ variable.
  • If you have followed all the steps correctly, the structure will look like the one below. Please ensure you provide step-level comments for improved readability.

Load Config to Cache from UI:


We have created the flow service that can be executed to load data from the database to the cache. Now, we will create a button in the UI that allows us to load the data into the cache directly from the user interface.

  • Insert the following code to create a button after the RowCount in the ‘index.dsp’ file.
    <button class="action-btn" onclick="loadConfig()">Load Config</button>
    
    

     

  • Insert the below code to the ‘script.js’ file to handle the on-click button event.
    function loadConfig(){
        const confirmation = window.confirm('Are you sure you want to load Config to Cache?');
        if (confirmation) {
            // User confirmed, proceed with the save action
            var url = "load.dsp";
            document.location.replace(url);
        } else {
            // User canceled the action, do nothing or provide feedback
        }
    }
  • Create a ‘load.dsp’ file in the ‘pub’ directory and insert the below code into the page. This code will help us handle the request and response.
    <!DOCTYPE html>
    <html>
    
    <head>
        <title>Config Manager</title>
    </head>
    <body>
        <!--Edit property-->
        %invoke HGUtil.v1.util.property:loadConfigProperty%
        <script>
            if('%value result%'!='')
            {
                alert('%value result%');
                document.location.replace('index.dsp');
            }
        </script>
        %onerror%
        <script>
             alert('%value errorMessage%');
             document.location.replace('index.dsp');
        </script>
        %endinvoke%
    </body>
    <!-- Include the external JavaScript file -->
    
    </html>
  • The above code will call the ‘loadConfigProperty’ flow service to load the config from the database to the cache.
  • Now, we will be able to load the configuration from the database to the Cache when the button is clicked.

 

Get Config from Cache :


  • Create a flow service named ‘getProperties’ under the ‘property’ folder. In the input section, create a string list variable with the name ‘propertyKey’ and in the output section, create a string list variable with the name ‘propertyValue’.
  • Invoke ‘pub.cache:getAll’ and follow the below mapping.
    • cacheManagerName -> hard code it with ‘%cacheManagerName%’ and check the ‘perform Global variable substitution’ option as we have stored the cache manager name in the global variable.
    • cacheName -> hard code it with ‘%cacheName%’ and check the ‘perform Global variable substitution’ option.
    • keys -> ‘propertyKey’.
    • elements[0]/value -> ‘propertyValue
  • Now that the development is complete, when you run this service with one or more keys, you will receive the corresponding values from the cache in return.

Conclusion :


This project is now completed, and we hope you have enjoyed this article and found it beneficial, Please click the below link to download the source code.

Leave a Comment