English 中文(简体)
利用Netty在 Java发送连续数据的最佳方式
原标题:Best way to send continuous data in Java using Netty

I m planning to use Netty to design a TCP Server. When the client connects, I have to immediately start pumping XML data to the client continuously...for hours/days. Its that simple.

因此,我凌驾于“渠道”方法之上,并从这一方法中发送数据,是正确的。

我将使用以下渠道。

ChannelFactory factory =
    new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());

<>NioServerSocketChannelFactory

工人读书以非锁定的方式为一个或多个频道读写。

良好。

根据有效的Java 项目51:Don t 取决于校对进度表,我希望工人准备做“工作单位”,然后完成/恢复。

So in my case, though I have to send data continuously, I want to send some chunk (lets say 1 MB) and then be done (unit of work completed), so that worker thread can return. Then I ll send another 1 MB.

Below example is from the official guide of Netty HERE.
I guess the question is then, in this scenario, if i had to unconditionally keep sending time to the client, how would I do it, considering each send as a unit of work.

这样做的一个途径是,只 put一.,做一read。 睡觉。 任何其他方式?

    package org.jboss.netty.example.time;

    public class TimeServerHandler extends SimpleChannelHandler {

        @Override
        public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
            Channel ch = e.getChannel();

            ChannelBuffer time = ChannelBuffers.buffer(4);
            time.writeInt(System.currentTimeMillis() / 1000);

            ChannelFuture f = ch.write(time);

            f.addListener(new ChannelFutureListener() {
                public void operationComplete(ChannelFuture future) {
                    Channel ch = future.getChannel();
                    ch.close();
                }
            });
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
            e.getCause().printStackTrace();
            e.getChannel().close();
        }
    }
最佳回答

选择一个时段/秒钟会奏效,但不会是网状超尺度。 这将是双向的方案拟订。

相反,安排一名向该频道发送信息的检察官定期工作。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签