Communication Between Java Applications (2) – JGroups

JGroups is a library which may serve some APIs to communicate between java applications. Unfortunately it is a little bit complex and has some advanced configurations, so I just show quick and simple example to introducing.

Project Explorer is going to looks like this:
jgroups0

MyPacket.java

package com.jgroups;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyPacket implements Serializable {

	private static final long serialVersionUID = 7279473326145415584L;

	private String message;
	private Integer id;
	private Date creationTime;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Date getCreationTime() {
		return creationTime;
	}

	public void setCreationTime(Date creationTime) {
		this.creationTime = creationTime;
	}

	@Override
	public String toString() {
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		String dateStr = df.format(creationTime).toString();
		return "<MyPacket> ["
				+ "ID=" + id + ", "
				+ "Message:" + message + ", "
				+ "Date=" + dateStr + 
				"]";
	}

}

JGroupsReceiver.java

package com.jgroups;

import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
import org.jgroups.util.Util;

public class JGroupsReceiver extends ReceiverAdapter {

	public static JChannel appChannel = null;

	public static void main(String[] args) {

		try {
			// full path of config file
			appChannel = new JChannel("JGroupsConf_Receiver.xml");

			// name of channel that we connect
			appChannel.connect("myCluster");

			// attend receiver
			appChannel.setReceiver(new JGroupsReceiver());

			System.out.println(appChannel.getState());

			// important if application contains both sender and receiver
			// appChannel.setDiscardOwnMessages(true);

		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public void viewAccepted(View view) {
		System.out.println("New view is: " + Util.print(view.getMembers()));

	}

	public void receive(Message msg) {
		try {
			MyPacket get = (MyPacket) msg.getObject();
			System.out.println("Received:" + get);
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

JGroupsSender.java

package com.jgroups;

import java.util.Date;

import org.jgroups.JChannel;
import org.jgroups.Message;

public class JGroupsSender {
	public static JChannel appChannel = null;

	public static void main(String[] args) throws InterruptedException {
		try {
			// full path of config file
			appChannel = new JChannel("JGroupsConf_Sender.xml");

			// name of channel that we connect
			appChannel.connect("myCluster");

			// important if application contains both sender and receiver
			// appChannel.setDiscardOwnMessages(true);

		} catch (Exception e) {
			System.out.println(e);
		}

		System.out.println(appChannel.getState());

		send();

		//terminating
		appChannel.disconnect();
		System.out.println(appChannel.getState());
		appChannel.close();
		System.out.println(appChannel.getState());
	}

	public static void send() throws InterruptedException {

		for (int i = 0; i < 5; i++) {

			try {

				MyPacket packet = new MyPacket();
				packet.setMessage("test_message");
				packet.setId(i);
				packet.setCreationTime(new Date());
				System.out.println("Sending: " + packet);

				Message msg = new Message(null, null, packet);

				// send all members in the channel
				appChannel.send(msg);

				// to specific destination
				// appChannel.send(address, packet);

			} catch (Exception e) {
				System.out.println(e);
			}

			// wait
			Thread.sleep(2000);
		}

	}

}

JGroupsConf_Receiver.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<TCP bind_addr="${jgroups.tcp.address:127.0.0.1}" bind_port="${jgroups.tcp.port:8082}"
		port_range="1" recv_buf_size="5000000" send_buf_size="640000" />

	<TCPPING initial_hosts="127.0.0.1[8081],127.0.0.1[8082]"
		port_range="1" />

	<pbcast.NAKACK use_mcast_xmit="false" retransmit_timeout="300,600,1200,2400,4800"
		discard_delivered_msgs="true" />
	<pbcast.STABLE />
	<pbcast.GMS />

</config>

JGroupsConf_Sender.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<TCP bind_addr="${jgroups.tcp.address:127.0.0.1}" bind_port="${jgroups.tcp.port:8081}"
		port_range="1" recv_buf_size="5000000" send_buf_size="640000" />

	<TCPPING initial_hosts="127.0.0.1[8081],127.0.0.1[8082]"
		port_range="1" />

	<pbcast.NAKACK use_mcast_xmit="false" retransmit_timeout="300,600,1200,2400,4800"
		discard_delivered_msgs="true" />
	<pbcast.STABLE />
	<pbcast.GMS />

</config>

JGroupsConf_Receiver.xml and JGroupsConf_Sender.xml are NOT EXACTLY SAME.
Make sure that TCP Bind Ports are different if TCP Bind Addresses are same!

First run our Receiver:
jgroups1
Then run Sender:
jgroups2
Final looking of Receiver:
jgroups3

Tutorial:
http://www.jgroups.org/tutorial-3.x/html/

Manual:
http://jgroups.org/manual/index.html

Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: