How do you know when users experience problems with your composite applications within a SOA environment? If you are like most people you simply wait until end-users call you with a problem; unfortunately this could be several hours or even days later. Wouldn’t it be really useful if there was a way to be notified via email the moment a given application encounters a problem or when a web service that is supposed to receive large amounts of orders stops working due to some unforeseen problem?
This blog shows an example of how you can utilize the open source Log4j library in conjunction with the SAP NetWeaver Composition environment (CE) to notify you when errors occur. If you are unfamiliar with Log4j it’s simply an open source logging API provided by Apache that you can easily incorporate into your applications to perform logging functionality. What separates this API is the fact that you can write messages not only to log files but also to a SMTP mail server.
For this example you will need the SAP NetWeaver Composition Environment, the NetWeaver Developer Studio and have some familiarity with the Composite Application Framework (CAF). The specific scenario will utilize a Create-Order Web Service developed in CAF; since the web service directly affects a company’s bottom line it is essential that the Web Service remains functional or at least notifies someone when errors occur in order to minimize loss of revenue.
Let’s get started with the NetWeaver Developer Studio. You need to create 2 projects; first an external library project that will act as a wrapper around the log4j library. Second, a CAF project to provide the functionality for the Create-Order web service. Eventually you will only deploy the CAF project to the Composition Environment. Please note you do not have to deploy the external library project; simply creating an Assembly dependency to the CAF project will ensure that the external library gets bundled with the CAF project during compilation.
Once you have the library in place you need to add the essential Log4j configuration file to the EJB module of the CAF project. Finally, it’s always good practice to create your own LogUtil helper class to separate the logging code from the business logic.
Once you have all these pieces in place you simply need to add logging statements to your code especially in potential problematic areas where you see ‘try/catch’ statements and throwing of exceptions. This is precisely where you need to add Fatal log statements. Using the Log4j configuration file I will show you how to direct Fatal log statements to the SMTP server of your choice. Other less fatal (debug and info) messages will simply get directed to a standard log file.
Next I will show you step by step how this is done within NetWeaver Developer Studio.
1: Create External Library Development Component (DC) called log4j_ext_lib_sol
2: Download and import the Log4j library into the libraries folder. You can get the latest log4j.jar file from Apache.org. If the library contains any underscores “_” remove them by renaming the library as shown below.
3: Define 2 public parts (Assembly and Compilation) to the log4j_ext_lib_sol external library DC. Add the log4j library to each public part.
4: Create a CAF project with the name log4/caf/smtp. Go ahead and create any Web Service; ideally one that creates an order in either a local database or in your ERP System. Open up the Navigator view and notice the 5 different development components part of the CAF Project.
5: Within the Navigator view select the ejbmodule DC and assign a dependency to the api_comp public part of the External Library DC. Ensure that you select Build Time for api_comp public part – this will ensure that you will be able to reference the Log4j API from within your CAF project. Without this dependency you will not be able to compile. Uncheck Build Time checkbox of the api_*** public part.
6: Within the Navigator view select the ear DC and assign a dependency to the api_*** public part of the External Library DC. Ensure that you select Build Time for the api_*** public part – this will ensure that the Log4j library (jar file) will get bundled in with the CAF deployment file. Uncheck Build Time for the api_comp public part.
7: Create the log4j.properties file. Remember to change the package name, log file name including all the email addresses, plus the location of the SMTP Server.
8: Create the LogUtil.java class. Change the package name to reflect your application.
9: Copy both the Log4j.properties and the LogUtil.java file into the following directories as shown in the image below.
10: Add the necessary logging statements to your application code. Specifically add fatal log statements when you want to be notified if an error occurs.
11: Deploy and test your application. For every fatal log message you will receive a notification email in your inbox.