Recently, I had a requirement in my project where my client wants a notification to be send to their external event monitoring system (via a JMS protocol) as soon as there is any failure in identified business critical interfaces. After hearing this requirement, below nice article came to my mind.

http://scn.sap.com/community/pi-and-soa-middleware/blog/2012/10/23/customize-e-mail-body-and-subject-in-alerts-in-sap-pi-731-java-stack-only-part-1-esr

But having said so, I was not very keen in implementing “Alert” interfaces in my landscape and looking for something more generic. So, I started wondering (?), why I cannot create and schedule my own job (somewhat similar to “AlertConsumerJob”) which will keep on inquiring interface status (using AlertRetrieveAPI) and in case of any failures read alerts from the alert store (i.e. Consumer) and report the error logs in a more readable format – just thought of mentioning it 😉 .

So, here below I am showing how we can create a custom job in PI/PO (in just 6 simple steps – yeah that’s true 🙂 ) which will consume alerts from the alert store using “AlertRetrieveAPI” and format the alert text.

Step 1: Create EJB (CustomJOB) and its corresponding EAR (CustomJOB_EAR) project and then import below jar files in your ejb project.

a) engine.jee5.facade.jar

b) jee5.facade.jar

c) tc~je~scheduler~api.jar

Step 2: Create one java class (in my case I have named it as Custom_Job) and start coding the business logic inside OnJob() method.

At runtime, job will execute the logic which is written inside onJob() method.


package com.customjob;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sap.scheduler.runtime.JobContext;
import com.sap.scheduler.runtime.JobParameter;
import com.sap.scheduler.runtime.mdb.MDBJobImplementation;
    /**
 * Message-Driven Bean implementation class for: Custom_JOB
 *
 */
@MessageDriven(activationConfig = {
 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
 @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "JobDefinition='Custom_JOB'")
                        })
public class Custom_JOB extends MDBJobImplementation {
    /**
     * Default constructor.
     */
               /**
             * @see MessageListener#onMessage(Message)
             */
    public void onJob(JobContext ctx) {
    Logger log = ctx.getLogger();
    try {
    JobParameter Alert_URL = ctx.getJobParameter("Alert_URL");
    JobParameter Consumer = ctx.getJobParameter("Consumer");
//Cosume alerts from the alert store using custom "ConsumerURL" method
    RetrieveAlert.ConsumeURL(log, Alert_URL.getStringValue(), Consumer.getStringValue());
    }
 catch (Exception e) {
    log.log(Level.SEVERE, e.toString(), e);
    throw new RuntimeException(e);
                }
        }
}

Few points to remember:

a) destinationType: Value of property “destinationType” should always be “javax.jms.Queue”.


b) messageSelector: Value of property “messageSelector” can be set as your “Job Name” (in my case it is Custom_Job). In fact, you can choose any name over here, but make sure the “JobDefinition” name which you have defined over here has to be identical with the name of job definition (which you will define later in step 4) in job-definition.xml deployment descriptor file.


c) OnJob() method: Inside this method you have to code the business logic that the instance of the job definition should perform at runtime.


d) JobContext Interface: Using this you can access a logger object to write job logs in the database. In addition to that, you can read job parameters from PI (which you will define while scheduling jobs in NWA) using getJobParameter method.


e) “ConsumeURL” method : For a clarity purpose, i have created a separate method named as “ConsumeURL” which will perform the task of consuming alerts from store using “AlertRetrieveAPI” and formatting the alert text.


Step 3: Edit “ejb-j2ee-engine.xml” deployment descriptor file.


<?xml version="1.0" encoding="UTF-8"?>
<ejb-j2ee-engine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <enterprise-beans>
    <enterprise-bean>
      <ejb-name>Custom_JOB</ejb-name>
      <jndi-name> CustomAlert</jndi-name>
      <message-props>
        <destination-name>JobQueue</destination-name>
        <connection-factory-name>JobQueueFactory</connection-factory-name>
      </message-props>
    </enterprise-bean>
  </enterprise-beans>
</ejb-j2ee-engine>

Points to remember:

a) You have to specify “JobQueue” as the destination name, and “JobQueueFactory” as the connection factory name as shown in the code sample above.


b) You have to specify bean name (in my case it’s Custom_JOB) as your ejb-name.

Step 4: Create “job-definition.xml” deployment descriptor file from scratch and then edit the same with below sample code.


<?xml version="1.0" encoding="UTF-8"?>
<job-definitions>
 <job-definition name="Custom_JOB"
        description="Retrieve SAP PO Alerts from Consumer">
                                <job-definition-parameter name="Alert_URL"
                                                data-type="String" direction="IN"/>
                                <job-definition-parameter name="Consumer"
                                                data-type="String" direction="IN"/>
                </job-definition>
</job-definitions>

Job-definition.xml is an additional descriptor which identifies the deployed message driven bean as a JobBean.


Important: In the job-definition.xml file, you have to specify the name of the job definition and it has to be the same as the job name specified in the message selector in the JobBean class (i.e. Custom_JOB, refer step 2.b).

In addition to that, you have to declare the name, data type, and direction of the job parameters used in the Custom_JobBean class. The same parameters you will see while scheduling job in NWA (refer step 6).


Step 5: Open EAR project (CustomJOB_EAR) and edit application-j2ee-engine.xml file with below sample code.



<?xml version="1.0" encoding="UTF-8"?>
<application-j2ee-engine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <provider-name>sap.com</provider-name>
  <reference reference-type="hard">
    <reference-target provider-name="sap.com" target-type= "service">
           scheduler~runtime
    </reference-target>
  </reference>
  <modules-additional>
    <module>
      <entry-name>Custom_JOB.jar</entry-name>
      <container-type>scheduler~container</container-type>
    </module>
  </modules-additional>

</application-j2ee-engine>

Step 6: Deploy EAR file on PO server and eventually schedule your custom job.

Capture1.PNG

Provide values to the input parameters while scheduling custom job (i.e “AlertRetrieveAPI” Endpoint URL and Consumer Name)

Capture4.PNG

And here you go 🙂

Untitled.png

Conclusion: The steps which I have explained above will hold true while creating any custom job in SAP PI/PO and IMO, this could be useful in many types of scenarios. In fact, a replica of “AlertConsumerJob” can also be created and alerts can be send to email server in a desirable format.

References:

http://help.sap.com/saphelp_nwce71/helpdata/en/44/3652e89c3b4cc6e10000000a11466f/content.htm

http://scn.sap.com/community/pi-and-soa-middleware/blog/2013/03/27/alerting-on-aaeaex

http://help.sap.com/saphelp_nw73ehp1/helpdata/en/f0/fa16430baf4c22a5b1265a6a973f36/content.htm – Details about how to register custom consumer using standard API

To report this post you need to login first.

15 Comments

You must be Logged on to comment or reply to a post.

  1. Iñaki Vila

    Hi Amit,

    Sincerely, this is an exceptional contribution. From my point of view the standard alert monitoring of PI has a little gaps and the examples of using these APIs are very useful to people like me that we come from ABAP world.

    Regards.

    (0) 
  2. Veerendra Kumar Mamidi

    Hi Amit,

    This blog is very helpful.

    In the blog, i can see a new alert consumer (Test) has been used and configured.

    It would be great if you could let me know the steps in creating new alert consumer.

    Thanks in Advance!

    Regards,

    Veerendra.

    (0) 
  3. Simoens Fabien

    Hi all,

    Very useful contribution but I have an issue following this method. When I deploy the EAR file and ask to schedule the task to be executed recursively, the javabean file is run only the first time. To run again the javabean I have to redeploy the component. Could you please help me on this issue ?

    Many thanks by advance,

    Fabien Simoens

    (0) 
    1. Amit Srivastava Post author

      Hello,

      I haven’t faced anything like this while scheduling jobs, so i can only say that just make sure u are properly specifying start/end date and period.

      Thanks

      Amit Srivastava

      (0) 
      1. Simoens Fabien

        Hi Amit,

        Thanks for your quick response. I confirm that start date, end date and period are correct, I can see that job has been run on each period …

        Thanks by advance,

        Fabien Simoens

        (0) 
  4. Venkat Nimmagadda

    Hi Amit,

    Nice blog.I just started working on this in our PI server .Can you please suggest where can i find the above mentioned jar files ?

    I checked with my BASIS team ,but no luck .Could you please help me on this .

    Thanks

    Regards

    Venkat

    (0) 
  5. Neha Jain

    Hi Amit,

    It’s indeed useful blog, I wonder, how email’s formatting was done in Consumer_URL method, also how can we verify the scheduler’s name i.e whether the values are coming from AlertRetrieverAPI or some other API’s? 

    Thanks!

    BR, Neha

    (0) 
  6. Thorsten Stork

    Hello,

    nice blog…..

    Is it possible, that you add the “project” to this blog, so that we can see the ProjectType, where e.g. the “job-definition.xml” have to be placed and how you retreive the URL API…?


    Thank you.


    Regards

       Thorsten

    (0) 
  7. Arati Pradhan

    Hi Amil,

    This is a great blog. We have similar requirement and i am looking for sample code for the custom class RetrieveAlert  and  method ConsumeURL ?

    Is it possible to share the same

    Kind Regards

    Arati

    (0) 
  8. BRM Fresher

    Hello Amit,

    Its really a very helpful blog. Thanks a lot for the blog. I have a similar requirement wherein I need to customize the alert subject. Need to know if I can use the similar code?

    Also as the logic is in the class RetrieveAlert . Can you please let me know the code to be written in this class? Also being new to SAP PO and JAVA coding in it would like to know which tool to be used to compile this code and which view /steps to be used in NWDS to deploy it to SAP PO.

    Really appreciate your valuable inputs. Its really very urgent.

    Thanks in advance

    (0) 

Leave a Reply