Setting Up Event Driven Reporting in JasperServer Tutorial
Introduction
This tutorial covers options and limitations for implementing event driven reporting in JasperServer.
Example: In this tutorial, we use the following example to show how to configure a report to be viewed in JasperServer, and then how to trigger an email when a certain event happens during report execution.
- A list of customers is filtered by the date they opened an account
- A report is deployed to JasperServer and can be executed manually
- A wrapper report is created which sends an email when more than 60 customers have opened an account
- This will be simulated by changing the database manually
- Samples files to follow this example.
Product Versions
This tutorial works with version 3.x and later.
User
Report Developer
Objectives
This guide will enable you to:
- Prepare the environment
- Create the sample report
- Deploy the report to the Repository
- Create the wrapper report
- Schedule the wrapper report
- Test the example
Pre-Requisites
- JasperServer and iReport must be installed
- In this tutorial the iReport JasperServer plug-in is used for deployment and report execution.
- A basic understanding of report creation, deployment and scheduling is required
- JasperServer must be configured to send emails
- Refer the JasperServer Professional Administrators Guide: Chapter 5.6 Configuring a Mail Server, located in the <js-install>/docs folder
Preparing the Environment
To execute the example described above, extract the samples to create the database and set up the connections.
| Step | Action |
| 1 |
Create the database in MySQL
|
| 2 |
Set up the database connection in iReport
Deploy the Data Source to JasperServer using the iReport JasperServer plug-in
|
Creating the Sample
The sample report contains a simple list of customers filtered by the account opened date.
| Step | Action |
| 1 |
Create a new empty report in iReport
|
| 2 |
Create a query selecting all fields from the customer table
|
| 3 |
Add the following fields: customer_id, lname, fname, city, state, country to the detail band and design the report.
|
| 4 |
|
| 5 |
Change the report query and add a where clause containing the opened parameter
WHERE
|
| 6 |
Add a title containing the parameters REPORT_COUNT and opened such as: $V{REPORT_COUNT} + " new Customers since " + new SimpleDateFormat("dd/MM/yyyy").format($P{opened}) Tip: Ensure that you set the evaluation time for this field to Report.
|
| 7 |
Execute the report in iReport; it should look similar to the image below. You can also view a sample of the report in the samples folder: theReport.jrxml.
|
Deploying the Report to the Repository
Deploy the report to the Repository, configure, and execute it.
| Step | Action |
| 1 |
Add the report as JRXML Document to the JasperServer repository
|
| 2 |
Add a Report Unit to the JasperServer repository using the deployed JRXML file as main JRXML as shown in the image below.
|
| 3 |
Add a local input control of type Date for the opened parameter
|
| 4 |
Log into JasperServer and run the report
|
Creating the Wrapper Report
The wrapper report contains the main report as a subreport. Using report parameters and return values, we will define a condition which simulates firing an event.
| Step | Action |
| 1 |
Create a new empty report in iReport
|
| 2 |
Add a new parameter opened which is filled with the last date of the day one month ago, type java.util.Date, not for prompting, default value new Date(new Date().getYear(), new Date().getMonth() -1, new Date().getDate())
|
| 3 |
Add a subreport to the Detail band using the wizard
|
| 4 |
Add the report query counting the rows from the original query, and checking if the returned amount of rows is larger than 60. Note: This query is testing for the number of newly created customer accounts being greater than 60. This query serves as an example and any desired criteria can be defined. Example: The full query is available in the samples folder: query_2.txt select count(*) as cnt from (<original query>) as tmp having cnt > 60
|
| 5 |
Add a new header using the parameter opened and the field cnt such as: $F{cnt} + " new customers since " + new SimpleDateFormat("dd/MM/yyyy").format($P{opened})
|
| 6 |
Execute the report.
|
| 7 |
Change the subreport expression to the repository URI of the main report’s JRXML such as: /Event_Driven_Reporting/Common/edr_mainreport
|
| 8 |
Deploy the wrapper report to the repository as report unit You can also view a sample report in the samples folder: wrapperreport.jrxml
|
Scheduling the Wrapper Report
Schedule the wrapper report to run every minute and send the email in a PDF format.
| Step | Action |
| 1 |
Login to JasperServer and open the repository
|
| 2 |
Select the wrapper report and set up a scheduler for this report:
The report runs now every minute, if you check the snapshot folder .
|
Testing the Example
To test the example, you can create a fake event by updating the database.
| Step | Action |
| 1 |
Update 57 rows of the sample database by executing the statement in the samples folder: update_01.txt. Or, type the following statement into your MySQL frontend: update customer set date_accnt_opened = STR_TO_DATE(CONCAT(YEAR(NOW()),'-',MONTH(NOW()),'-',DAY(date_accnt_opened)), '%Y-%m-%d') where YEAR(date_accnt_opened) = 1990 There will not be any notification in the report.
|
| 2 |
Reset the database to ensure that we get back into the initial state of the database, by executing this statement: update customer set date_accnt_opened = STR_TO_DATE(CONCAT(1990,'-',MONTH(NOW()),'-',DAY(date_accnt_opened)), '%Y-%m-%d') where YEAR(date_accnt_opened) = YEAR(NOW())
|
| 3 |
Produce an email notification by changing 93 rows in the resultset; execute this statement, which is also available in the samples folder: update_02.txt update customer set date_accnt_opened = STR_TO_DATE(CONCAT(YEAR(NOW()),'-',MONTH(NOW()),'-',DAY(date_accnt_opened)), '%Y-%m-%d') where YEAR(date_accnt_opened) = 1993 You should get an email notification within the next few minutes. |
What's Next?
You can now catch any kind of event using default features of JasperServer and JasperReports. Even more complex conditions for report execution can be easily integrated by using scriptlets or external jars.
Further resources:
- JasperReport Ultimate Guide
- JasperServer Administration Guide
- JasperServer User Guide
Written By
This tutorial was written by Kerstin Klein, Professional Services Consultant, July 2009.
Language





