This topic contains 14 replies, has 3 voices, and was last updated by  Sandeep 4 months, 2 weeks ago.

Loading a properties file once a channel is active

  • Hi, I have a requirement of loading properties file only one time once a channel is started. Subsequent channel runs should use the same properties and channel should not read that property file again. How can I achieve this?

    Thank you in advance

    You create a function on main.lua and call it inside the main.lua.

    For example:

    function init()
    do something here
    end

    init()

    Hi James,

    Thank you for your response. How to make sure this function doesn’t get load again in the next run?

    The entry point for channel is main(). Hence it will not run the init() after channel started.
    *don’t call init() inside the main function

    Hi James,

    I think it won’t work. Here is my requirement :

    1. Channel is inactive.
    2. Start the channel.
    3. As soon as channel starts, get the properties file and load it into an LUA table.
    4. Those properties( through LUA table) will be used throughout the channel( source, filter, destination).
    5. During the next polling time/next run, source component should not load the file again. It should get the properties from the same LUA table that I created on step #3
    6. LUA table should retain the properties till the channel is active.

    Thank you

    Any code included before function main gets executed once at channel start. For example:

    local function getCfg(cfg)
       local fh = io.open(cfg,"r")
       local js = fh:read("*a")
       fh:close()
       return json.parse{data=js}
    end
    
    local cfg = getCfg("IguanaRepo.cfg") -- located in default program directory
    
    function main()
       trace(cfg)
    end
    

    The variable cfg is available in the main() function and contains a table of data parsed from the JSON file IguanaRepo.cfg.

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Oh.. now I got it. Thank you, Jeff and James.:)

    To use this loaded table in the filter (translator) and destination( translator) , do I have to use the store module or is there any other good approaches?

    Thank you

    If this is just a simple properties file, I don’t see any downside to loading it in each translator using the same method. It might be a good idea to move the code to a shared library for maintenance purposes, though.

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Hi Jeff,

    Thank you for suggesting this approach. As you mentioned that any method in main.lua and outside the function main will be called once a channel is started the first time. Does it mean, I should not have any method that is being called from function main but outside the function main.

    main .lua{

    function main()
    local fileLocation = d:\data.txt
    readTxtFile(fileLocation)

    end

    function readTxtFile(fileLoc)
    trace(‘I am reading the file’..fileLoc)
    end

    }

    Should we always have every code inside function main?

    Thanks in advance

    You should define any functions you’ll need outside of function main(), ideally placing them in a common library that may be shared among multiple translators. click the [+] link next to the shared folder on the left, select Create a new file and enter the name of the library to create, for example cfg:

    cfg.lua <— name, not part of the file contents

    local cfg = {}
    
    function cfg.getCfg(cfgFile)
       local fh = io.open(cfgFile,"r")
       local js = fh:read("*a")
       fh:close()
       return json.parse{data=js}
    end
    
    return cfg

    In the main script, before function main() add:

    local cf = require(“cfg”)

    you can now use a functiion called cf.getCfg(“/path/to/file”) anywhere in the script below it. If you only want to call it only at channel start, put it before function main():

    local cf = require("cfg")
    local myCfg = cf.getCfg("C:/CfgDir/AConfigFile.json")
    
    function main()
       ... the myCfg variable can be referenced here
    end

    If you need to call it every time a message is received, call the function inside function main():

    local cf = require("cfg")
    
    function main()
       local cfgFileName =  "C:/CfgDir/AConfigFile.json"
       local myCfg = cf.getCfg(cfgFileName)
       ... do more stuff
    end

    This might be the case if you need to fetch a different configuration file based on the content of the message received, in which case you would call cf.getCfg(cfgFileName) with a different value for cfgFileName.

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Thank you, Jeff. Sorry back and forth. Actually, after understanding this approach I have another concern where I have divided my main function logic( not related to property/config file) into small functions and call those small functions from the main function. Those small functions are in the same “main.lua”. Does this mean these small functions will be also called during the start of the channel?

    function main ()
    downloadFTPFile(fileLoc)
    local data = readFile(fileLoc)
    transformToXML(data )
    end

    function downloadFTPFile(loc)
    –logic to download the file from FTP to the local folder
    end

    function readFile(loc)
    –logic to read CSV file
    return data
    end

    function transformToXML(data )
    –logic to convert it to XML and push to filter
    end

    so all these three small functions will be also called when the channel is started?

    downloadFTPFile
    readFile
    transformToXML

    Thank you

    No. They’re defined but won’t executed until you call them.

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    How the iguana engine is making sure which functions to call on the channel start and before function main? Like for the property loading function, it will be called before the main function.. Why these 3 won’t get executed?

    Sandeep, I think you need to get some fundamental instruction in Lua and possibly programming in general. If you don’t understand the difference between defining a function and calling it, your problem isn’t really with Iguana.

    Here’s the official starting point: https://www.lua.org/pil/1.html

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Hi Jeff,

    Thank you. I will check this.

    Thank you very much for all the help.

Tagged: 

You must be logged in to reply to this topic.