📜 ⬆️ ⬇️

Java Application Interoperability with JGroups

Today I want to talk about JGroups . This is a Java library for organizing group interaction between different Java processes. Applications using JGroups can:The library is widely used, in particular, in the JBoss application server, in the OSCache cache and in the Infinispan Grid platform.

Here I will limit myself to the initial information and describe the creation of a simple group chat in Java.

Beginning of work


So, first you need to download JGroups. You can do this here: http://sourceforge.net/projects/javagroups/files/ . When writing this article, I used version 2.6.13.GA. Also, it requires Apache Commons Logging, or something to replace it (for example, jcl-over-slf4j). Download here: http://commons.apache.org/downloads/download_logging.cgi .
')
If you are using Maven, you can add the JBoss repository:

Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
  1. Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
  2. Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
  3. Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
  4. Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
  5. Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
  6. Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
  7. Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
  8. Copy Source | Copy HTML < repository > < id > jboss </ id > < name > jboss </ name > < url > http://repository.jboss.com/maven2 </ url > < snapshots > < enabled > false </ enabled > </ snapshots > </ repository >
and add depending:

Copy Source | Copy HTML
  1. < dependency >
  2. < groupId > jgroups </ groupId >
  3. < artifactId > jgroups </ artifactId >
  4. < version > 2.6.13.GA </ version >
  5. </ dependency >
  6. < dependency >
  7. < groupId > commons-logging </ groupId >
  8. < artifactId > commons-logging </ artifactId >
  9. < version > 1.1.1 </ version >
  10. </ dependency >

You may need to configure the network interface to work with multicast. In Linux, for this you need to add the corresponding route:

 route add -net 224.0.0.0 netmask 240.0.0.0 dev lo 


Initialization


In order to create a channel, you need to create an object of the JChannel class, passing configuration parameters to it in the constructor. In the example in the configuration, I specify the address to be used for the channel. A complete list of options can be found in the documentation .

 JChannel channel = new JChannel( "UDP(bind_addr=127.0.0.1)" ); 

Now you can connect to the cluster by calling JChannel # connect (). The name of the group is passed to it as a parameter, you can choose any one you like.

 channel.connect( "MyCluster" ); 

Sending messages


Messages in JGroups are represented by the Message class, which contains the sender's address, the recipient's address, and data. Addresses are represented by objects of the Address class, and data is any serializable objects or just an array of bytes. For example, to create a message for the whole group containing a string, you can write:

 new Message( null, null, "Some content" ) 

Here, the first parameter is the receiver, then the sender, and then the content.

To send a message you need to call the JChannel # send method and pass it a message as a parameter. For example:

 channel.send( new Message( null, null, "Some content" ) ) 


Message handling


Now you need to write code to handle messages coming to the application. To do this, call the JChannel # setReceiver method, passing as an argument an object that implements the Receiver interface. For example, to simply print all received messages to print, just write the following code:

Copy Source | Copy HTML
  1. channel.setReceiver ( new ReceiverAdapter () {
  2. @Override
  3. public void receive ( Message m) {
  4. System. out .println (m.getObject ());
  5. }
  6. });
Here, in order to reduce the amount of code, an object is inherited from the ReceiverAdapter class, which provides empty implementations of various Receiver interface methods. As you can see from the example, the receive method is used to process the message, to which the message is passed as a parameter.

Simplest chat


Now you can collect the simplest chat. It will send to the group the lines received from the console and print the received lines to the console. Code example:

Copy Source | Copy HTML
  1. import java.io. BufferedReader ;
  2. import java.io. InputStreamReader ;
  3. import org.jgroups. JChannel ;
  4. import org.jgroups. Message ;
  5. import org.jgroups. ReceiverAdapter ;
  6. public class SimplestChat {
  7. public static void main ( String [] args) throws Exception {
  8. JChannel channel = new JChannel ( "UDP (bind_addr = 127.0.0.1)" );
  9. channel.setReceiver ( new ReceiverAdapter () {
  10. @Override
  11. public void receive ( Message m) {
  12. System. out .println (m.getObject ());
  13. }
  14. });
  15. channel.connect ( "MyCluster" ); // Connect to the group
  16. / ** <br/> * Command processing loop from the console <br/> * /
  17. BufferedReader in = new BufferedReader ( new InputStreamReader (System. In ));
  18. while ( true ) {
  19. String line = in .readLine ();
  20. if (line.equalsIgnoreCase ( "quit" ) || line.equalsIgnoreCase ( "exit" )) {
  21. break ;
  22. }
  23. channel.send ( new Message ( null , null , line));
  24. }
  25. channel.close (); // Disconnect from the group upon completion
  26. }
  27. }
When run, this code should output something like:

   Nov 8, 2009 4:18:27 PM org.jgroups.JChannel init
   INFO: JGroups version: 2.6.13.GA

What's next?


Here I considered only creating a channel and sending messages to a group. There are quite a few moments left: addressing, messages to a specific process and, most importantly, management of the group. Those wishing to familiarize themselves with them can have a look at the JGroups documentation: http://jgroups.org/ug.html .

Source: https://habr.com/ru/post/74660/


All Articles