Search in sources :

Example 1 with Stream

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");
}
Also used : FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) Stream(info.xiancloud.core.stream.Stream) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with Stream

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);
}
Also used : StreamFragmentBean(info.xiancloud.core.stream.StreamFragmentBean) UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler) Stream(info.xiancloud.core.stream.Stream) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

Stream (info.xiancloud.core.stream.Stream)2 NotifyHandler (info.xiancloud.core.NotifyHandler)1 UnitResponse (info.xiancloud.core.message.UnitResponse)1 StreamFragmentBean (info.xiancloud.core.stream.StreamFragmentBean)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Test (org.junit.Test)1