use of info.xiancloud.core.stream.Stream in project xian by happyyangyuan.
the class StreamSerializeTest method copy.
@Test
public void copy() throws FileNotFoundException, InterruptedException {
long start = System.nanoTime();
CountDownLatch latch = new CountDownLatch(1);
String file = "/Users/happyyangyuan/Downloads/zz.dmg";
String newFile = "/Users/happyyangyuan/Downloads/yy.dmg";
StreamSerializer.singleton.encodeAndApply(new FileInputStream(file), "ssid", "msgId", streamBean -> {
Stream xianStream = StreamManager.singleton.add(streamBean);
if (streamBean.getHeader().isFirst()) {
new Thread(() -> {
try {
FileUtil.copyFile(xianStream, newFile);
latch.countDown();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).start();
}
});
latch.await();
System.out.println("cost= " + (System.nanoTime() - start) / (1000000 * 1000) + " s");
}
use of info.xiancloud.core.stream.Stream in project xian by happyyangyuan.
the class StreamRpcClientHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, JSONObject msg) throws Exception {
LOG.warn("已关闭此功能");
if (MessageType.isStream(msg)) {
try {
StreamFragmentBean streamFragmentBean = msg.toJavaObject(StreamFragmentBean.class);
MsgIdHolder.set(streamFragmentBean.getHeader().getMsgId());
String ssid = streamFragmentBean.getHeader().getId();
NotifyHandler handler = LocalNodeManager.handleMap.getIfPresent(ssid);
LocalNodeManager.handleMap.invalidate(ssid);
// 以下写出不会阻塞
Stream stream = StreamManager.singleton.add(streamFragmentBean);
if (streamFragmentBean.getHeader().isFirst()) {
UnitResponse responseUnitResponse = UnitResponse.success(stream);
try {
ThreadPoolManager.execute(() -> handler.callback(responseUnitResponse));
} catch (RejectedExecutionException threadPoolAlreadyShutdown) {
LOG.info("线程池已关闭,这里使用临时线程执行任务,针对停服务时线程池已关闭的情况。");
new Thread(() -> handler.callback(responseUnitResponse)).start();
}
}
} catch (Throwable e) {
LOG.error(e);
} finally {
MsgIdHolder.clear();
}
} else
ctx.fireChannelRead(msg);
}
Aggregations