How to Create a Multicast Client/Server with Apache Tribes

Client

import static java.lang.System.out;

import java.io.Serializable;

import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.ChannelListener;
import org.apache.catalina.tribes.Member;
import org.apache.catalina.tribes.group.GroupChannel;

public final class TribesClient {

  public static void main(final String[] args) throws Exception {
    final Channel chan = new GroupChannel();

    chan.start(Channel.DEFAULT);

    chan.addChannelListener(new ChannelListener() {

      public boolean accept(final Serializable msg, final Member sender) {
        return true;
      }

      public void messageReceived(final Serializable msg, final Member sender) {
        out.println(msg);
      }

    });
  }

}

Server

import org.apache.catalina.tribes.Channel;
import org.apache.catalina.tribes.group.GroupChannel;

public final class TribesServer {

  public static void main(final String[] args) throws Exception {
    final Channel chan = new GroupChannel();

    chan.start(Channel.DEFAULT);

    while (true)

      if (chan.hasMembers())
        chan.send(chan.getMembers(), "Simples Assim", Channel.SEND_OPTIONS_MULTICAST);

  }

}

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s