Created
May 28, 2017 14:22
-
-
Save nanomad/faad8d14336dbd8c2f6a0ec1b219d973 to your computer and use it in GitHub Desktop.
A simple JGroups demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.tgi.demos; | |
import org.jgroups.*; | |
import static it.tgi.demos.JGroupsDemo.Role.MASTER; | |
import static it.tgi.demos.JGroupsDemo.Role.SLAVE; | |
/** | |
* Created by nanomad on 28/05/2017. | |
*/ | |
public class JGroupsDemo extends ReceiverAdapter { | |
private Role role; | |
enum Role { | |
MASTER, | |
SLAVE | |
} | |
private final JChannel channel; | |
public static void main(String[] args) throws Exception { | |
new JGroupsDemo(); | |
} | |
public JGroupsDemo() throws Exception { | |
this.role = SLAVE; | |
channel = new JChannel(); | |
channel.setReceiver(this); | |
channel.connect("Ze Cluster"); | |
} | |
@Override | |
public void viewAccepted(View view) { | |
System.out.println("New view " + view.toString()); | |
Address coordinator = view.getCoord(); | |
if (coordinator.equals(channel.getAddress())) { | |
switch (role) { | |
case SLAVE: | |
System.err.println(channel.getClusterName() + " I'm your master, now"); | |
this.role = MASTER; | |
break; | |
case MASTER: | |
break; | |
default: | |
throw new IllegalStateException("Role not supported: " + role); | |
} | |
} else { | |
switch (role) { | |
case MASTER: | |
System.err.println("Uh-oh, we lost master status! Our lord now is " + coordinator); | |
break; | |
case SLAVE: | |
break; | |
default: | |
throw new IllegalStateException("Role not supported: " + role); | |
} | |
} | |
} | |
@Override | |
public void receive(Message msg) { | |
System.out.println("Got message " + msg.getObject()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment