Running JGroups Cluster on Jelastic

By July 18, 2013 HowTo No Comments
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

I think every developer related to Java has at least heard about JGroups multicast system, which is used for clusters’ creation. In this case cluster nodes can communicate with each other by sending messages. The most important advantage of JGroups is the possibility to adopt its flexible protocol stack to your application requirements. So, you pay only for what you use.

Here are some other features of JGroups:

  • Easy cluster creation and removal
  • Adding and deleting of cluster members
  • Notifications about changes in the cluster
  • Sending and receiving of node-to-node and node-to-cluster messages.

Let’s now take a look at how to deploy your JGroups cluster to the cloud in a few minutes. All you need to do is follow a few simple steps.

Create environments

1. Select VDS and specify the cloudlet limit for it. Type your environment name and click Create.

jgroups cluster environment

In a few minutes your environment will be created.

2. Use any SSH client to establish a SSH connection with your server.

Install Java

1. In the opened console enter your VDS credentials (find them in the Jelastic email you’ve received after creating an environment).

2. Download and install Java 7.

wget {utility_address}
rpm -ivh {java_rpm_package}

Install Java

Create application

1. Navigate to the JGroups official web site and download the latest version package.

jgroups download

2. Extract the files from the package you’ve just downloaded.

3. Add jgroups-all.jar to your classpath.

4. Create your Java application. In our case we use a simple chat application, which sends messages from one server node to another.

package com.mycompany.simplechat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;

public class SimpleChat extends ReceiverAdapter{
    JChannel channel;
    String user_name=System.getProperty("user.name", "n/a");

    public static void main(String[] args) throws Exception {
        new SimpleChat().start();
    }

     private void start() throws Exception {
        channel=new JChannel("tcp.xml");
        channel.setReceiver(this);
        channel.connect("ChatCluster");
        eventLoop();
        channel.close();
    }

    private void eventLoop() {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            try {
                System.out.print("> "); System.out.flush();
                String line=in.readLine().toLowerCase();
                if(line.startsWith("quit") || line.startsWith("exit")) {
                    break;
                }
                line="[" + user_name + "] " + line;
                Message msg=new Message(null, null, line);
                channel.send(msg);
            }
            catch(Exception e) {
            }
        }
    }

    @Override
    public void viewAccepted(View new_view) {
        System.out.println("** view: " + new_view);
    }

    @Override
    public void receive(Message msg) {
        System.out.println(msg.getSrc() + ": " + msg.getObject());
    }
}

5. Create your tcp.xml file and specify your JGroups cluster configuration in there, e.g.:

<config xmlns="urn:org:jgroups"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:org:jgroups http://www.jgroups.org/schema/JGroups-3.2.xsd">
    <TCP bind_port="7800"
         loopback="false"
         recv_buf_size="${tcp.recv_buf_size:20M}"
         send_buf_size="${tcp.send_buf_size:640K}"
         max_bundle_size="64K"
         max_bundle_timeout="30"
         enable_bundling="true"
         use_send_queues="true"
         sock_conn_timeout="300"

         timer_type="wheel"
         timer.min_threads="4"
         timer.max_threads="10"
         timer.keep_alive_time="3000"
         timer.queue_max_size="500"

         thread_pool.enabled="true"
         thread_pool.min_threads="1"
         thread_pool.max_threads="10"
         thread_pool.keep_alive_time="5000"
         thread_pool.queue_enabled="false"
         thread_pool.queue_max_size="100"
         thread_pool.rejection_policy="discard"

         oob_thread_pool.enabled="true"
         oob_thread_pool.min_threads="1"
         oob_thread_pool.max_threads="8"
         oob_thread_pool.keep_alive_time="5000"
         oob_thread_pool.queue_enabled="false"
         oob_thread_pool.queue_max_size="100"
         oob_thread_pool.rejection_policy="discard"/>

    <TCPPING timeout="3000"
             initial_hosts="${jgroups.tcpping.initial_hosts:localhost[7800],localhost[7801]}"
             port_range="1"
             num_initial_members="10"/>
    <MERGE2 min_interval="10000"
            max_interval="30000"/>
    <FD_SOCK/>
    <FD timeout="3000" max_tries="3"/>
    <VERIFY_SUSPECT timeout="1500"/>
    <BARRIER/>
    <pbcast.NAKACK2 use_mcast_xmit="false"
                    discard_delivered_msgs="true"/>
    <UNICAST/>
    <pbcast.STABLE stability_delay="1000" desired_avg_gossip="50000"
                   max_bytes="4M"/>
    <pbcast.GMS print_local_addr="true" join_timeout="3000"

                view_bundling="true"/>
    <MFC max_credits="2M"
         min_threshold="0.4"/>
    <FRAG2 frag_size="60K"/>
    <!--RSVP resend_interval="2000" timeout="10000"/-->
    <pbcast.STATE_TRANSFER/>
</config>

6. Build your project into a jar archive.

Deploy application

1. Connect to your VDS using for example a WinSCP client. Read more in this tutorial.

2. Once you are connected, create a new directory for your application. For example: home/jgroups

3. Navigate to the new folder and upload your application jar file into it using your client app.

Configure JGroups

1. Go back to the Jelastic dashboard and clone the environment you created earlier.

jgroups cluster clone

The cloned environment is identical to the original environment and includes all data in its databases, deployed *.WAR and *.JAR packages.

2. For our sample application to properly work, we also have to replace [ip-vds1-local] with local IP address of your first VDS node and [ip-vds2-local] with local IP of the second node.

java -Djava.net.preferIPv4Stack=true -Djgroups.bind_addr=[ip-vds1-local] -Djgroups.tcpping.initial_hosts=[ip-vds1-local][7
800],[ip-vds2-local][7800] -Dfile.encoding=UTF-8 -jar /root/SimpleChat-1.0-SNAPSHOT-jar-with-dependencies.jar

3. Now we can check the results. Run your application and test JGroups cluster work .

jgroups cluster app

Voila!

As you can see, your JGroups cluster can be deployed into the cloud a matter of minutes with no code changes required. If you already have any experience with JGroups on Jelastic, please share your knowledge with us in the comments below.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Leave a Reply

Subscribe to get the latest updates