Quantcast
Channel: SCN : All Content - Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 7030

How to send Alerts using UDF or JAVA Mapping

$
0
0

For Alert management in PI we have one easy way out to send alert from PI system using UDF or JAVA mapping. We won’t need the ALRTCATDEF and SMTP configuration in PI application for alert management by following this approach. It just depends on few external jars (imported as IMPORTED ARCHIVES in 1.0 and as libraries in 7.1 or above versions) and a JAVA Code with your logic.

You can design you own code as per your requirement can send attachments, can share important data from payload, can provide message IDs and many. Here I am just sharing the basic code of both UDF and Java Mapping.

 

Note - A java mapping code can be used in INTERFACE DETERMINATION step with a condition for sending alert in PI or in INTERFACE\OPERATION MAPPING with actual mapping.

A java code can be used with some triggering logic in PI mapping.


I'm considering that you have understanding of creating UDFs and JAVA mapping.


PFB steps for getting the idea how it’s done:


External jars needed are (attached separately):

1) java-mail-1.4.jar

2)javax.activation.jar

3) aii_map_api.jar or com.sap.xpi.ib.mapping.lib.jar

 

External jars in PI (needs to be done if you are using version 7.0):

1.png

Screenshot of Alert mail:

2.png


Create a NWDS Java project with this below code:

 

JAVA MAPPING CODE:

/*

* Created on May 9, 2014

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

 

/**

* @author ashutosh.a.upadhyay

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import java.util.Properties;

 

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.AbstractTrace;

import com.sap.aii.mapping.api.StreamTransformationException;

 

import java.util.HashMap;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Result;

import javax.xml.transform.Source;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

 

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

 

 

public class Alert_Java implements StreamTransformation{

       private Map map = null;

       private AbstractTrace trace = null;

       public void setParameter(Map arg0) {

            map = arg0;   // Store reference to the mapping parameters

            if (map == null) {

                  this.map = new HashMap();

            }

       }

/*public static void main(String args[]) {//FOR EXTERNAL STANDALONE TESTING

            try{

                  FileInputStream fin = new FileInputStream ("C:/Users/ashutosh.a.upadhyay/My Documents/ashuXML1.xml"); //INPUT FILE (PAYLOAD)

                  FileOutputStream fout = new FileOutputStream ("C:/Users/ashutosh.a.upadhyay/My Documents/ashuXML2.xml"); //OUTPUT FILE (PAYLOAD)

                  Alert_Java mapping = new Alert_Java ();

                  mapping.execute(fin, fout);

            }

            catch (Exception e1){

                  e1.printStackTrace();

            }

      }*/

 

      public void execute(InputStream inputstream, OutputStream outputstream) throws StreamTransformationException  {

            try{

 

//Code for sending the input XML as it is for further processing...

// receive XML

                  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

                  DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

                  Document xml = documentBuilder.parse(inputstream);

        

                  // write XML

                  TransformerFactory transformerFactory = TransformerFactory.newInstance();

                  Transformer transformer = transformerFactory.newTransformer();

                  Result result = new StreamResult(outputstream);

                  Source domSource = new DOMSource(xml);

                  transformer.transform(domSource, result);

              

                  String to = "<Recipient mail ID>";

 

                  // Sender's email ID needs to be mentioned

                  String from = "<Sender mail ID>";

 

                  // Assuming you are sending email from localhost

                  String host = "<Hostname of the SMTP server>";

 

                  // Get system properties

                  Properties properties = System.getProperties();

  

                  // Setup mail server

                  properties.setProperty("mail.smtp.host", host);

 

                  // Get the default Session object.

                  Session session = Session.getDefaultInstance(properties);

                   //or use Session session = Session.getInstance(properties); //if you get Unknown Host exception in JAVA

                  // Create a default MimeMessage object.

                  MimeMessage message = new MimeMessage(session);

 

                  // Set From: header field of the header.

                  message.setFrom(new InternetAddress(from));

 

                  // Set To: header field of the header.

                  message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

 

                  // Set Subject: header field

                  message.setSubject("Check the problem dude...");

 

                  // Now set the actual message. Experiment here pass values from payload or desired template

                  message.setText("You did blunder correct that...");

 

                  // Send message

                  Transport.send(message);

 

                  // for standalone test in NWDS    

                  System.out.println("\n"+"File processed");

                  System.out.println("Sent message successfully....");

            }

 

            //Code to transform the Input stream to Output

 

            catch (ParserConfigurationException e) {

                  e.printStackTrace();

                  throw new StreamTransformationException("Can not create DocumentBuilder."+ e.getMessage(), e);

            }

            catch (SAXException e) {

                  e.printStackTrace();

                  throw new StreamTransformationException("Can not read XML. "+ e.getMessage(), e);

            }

            catch (IOException e) {

                  e.printStackTrace();

                  throw new StreamTransformationException("Can not read XML. "+ e.getMessage(), e);

            }

            catch (TransformerConfigurationException e) {

                  e.printStackTrace();

                  throw new StreamTransformationException("Can not create Transformer. "+ e.getMessage(), e);

            }

            catch (TransformerException e) {

                  e.printStackTrace();

                  throw new StreamTransformationException("Can not write XML. "+ e.getMessage(), e);

            }

            catch (MessagingException e) {

                  //trace.addWarning(e.getMessage());

                  e.printStackTrace();

                  throw new StreamTransformationException("Can not send alert. " + e.getMessage(), e);  

            }       

       }

}

 

 

 

JAVA UDF CODE;

public String Mail_Test(String input,Container container){

      // Recipient's email ID needs to be mentioned.

      String to = "<Recipient mail ID>";

 

        // Sender's email ID needs to be mentioned

      String from = "<Sender mail ID>";

 

       // Assuming you are sending email from localhost

       String host = "<Hostname of the SMTP server>";

   

       // Get system properties

       Properties properties = System.getProperties();

   

      // Setup mail server

      properties.setProperty("mail.smtp.host", host);

 

      // Get the default Session object.

      Session session = Session.getDefaultInstance(properties);

    //or use Session session = Session.getInstance(properties); //if you get Unknown Host exception in JAVA

 

       try{

             // Create a default MimeMessage object.

            MimeMessage message = new MimeMessage(session);

 

             // Set From: header field of the header.

             message.setFrom(new InternetAddress(from));

 

             // Set To: header field of the header.

             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

 

             // Set Subject: header field

             message.setSubject("Check the problem dude...");

 

             // Now set the actual message

             message.setText("You did blunder correct that...");

 

             // Send message

             Transport.send(message);

             System.out.println("Sent message successfully....");

      }

      catch (MessagingException mex) {

            mex.printStackTrace();

            throw new RuntimeException(mex.getMessage());

      }

      return input;

  }


Viewing all articles
Browse latest Browse all 7030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>