Thomas Darimont
Erfahrenes Mitglied
Hallo,
schaut mal hier:
Startet man davon mehrere Anwendungen bekommt man beispielsweise folgende Ausgabe (für 3 Clients):
Gruß Tom
schaut mal hier:
Java:
/**
*
*/
package de.tutorials;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.jgroups.ChannelClosedException;
import org.jgroups.ChannelNotConnectedException;
import org.jgroups.JChannel;
import org.jgroups.Message;
/**
* @author Tom
*
*/
public class JGroupsExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String props = "UDP(mcast_addr=224.0.0.150;mcast_port=5555):PING";
final JChannel channel = new JChannel(props);
System.out.println("Channel ready...");
channel.connect("BUBU");
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
public void run() {
while (channel.isOpen()) {
try {
String data = "XXX " + System.currentTimeMillis();
System.out.println(channel.getLocalAddress()
+ " Sending... " + data);
channel.send(new Message(null, channel
.getLocalAddress(), data));
} catch (ChannelNotConnectedException channelNotConnectedException) {
channelNotConnectedException.printStackTrace();
} catch (ChannelClosedException channelClosedException) {
channelClosedException.printStackTrace();
}
try {
System.out.println(channel.getLocalAddress()
+ " Received: " + channel.receive(0));
} catch (Exception exception) {
exception.printStackTrace();
}
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
});
TimeUnit.MINUTES.sleep(2);
executorService.shutdown();
channel.close();
}
}
Startet man davon mehrere Anwendungen bekommt man beispielsweise folgende Ausgabe (für 3 Clients):
Code:
log4j:WARN No appenders could be found for logger (org.jgroups.JChannel).
log4j:WARN Please initialize the log4j system properly.
Channel ready...
192.168.174.1:1758 Sending... XXX 1178746249562
192.168.174.1:1758 Received: [dst: 224.0.0.150:5555, src: 192.168.174.1:1758 (1 headers), size = 21 bytes]
192.168.174.1:1758 Sending... XXX 1178746254562
192.168.174.1:1758 Received: [dst: 224.0.0.150:5555, src: 192.168.174.1:1761 (1 headers), size = 21 bytes]
192.168.174.1:1758 Sending... XXX 1178746259562
192.168.174.1:1758 Received: [dst: 224.0.0.150:5555, src: 192.168.174.1:1764 (1 headers), size = 21 bytes]
192.168.174.1:1758 Sending... XXX 1178746264562
192.168.174.1:1758 Received: [dst: 224.0.0.150:5555, src: 192.168.174.1:1755 (1 headers), size = 21 bytes]
192.168.174.1:1758 Sending... XXX 1178746269562
192.168.174.1:1758 Received: [dst: 224.0.0.150:5555, src: 192.168.174.1:1758 (1 headers), size = 21 bytes]
192.168.174.1:1758 Sending... XXX 1178746274562
192.168.174.1:1758 Received: [dst: 224.0.0.150:5555, src: 192.168.174.1:1761 (1 headers), size = 21 bytes]
192.168.174.1:1758 Sending... XXX 1178746279562
192.168.174.1:1758 Received: [dst: 224.0.0.150:5555, src: 192.168.174.1:1764 (1 headers), size = 21 b
Gruß Tom