use of org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster in project jackrabbit-oak by apache.
the class BroadcastTest method listen.
private static void listen() throws InterruptedException {
String config = "key 123";
ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
ca.setContext(lc);
PatternLayout pl = new PatternLayout();
pl.setPattern("%msg%n");
pl.setContext(lc);
pl.start();
ca.setLayout(pl);
ca.start();
ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(TCPBroadcaster.class);
logger.addAppender(ca);
logger.setLevel(Level.DEBUG);
TCPBroadcaster receiver = new TCPBroadcaster(config);
receiver.addListener(new Broadcaster.Listener() {
@Override
public void receive(ByteBuffer buff) {
int end = buff.position();
StringBuilder sb = new StringBuilder();
while (buff.remaining() > 0) {
char c = (char) buff.get();
if (c >= ' ' && c < 128) {
sb.append(c);
} else if (c <= 9) {
sb.append((char) ('0' + c));
} else {
sb.append('.');
}
}
String dateTime = new Timestamp(System.currentTimeMillis()).toString().substring(0, 19);
System.out.println(dateTime + " Received " + sb);
buff.position(end);
}
});
Random r = new Random();
int x = r.nextInt();
System.out.println("Sending " + x);
for (int i = 0; i < 10; i++) {
Thread.sleep(10);
ByteBuffer buff = ByteBuffer.allocate(1024);
buff.putInt(0);
buff.putInt(x);
buff.put(new byte[100]);
buff.flip();
receiver.send(buff);
if (!receiver.isRunning()) {
System.out.println("Did not start or already stopped");
break;
}
}
Thread.sleep(Integer.MAX_VALUE);
}
use of org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster in project jackrabbit-oak by apache.
the class PersistentCache method initBroadcast.
private void initBroadcast(String broadcast) {
if (broadcast == null) {
return;
}
if (broadcast.equals("disabled")) {
return;
} else if (broadcast.equals("inMemory")) {
broadcaster = InMemoryBroadcaster.INSTANCE;
} else if (broadcast.startsWith("udp:")) {
String config = broadcast.substring("udp:".length(), broadcast.length());
broadcaster = new UDPBroadcaster(config);
} else if (broadcast.startsWith("tcp:")) {
String config = broadcast.substring("tcp:".length(), broadcast.length());
broadcaster = new TCPBroadcaster(config);
} else {
throw new IllegalArgumentException("Unknown broadcaster type " + broadcast);
}
broadcaster.addListener(this);
}
Aggregations