在骆驼上发布关于netty的重大交流

时间:2013-04-16 15:43:42

标签: java netty apache-camel

尝试使用camel中的netty编写大型传输时,出现TooLongFrameException异常。如果没有一些严肃的重构,我无法真正缩小交换的规模。

作为一种解决方法,我明确指定了一个具有更大的estimatedLength的ObjectEncoder。但是,它不会自动将对象转换回其本机形式。我必须在camel路由上调用convertBodyTo(),这需要事先知道类的类型。

以下是一个例子:

public static void main(String[] args) throws Exception{
    JndiContext registry=new JndiContext();

    //Workaround #1:  explicitly specify the encoder so we can increase the estinatedLength.
    registry.bind("encoder", new ObjectEncoder(99999999));
    CamelContext context=new DefaultCamelContext(registry);
    context.addRoutes(new RouteBuilder() {        
        @Override
        public void configure() throws Exception {
            //This is the receiving end of the large data.
            from("netty:tcp://0.0.0.0:9002?transferExchange=true&encoder=#encoder")
                //At this point the body is a BigEndianHeapChannelBuffer not a String
                .to("log:nettyin?level=INFO")
                .bean(System.out, "println")         

                //Workaround #2:  explicitly convert the body to the expected class.  What do you do if you want several possible classes?
                .convertBodyTo(String.class)

                //Now we have a String as the body.
                .to("log:nettyin.string?level=INFO")
                .bean(System.out, "println");            

            //Use this endpoint to send large data.
            from("direct:start")
                .to("log:start?level=INFO")
                .to("netty:tcp://localhost:9002?transferExchange=true&encoder=#encoder");
        }
    });
    context.start();
    final ProducerTemplate template = context.createProducerTemplate();
    String s=org.apache.commons.lang.StringUtils.repeat("x", 1133461);
    template.sendBody("direct:start", s);                
    context.stop();
}

有没有办法不显式调用convertBodyTo()?另外我如何处理泛型?例如,convertBodyTo(List.class)不适用于List< String>。我得到这样的例外:

Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.jboss.netty.buffer.BigEndianHeapChannelBuffer to the required type: java.util.ArrayList with value BigEndianHeapChannelBuffer(ridx=0, widx=422, cap=422)
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:169) ~[camel-core-2.10.3.jar:2.10.3]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99) ~[camel-core-2.10.3.jar:2.10.3]
    ... 40 common frames omitted

0 个答案:

没有答案