
BTP - Application Job (Exit Notification)
What can you use the exits in the application job for and how are they defined? You can find out more about this in this article.
Table of contents
In the last article we looked at creating the application job and built our first executable job. We have already noticed the first exits in the job catalog. In this article we want to take a closer look at these and what we can do with them.
Introduction
After creating the job catalog, we end up in the form and the object is available in the ABAP Development Tools. In the upper section we find our execution class, in which the parameters and the execution are defined.
In the lower section there are three exits available for checks, value help and notifications. In this article we will introduce you to the exams in more detail.
Implementation
As with the exams, we can create the class by clicking on the link in the job catalog and receive the standard wizard for classes.
Unfortunately, no interface is preassigned in this case either and we have to define the interface IF_APJ_RT_JOB_NOTIF_EXIT ourselves and create the methods using a quick fix (CTRL + 1). Unlike the checks, the methods in the interface are not implemented via DEFAULT, so the quick fix works.
CLASS zcl_bs_demo_job_adt_ntf DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES IF_APJ_RT_JOB_NOTIF_EXIT.
ENDCLASS.
CLASS zcl_bs_demo_job_adt_ntf IMPLEMENTATION.
METHOD if_apj_rt_job_notif_exit~notify_jt_start.
ENDMETHOD.
METHOD if_apj_rt_job_notif_exit~notify_jt_end.
ENDMETHOD.
ENDCLASS.
Methods
In the following section we will now describe the individual methods from the interface and when they are called, unfortunately the documentation is no longer that extensive.
NOTIFY_JT_START
The method is currently not called or supported, unfortunately you can only get the hint via the outlines. Since the interface appears to have been created via SE24 (parameters with ! and texts in the outlines), the ABAP Docs, which will probably be delivered with the next releases, are also missing.
Hint: We tested the call by generating a dump when the method is called, this was not currently the case.
NOTIFY_JT_END
After some testing, the method is only called when the job runs into errors. If the job ends without errors, the exit is not activated. To provoke an error in the execution class, all we need to do is throw the exception of the EXECUTE method, then the job will automatically be set to error and our exit will be called.
RAISE EXCEPTION NEW cx_apj_rt_content( ).
In the method we receive various information about the job, the step the job catalog and the job info, which only contains name and count.
So that we can now generate a notification, we need further information about the job. We use the shared API to read further information about the job. This gives us access to the job creator, runtimes and status.
DATA(ls_job_details) = cl_apj_rt_api=>get_job_details( iv_jobname = is_job_info-job_name
iv_jobcount = is_job_info-job_count ).
To do this, we read the email from the job creator, format the job information for the email and send the email to then validate it.
Notification
If we now schedule the job again, it should now run into errors and a notification should be sent to us.
In the email we now find the prepared job information as JSON in order to validate again what information is available. The actual implementation now works so far.
Complete example
Here you will find the complete exit class with the auxiliary methods for preparing the content for the email and reading the email from the job creator. We explain how to set up email delivery and send emails in other articles on the blog.
CLASS zcl_bs_demo_job_adt_ntf DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_apj_rt_job_notif_exit.
PRIVATE SECTION.
METHODS send_mail
IMPORTING id_receiver TYPE cl_bcs_mail_message=>ty_address
id_subject TYPE string
id_content TYPE string.
METHODS format_json_content
IMPORTING is_job_details TYPE cl_apj_rt_api=>ty_job_info
RETURNING VALUE(rd_result) TYPE string.
ENDCLASS.
CLASS zcl_bs_demo_job_adt_ntf IMPLEMENTATION.
METHOD if_apj_rt_job_notif_exit~notify_jt_start.
" Currently not working
ENDMETHOD.
METHOD if_apj_rt_job_notif_exit~notify_jt_end.
DATA(ls_job_details) = cl_apj_rt_api=>get_job_details( iv_jobname = is_job_info-job_name
iv_jobcount = is_job_info-job_count ).
SELECT SINGLE FROM I_BusinessUserVH
FIELDS DefaultEmailAddress
WHERE UserID = @ls_job_details-job_user
INTO @DATA(ld_mail).
DATA(ld_json_formatted) = format_json_content( ls_job_details ).
send_mail( id_receiver = CONV #( ld_mail )
id_subject = |Error in Job: { is_job_info-job_name }|
id_content = ld_json_formatted ).
ENDMETHOD.
METHOD send_mail.
TRY.
DATA(lo_mail) = cl_bcs_mail_message=>create_instance( ).
lo_mail->set_sender( 'BTP-noreply@CONNECT.com' ).
lo_mail->add_recipient( id_receiver ).
lo_mail->set_subject( CONV #( id_subject ) ).
lo_mail->set_main( cl_bcs_mail_textpart=>create_instance( iv_content = id_content
iv_content_type = 'text/html' ) ).
lo_mail->send( ).
CATCH cx_bcs_mail INTO DATA(lo_error).
RAISE SHORTDUMP lo_error.
ENDTRY.
ENDMETHOD.
METHOD format_json_content.
DATA(ld_content) = /ui2/cl_json=>serialize( data = is_job_details
format_output = abap_true ).
rd_result = replace( val = ld_content sub = cl_abap_char_utilities=>cr_lf with = '<br>' occ = 0 ).
ENDMETHOD.
ENDCLASS.
Remarks
Without ABAP resources, an email can also be sent via Cloud ALM if a job in the ABAP Environment encounters an error. With ABAP you have more options for reacting to errors, but a re-use component is also recommended if you want to send emails again and again, especially for managing the email texts and recipients.
Conclusion
If a job in the system cancels, you now have an easy way to send an email to inform you of the cancellation. Instead of an email, a re-schedule can also be triggered or post-processing can be carried out.