19
Fri, Apr
5 New Articles

Programmer's Toolbox: How to Really Monitor for Messages

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

If you are going to be a good CL programmer, you need to know how to monitor for messages with the MONMSG command. Most CL programmers think they know how, but I've seen some poor examples. Let me discuss what I consider the important points.

The message that you monitor for must be an escape type of a message sent to your program. You can't monitor for diagnostic messages, completion messages, and so forth. If you are not sure about the message type or the program name, look in the job log or request Help on the message (the first display has the type, and the second display has the From and To programs).

A big mistake some programmers make is trying to monitor for some message that isn't an escape type of message. No error occurs on the Create CL Program (CRTCLPGM) listing because the compiler does not know the valid error conditions associated with each command.

The message type is not part of the message description; the sending program decides the type of message that is sent. It is possible that the same message ID will be sent as a diagnostic by one program and an escape by another.

Another thing that may fool you is this: you see an escape message in the job log that you want to monitor for, but it isn't sent to your program. Typically, this condition occurs when some low-level program receives the message, and does not bubble it up to your program. It must be sent to your program for the MONMSG command to work.

You can also monitor for a notify type of message and a special form of status type of message. I've never found a use for this, but maybe you have.

When you monitor, the ball is in your court. You are telling the system that it is OK to proceed even though an error has occurred. The worst programs to debug are those in which the program monitored for some error and then proceeded as if the error never occurred. Although there are legitimate cases for this type of coding, the important thing to remember is that the ball is in your court. Don't drop it.

Another big mistake I see programmers making is that they always monitor for CPF0000. There is a legitimate use for this, but most of the time programmers are too lazy to determine the specific error ID. The problem with this type of coding is that the program may work for years and then give the wrong results one day because of some unusual condition.

Many commands have a wide variety of error conditions that include such things as no object, no member, no library, not authorized, can't allocate, and damaged. 1 contains an example of what I consider to be poor coding; the program is trying to ensure that there won't be a member when the Add Physical File Member (ADDPFM) command runs later in the program (or in another program).

Many commands have a wide variety of error conditions that include such things as no object, no member, no library, not authorized, can't allocate, and damaged. Figure 1 contains an example of what I consider to be poor coding; the program is trying to ensure that there won't be a member when the Add Physical File Member (ADDPFM) command runs later in the program (or in another program).

Since you can't remove a member if it is allocated (in use), if the file or library does not exist, or if some damage condition has occurred, the program may fail someday. If the program does fail, the job log may tell you that ADDPFM failed because the member exists; however, you will have to look elsewhere in the job log to see that the Remove Member (RMVM) command failed, and the actual message may not even exist (it could have been removed).

The same problem can occur on a range of generic messages like CPF9800. When you monitor for a range of messages, you really have to be an expert to understand what might happen.

No one can afford to put in a specific monitor message identifier for every possible condition, and most of the time you can't provide a more meaningful action than the system provides. Here's my philosophy of when to monitor:

o Monitor when the program can recover. For example, the situation in which the member does not exist can be handled by adding the member.

o Monitor when the system message is not meaningful-for example, the system may know the user is not authorized to the program, so it sends a message such as Not authorized to ORD500CZ in library OEDLIB. The user reading the job log has to translate this into something meaningful to the application. If this is an error you expect, it's probably worthwhile to monitor for the error and send your own specific text.

o Monitor when you are processing multiple requests and one fails. You might want to process all you can and then tell the user that the function was not 100 percent successful. I use this frequently, and it is a legitimate case for monitoring for CPF0000 on an individual command. Most of my code would look like the code in 2.

o Monitor when you are processing multiple requests and one fails. You might want to process all you can and then tell the user that the function was not 100 percent successful. I use this frequently, and it is a legitimate case for monitoring for CPF0000 on an individual command. Most of my code would look like the code in Figure 2.

I use several techniques to determine which message to monitor for. First, I use a short cheat sheet showing the few message IDs that I use at least 90 percent of the time. I am a big fan of the Check Object (CHKOBJ) command. I'd much rather check first than try to deal with exceptions that can occur when you try a "do something" type of command like Copy File (CPYF). This is my message ID list:

Check Object (CHKOBJ)

CPF9801 Not found

CPF9802 Not authorized

CPF9815 Member not found

Delete xxx (DLTxxx)

CPF2105 No object exists (most DLT commands)

Allocate Object (ALCOBJ)

CPF1002 Cannot allocate

Receive File (RCVF)

CPF0864 End of file

Then, if the message ID isn't on my list, I try the error condition and check the message with the Help key.

Finally, the AS/400 Programming Reference Summary V3R1 manual, which lists monitorable messages by command, can be useful to review the message IDs you might want to monitor for. Unfortunately, this book lists only the first level of description and can be confusing. I use the manual only in desperation or to review some things I may have overlooked.

When some commands send an escape message, the message text describes multiple conditions that could be wrong. This may be acceptable when you are reading the text, but when you are trying to program for a specific error condition, ambiguous conditions are bad news. That is why I like CHKOBJ. It's very clear what the escape conditions are.

When you have an unusual message ID you want to monitor for, make sure you read the second-level text to see if the message is ambiguous. Most of the ambiguous messages come from commands that "do something" (like CPYF) rather than "check something" (like CHKOBJ). If there is some ambiguity on a "do something" command, try to find a "check something" command you can use first to avoid the problem.

Some commands, such as CPYF or Submit Job (SBMJOB), send a general escape message preceded by a diagnostic that describes the specific problem. You can access the diagnostic message to determine the specific problem by using Receive Message (RCVMSG), but it isn't easy to do. I'll discuss some solutions in another article.

Most of the time, you will have only a single MONMSG command with a single message ID when you want to monitor. However, you can code for multiple error conditions using code like that shown in 3.

Most of the time, you will have only a single MONMSG command with a single message ID when you want to monitor. However, you can code for multiple error conditions using code like that shown in Figure 3.

I normally want to do different things on different error conditions, so I would use the other approach, shown in 4.

I normally want to do different things on different error conditions, so I would use the other approach, shown in Figure 4.

The CHKOBJ command sends an escape message when the object, library, or member is not found or not authorized. This makes for simple coding when something like not found is the error condition. You just follow CHKOBJ with a DO group as the example code in 5 shows.

The CHKOBJ command sends an escape message when the object, library, or member is not found or not authorized. This makes for simple coding when something like not found is the error condition. You just follow CHKOBJ with a DO group as the example code in Figure 5 shows.

However, when the object should not exist, you need some negative MONMSG logic. Normally, you wind up defining a label, as shown in the code in 6.

However, when the object should not exist, you need some negative MONMSG logic. Normally, you wind up defining a label, as shown in the code in Figure 6.

It is the ability to handle error conditions that helps make CL a very effective control language. Monitoring for messages can be a lot more complex than it looks. Keep it simple and take the time to find the specific case to monitor for.

Jim Sloan, president of Jim Sloan, Inc., is a software vendor and consultant. A retired IBMer, Sloan was a software planner on the S/38 when it began as a piece of paper. He also worked on the planning and early releases of the AS/400. In addition, Sloan wrote the TAA tools that exist in QUSRTOOL and is now the owner of the TAA Productivity Tools product. He has been a speaker at COMMON for many years.

REFERENCE

AS/400 Programming Reference Summary V3R1 (SX41-3720, CD-ROM QBKAUT00).


Programmer's Toolbox: How to Really Monitor for Messages

Figure 1: A Bad Example of Monitoring for a Message

 /* Bad example */ RMVM FILE(LIB1/XXX) MBR(AAA) MONMSG MSGID(CPF0000) /* Ignore */ . . ADDPFM FILE(LIB1/XXX) MBR(AAA) 
Programmer's Toolbox: How to Really Monitor for Messages

Figure 2: CL Example to Process Multiple Requests

 XXXXXX /* Some command like MOVOBJ */ MONMSG MSGID(CPF0000) EXEC(DO) CHGVAR &ERRORFLAG 'X' /* Report error */ ENDDO . . /* At end of program */ IF (&ERRORFLAG *EQ 'X') DO /* Some error */ . /* Send escape message */ ENDDO /* Some error */ 
Programmer's Toolbox: How to Really Monitor for Messages

Figure 3: Monitoring for Multiple Messages at Once

 CHKOBJ OBJ(xxx) OBJTYPE(yyy) AUT(zzz) MONMSG MSGID(CPF9801 CPF9802) EXEC(DO) 
Programmer's Toolbox: How to Really Monitor for Messages

Figure 4: Monitoring for Multiple Messages Separately

 CHKOBJ OBJ(xxx) OBJTYPE(yyy) AUT(zzz) MONMSG MSGID(CPF9801) EXEC(DO) /* No object */ . ENDDO /* No object */ MONMSG MSGID(CPF9802) EXEC(DO) /* Not authorized */ . ENDDO /* Not authorized */ 
Programmer's Toolbox: How to Really Monitor for Messages

Figure 5: Monitoring for an Object That Should Exist

 CHKOBJ OBJ(xxx) OBJTYPE(yyy) AUT(zzz) MONMSG MSGID(CPF9801) EXEC(DO) /* Not found */ . . /* Handle error condition */ . ENDDO /* Not found */ /* Normal processing */ 
Programmer's Toolbox: How to Really Monitor for Messages

Figure 6: Monitoring for an Object That Should Not Exist

 CHKOBJ OBJ(xxx) OBJTYPE(yyy) AUT(zzz) MONMSG MSGID(CPF9801) EXEC(GOTO NORMAL) /* Object exists */ . . /* Handle error condition */ . NORMAL: /* Normal processing */ 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: