本文整理汇总了Java中reactor.io.net.ReactorChannelHandler类的典型用法代码示例。如果您正苦于以下问题:Java ReactorChannelHandler类的具体用法?Java ReactorChannelHandler怎么用?Java ReactorChannelHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ReactorChannelHandler类属于reactor.io.net包,在下文中一共展示了ReactorChannelHandler类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getHandler
import reactor.io.net.ReactorChannelHandler; //导入依赖的package包/类
private ReactorChannelHandler<String, String, HttpChannel<String, String>> getHandler() {
return channel -> {
channel.headers()
.entries()
.forEach(
entry1 -> System.out.println(String.format(
"header [%s=>%s]", entry1.getKey(),
entry1.getValue())));
String response;
try {
response = getWebPage("src/main/java/webapp/ws.html");
} catch (IOException e) {
e.printStackTrace();
response = e.getMessage();
}
System.out.println(String.format("%s from thread %s",
response.toString(), Thread.currentThread()));
return channel.writeWith(Streams.just(response
.toString()));
};
}
示例2: postHandler
import reactor.io.net.ReactorChannelHandler; //导入依赖的package包/类
private ReactorChannelHandler<Buffer, Buffer, HttpChannel<Buffer, Buffer>> postHandler() {
return channel -> {
channel.headers().entries().forEach(entry -> System.out.println(String.format("header [%s=>%s]", entry.getKey(),
entry.getValue())));
return channel.writeWith(Streams
.wrap(channel)
.take(1)
.log("received")
.flatMap(data -> {
final StringBuilder response = new StringBuilder().append("hello ").append(new String(data.asBytes()));
System.out.println(String.format("%s from thread %s", response.toString(), Thread.currentThread()));
return Streams.just(Buffer.wrap(response.toString()));
}));
};
}
示例3: getHandler
import reactor.io.net.ReactorChannelHandler; //导入依赖的package包/类
private ReactorChannelHandler<String, String, HttpChannel<String, String>> getHandler() {
return channel -> {
// channel.headers()
// .entries()
// .forEach(
// entry1 -> System.out.println(String.format(
// "header [%s=>%s]", entry1.getKey(),
// entry1.getValue())));
// System.out.println(channel.uri());
String uri = channel.uri();
if (uri.equals("/"))
uri = "/index.html";
String path = "src/main/webapp" + uri;
String response;
try {
response = getStaticResource(path);
} catch (IOException e) {
e.printStackTrace();
response = e.getMessage();
}
return channel.writeWith(Streams.just(response
.toString()));
};
}
示例4: wsHandler
import reactor.io.net.ReactorChannelHandler; //导入依赖的package包/类
private ReactorChannelHandler<String, String, HttpChannel<String, String>> wsHandler() {
return channel -> {
System.out.println("Connected a websocket client: " + channel.remoteAddress());
channel.headers()
.entries()
.forEach(
entry1 -> System.out.println(String.format(
"header [%s=>%s]", entry1.getKey(),
entry1.getValue())));
channel.consume(
s -> {
System.out.printf("%s greeting = %s%n", Thread.currentThread(), s);
channelBroadcaster.onNext(s);
String[] parts = s.split(":");
if(parts.length == 2) {
switch(parts[0]) { //Command part
case "+1" : wishPlusOne(parts[1]); //Data part
}
}
});
//signal to send initial data to client
timer.submit(time -> {System.out.println("Timeout"); channelBroadcaster.onNext("");}, 50, TimeUnit.MILLISECONDS);
return channelBroadcaster
.flatMap(str -> channel.writeWith(
Streams.just(
wishes.entrySet().stream()
.sorted(Comparator.comparing(entry -> -entry.getValue()))
.map(entry -> entry.getKey() + ":" + entry.getValue())
.reduce("", (acum, val) -> {
return (acum.length() > 0) ? acum + "," + val : val;
})
).map(x -> {System.out.println("TO BE SENT: " + x); return x;})
));
};
}
示例5: getHandler
import reactor.io.net.ReactorChannelHandler; //导入依赖的package包/类
private ReactorChannelHandler<Buffer, Buffer, HttpChannel<Buffer, Buffer>> getHandler() {
return channel -> {
channel.headers().entries().forEach(entry1 -> System.out.println(String.format("header [%s=>%s]", entry1.getKey
(), entry1.getValue())));
channel.params().entrySet().forEach(entry2 -> System.out.println(String.format("params [%s=>%s]", entry2.getKey
(), entry2.getValue())));
StringBuilder response = new StringBuilder().append("hello ").append(channel.params().get("name"));
System.out.println(String.format("%s from thread %s", response.toString(), Thread.currentThread()));
return channel.writeWith(Streams.just(Buffer.wrap(response.toString())));
};
}
示例6: createProtocolListener
import reactor.io.net.ReactorChannelHandler; //导入依赖的package包/类
private HttpServer<Buffer, Buffer> createProtocolListener()
throws Exception {
final Stream<Buffer> stream = Streams
.wrap(processor)
.window(flushCount, flushTime, TimeUnit.SECONDS)
.flatMap(new Function<Stream<Buffer>, Publisher<Buffer>>() {
@Override
public Publisher<Buffer> apply(Stream<Buffer> t) {
return t.reduce(new Buffer(), new BiFunction<Buffer, Buffer, Buffer>() {
@Override
public Buffer apply(Buffer prev, Buffer next) {
return prev.append(next);
}
});
}
})
.process(RingBufferWorkProcessor.<Buffer>create("gpfdist-sink-worker", 8192, false));
HttpServer<Buffer, Buffer> httpServer = NetStreams
.httpServer(new Function<HttpServerSpec<Buffer, Buffer>, HttpServerSpec<Buffer, Buffer>>() {
@Override
public HttpServerSpec<Buffer, Buffer> apply(HttpServerSpec<Buffer, Buffer> server) {
return server
.codec(new GpfdistCodec())
.listen(port);
}
});
httpServer.get("/data", new ReactorChannelHandler<Buffer, Buffer, HttpChannel<Buffer,Buffer>>() {
@Override
public Publisher<Void> apply(HttpChannel<Buffer, Buffer> request) {
request.responseHeaders().removeTransferEncodingChunked();
request.addResponseHeader("Content-type", "text/plain");
request.addResponseHeader("Expires", "0");
request.addResponseHeader("X-GPFDIST-VERSION", "Spring Dataflow");
request.addResponseHeader("X-GP-PROTO", "1");
request.addResponseHeader("Cache-Control", "no-cache");
request.addResponseHeader("Connection", "close");
return request.writeWith(stream
.take(batchCount)
.timeout(batchTimeout, TimeUnit.SECONDS, Streams.<Buffer>empty())
.concatWith(Streams.just(Buffer.wrap(new byte[0]))))
.capacity(1l);
}
});
httpServer.start().awaitSuccess();
log.info("Server running using address=[" + httpServer.getListenAddress() + "]");
localPort = httpServer.getListenAddress().getPort();
return httpServer;
}
示例7: wsHandler
import reactor.io.net.ReactorChannelHandler; //导入依赖的package包/类
private ReactorChannelHandler<String, String, HttpChannel<String, String>> wsHandler() {
return channel -> {
System.out.println("Connected a websocket client: " + channel.remoteAddress());
channel.headers()
.entries()
.forEach(
entry1 -> System.out.println(String.format(
"header [%s=>%s]", entry1.getKey(),
entry1.getValue())));
channel.consume(
s -> {
System.out.printf("%s greeting = %s%n", Thread.currentThread(), s);
channelBroadcaster.onNext(s);
String[] parts = s.split(":");
// System.out.println(Arrays.toString(parts));
if(parts.length == 2) {
switch(parts[0]) { //Command part
case "+1" : wishPlusOne(parts[1]); //Data part
}
}
});
//signal to send initial data to client
timer.submit(time -> {System.out.println("Timeout"); channelBroadcaster.onNext("");}, 50, TimeUnit.MILLISECONDS);
return channelBroadcaster
.flatMap(str -> channel.writeWith(
Streams.just(
wishes.entrySet().stream()
.sorted(Comparator.comparing(entry -> -entry.getValue()))
.map(entry -> entry.getKey() + ":" + entry.getValue())
.reduce("", (acum, val) -> {
return (acum.length() > 0) ? acum + "," + val : val;
})
).map(x -> {System.out.println("TO BE SENT: " + x); return x;})
));
// return Streams.from(new String[]{"Hello", "from", "Reactor", "Websocket"})
// .throttle(10000).flatMap(str -> channel.writeWith(
// Streams.just(
// wishes.entrySet().stream()
// .map(entry -> entry.getKey() + ":" + entry.getValue())
// .reduce("", (acum, val) -> {
// return (acum.length() > 0) ? acum + "," + val : val;
// })
// ).map(x -> {System.out.println("TO BE SENT: " + x); return x;})
// ));
//// return Streams.from(new String[]{"Hello", "from", "Reactor", "Websocket"})
// .throttle(20000).flatMap(str -> channel.writeWith(Streams.just(str)));
};
}
示例8: wsHandler
import reactor.io.net.ReactorChannelHandler; //导入依赖的package包/类
private ReactorChannelHandler<String, String, HttpChannel<String, String>> wsHandler() {
return channel -> {
System.out.println("Connected a websocket client: " + channel.remoteAddress());
// channel.headers()
// .entries()
// .forEach(
// entry1 -> System.out.println(String.format(
// "header [%s=>%s]", entry1.getKey(),
// entry1.getValue())));
channel.consume(
s -> {
System.out.printf("%s greeting = %s%n", Thread.currentThread(), s);
channelBroadcaster.onNext(s);
String[] parts = s.split(":");
if(parts.length == 2) {
switch(parts[0]) { //Command part
case "+1" : wishPlusOne(parts[1]); //Data part
}
}
});
String key = channel.headers().get("Sec-WebSocket-Key").trim();
byte[] sha1 = DigestUtils.sha1(key + GUID);
// byte[] sha1 = DigestUtils.sha1("x3JJHMbDL1EzLkh9GBhXDw==" + GUID);
String base64 = Base64.encodeBase64String(sha1);
// System.out.println("KEY:" + key);
// System.out.println("HEX_CODE:" + sha1);
// System.out.println("BASE_64:" + base64);
// channel.responseStatus(Status.SWITCHING_PROTOCOLS);
channel.responseHeaders()
.add("Sec-WebSocket-Accept", base64)
.add("Upgrade", "websocket")
.add("Connection", "Upgrade")
.add("Access-Control-Allow-Origin", channel.headers().get("Origin"));
//signal to send initial data to client
timer.submit(time -> {System.out.println("Timeout"); channelBroadcaster.onNext("");}, 100, TimeUnit.MILLISECONDS);
return channelBroadcaster
.flatMap(str -> channel.writeWith(
Streams.just(
wishes.entrySet().stream()
.sorted(Comparator.comparing(entry -> -entry.getValue()))
.map(entry -> entry.getKey() + ":" + entry.getValue())
.reduce("", (acum, val) -> {
return (acum.length() > 0) ? acum + "," + val : val;
})
).map(x -> {System.out.println("TO BE SENT: " + x); return x;})
));
};
}