Adding TaskListener

When viewing or scheduling a report, JReport Server enables you to call your Java application before or after the process.

In JReport Server, a TaskListener interface has been provided in the package jet.server.api for receiving task events before or after running. You can specify one Java class to implement this interface for a task event. When the event of this task occurs, the corresponding methods in the listener will be invoked. The interface contains two methods: beforeRun and afterRun, enabling you to set your Java application call before or after the process of viewing a report or setting up a schedule. Your applications will return true or false. For true, JReport Server will go on running. While for false, JReport Server will stop there.

Below is an example illustrating how to add TaskListener when setting up a schedule on a report.

  1. Develop your Java class to implement the interface. Here TestTaskListener.java is used, which is available in <install_root>\help\samples\APITaskListener.
  2. Compile TestTaskListener.java to generate the class file.
  3. Edit the batch file setenv.bat in <install_root>\bin. Assuming that TestTaskListener has been saved in C:\JReport\Server\tasklistener, add the path of the class file (C:\JReport\Server\tasklistener) to the ADDCLASSPATH variable in setenv.bat.
  4. Start JReport Server and set up a schedule on a report, check Add TaskListerner to be Invoked in the General tab of the Schedule dialog, then input the class name. In this example, input TastListener, and then submit the task.
  5. In this example, the class returns True.

    Print out the task and schedule properties before and after running the task. You will then get task and schedule information in the command window before and after the task is run.

You can also define properties of your own and transmit them through ServerInfo. To do this, use APIConst.TAG_USERDEFINED_PROPERTY_PREFIX as the prefix for the properties.

For example, if you want to transmit the properties host_name, host_ip and hosp_protocol, you will need to insert the properties, before calling the method runTask, into the properties named prop, as follows:

prop.put(APIConst. TAG_USERDEFINED_PROPERTY_PREFIX+"host_name", "host"); 
prop.put(APIConst. TAG_USERDEFINED_PROPERTY_PREFIX+"host_ip", "127.0.0.1");
prop.put(APIConst. TAG_USERDEFINED_PROPERTY_PREFIX+"host_protocol"+ "TCP/IP");

You can get the value of the properties listed above through the server info object, serverInfo, in the method beforeRun or afterRun of the TaskListener class. See the example below:

host_name=serverInfo.getTaskProperties().get(APIConst.TAG_USERDEFINED_
PROPERTY_PREFIX+"host_name");
host_ip=serverInfo.getTaskProperties().get(APIConst.TAG_USERDEFINED_
PROPERTY_PREFIX+"host_ip");
host_protocol=serverInfo.getTaskProperties().get(APIConst.TAG_USERDEFINED_
PROPERTY_PREFIX+"host_protocol");

Note: All properties without the prefix APIConst.TAG_USERDEFINED_PROPERTY_PREFIX will be denied and discarded by JReport Server.