### Balancing the server load

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

#### Load balancing process

1. When a server in a cluster receives a view task or a scheduled task, it will send a load balancing request to the admin server. The only difference between these two types of task is that for the view task the initial server will be treated as the local server, while for the scheduled task the admin server will be treated as the local server.
2. The admin server 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 IP address of the selected clustered server to the initial server.
3. The initial server sends the task to the selected server.

#### Customized load balancing algorithm based on API

You can write your own load balancing algorithm based on the API included in JReport Enterprise Server. Note that if you set load balancing algorithm with 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\server\en\samples`.

Assuming that your server has been configured to be an admin server, and you have several clustered servers.

Take the following steps:

1. Compile DemoLoadBalancer.java to generate the class file as follows:

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

   Note: When compiling DemoLoadBalancer.java, you need to add JRESServlets.jar to the class path.

2. Add DemoLoadBalancer.class to the class path of setenv.bat in the ADDCLASSPATH variable.
3. In server.properties, set loadbalance.custom_class=DemoLoadBalancer (if there is no such property entry, add one). You can also type `-Dloadbalance.custom_class=DemoLoadBalancer` in the command line.
4. Launch JREntServer.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 Enterprise Server Javadoc located in `<install_root>\help\server\en\api\jet\server\api\cluster`.

###### Related topics:

- Load balancing