In my last couple of PowerWire articles, I have written about using data-queues and about ODBC connectivity.
In this article, we are going to combine those two features and show how we can write a very simple node.js program that sits and waits for entries on a data-queue and then, when it receives an entry, it will send an email using the message from the data queue.
Before we get started on the node program, I will show the structure of the message that is in the data-queue for us to process.
It will include the email address of the supplier to send the confirmation email to, the supplier company, the supplier country code and the order number.
All these parts will be concatinated together to form a string. Each part will be deliminated with a hash character (#). An example of this string would be:
Now on to the node program.
As this program will use ODBC to connect to the IBM i and receive the data queue entries, I will be developing this program locally on my PC. One of the many benefits of using ODBC.
The NPM packages I will be using in the index.js node program will be:
This node.js script will use a .env file for all configuration. Please note that if you use a source control package, Git for example, the .env file should be exluded.
The layout of this environment file for this coding example, is as follows;
The DotEnv package will then let me use the process.env variable for all the configuration values. For example, process.env.DSN will hold the value of the Data Source Name (DSN).
Now onto the node coding. For ease, I’ll split the coding and will fully explain each section.
- Use the required modules for the project from Node Package Manager (NPM)
- Build an ODBC connection object
- Build a SMTP email transport object
In this short section, I’ll build a variable holding the SQL service to receive data from a data queue.
Third and Final Part
- Create a node asynchronous function called getFromDQ
- Make the ODBC connection
- Try and catch for any errors
- A continuous loop, used to get next data queue message
- Execute the data queue SQL statement prepared earlier
- Check if we have a data queue entry
- A quick debug message for article explanation
- Check if the data queue message is *END, which is used to gracefully stop the processing
- If data queue entry empty loop to next one
- Split up the message we now have, so that it is ready for processing
- Create the email transporter object with details from the data queue
- A couple more debug messages
- Error handling
- Execute the getFromDQ function
The figure below details the console output for running the node script.
- Start the Node script with debugging on
- Make connection to SMTP server
- First two attempts at receiving dataqueue, show no entries, the third attempt receives an entry
- Shows the SMTP logs as debugging is switch on
- Found no more entries on data-queue
- An *END processing message found so leave the node script
Here we can see the email produced by the NodeMailer package.
Once again, open source proves how easy it is to integrate our applications and take full advantage of the benefits they bring.
I could have used other methods to achieve this such as using the XML toolkit, but I hope you would agree, the ODBC method makes our code very clean and easy to read.
I would like to thank Jesse Gorskinki, from IBM, for providing an example of using data-queues using node.js. His enthusiasm for open source on the IBM i holds no bounds! Thanks Jesse.
All the examples I have written for this article can be found on my open-source repository on GitHub, which can be found at https://github.com/AndyYouens/f_Learning
If you have any questions, either on this article, or anything else on the IBM i, use the comments below, or send me a message on twitter @AndyYouens
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems with over 40 years IBM midrange experience.
IBM Champion 2021