### Balancing the server load

In a cluster environment, JReport Server provides a load balancing mechanism which enables the server to work more effectively.

#### Load balancing process

1. When the time of a scheduled task arrives, active schedulers compete and the winner gets to trigger the schedule.
2. The server that has the active scheduler selects a server in the cluster according to the load balancing algorithm specified which can either be a built-in one or a customized one, and then sends the task to the selected server.

Tip: You can also directly specify a server in a cluster to perform a scheduled task instead of using load balancing. To do this, first make sure that the Identify Server Preference option is enabled in the Profile dialog (Profile > Customize Server Preferences > Advanced > Identify Server Preference), and then use the Specify a preferred server to run the task option in the General tab of the Schedule dialog to specify a server manually.

#### Customized load balancing algorithm based on API

You can write your own load balancing algorithm based on the API included in JReport Server. Note that if you create a load balancing algorithm with the API, it will take effect in place of other built-in load balance algorithms you have set.

A demo DemoLoadBalancer.java has been provided to illustrate how to customize load balancing using APIs. You can find it in `<install_root>\help\samples\APICluster`.

Assuming that you have several clustered servers.

Take the following steps:

1. Compile DemoLoadBalancer.java to generate the class file as follows (when compiling DemoLoadBalancer.java, you need to add JRESServlets.jar to the class path):

   `javac -classpath <install_root>\lib\JRESServlets.jar DemoLoadBalancer.java`

2. Add DemoLoadBalancer.class to the class path of setenv.bat in the ADDCLASSPATH variable.
3. Add the parameter `-Dloadbalance.custom_class=DemoLoadBalancer` to the server's startup file JRServer.bat which locates in `<install_root>\bin`. For example:

   `"%JAVAHOME%\bin\java.exe" -Dloadbalance.custom_class=DemoLoadBalancer "-Dinstall.root=%REPORTHOME%" ...`

4. Launch JRServer.bat, the customized loadbalancer DemoLoadBalancer will then be applied.
5. Submit some tasks for running. You will now find that these tasks are allocated to the clustered servers based on the DemoLoadBalancer code.

Reference: For more details, see the jet.server.api.cluster.LoadBalancer interface in JReport Javadoc located in `<install_root>\help\api`.

Notes:

- You can choose the load balancing type by setting the API method setLoadBalanceType() at jet.server.api.admin.ClusterAdminService. For example, setting the API method as setLoadBalanceType(0), setLoadBalanceType(1), setLoadBalanceType(2), and setLoadBalanceType(3) means respectively the algorithm Least Current Reports (Min-load), Round Robin, Least Weighted Current Reports (Weighted Min-load) and Random will be chosen.
- For the load balancing algorithms: the server that holds the active scheduler selects from the servers with the number of concurrently running reports less than maximum number first. However, if all servers are full, it will select from all of them.