使用Apache Camel和netty4:route创建netcat样式使用者

时间:2015-11-21 18:53:36

标签: spring spring-boot apache-camel netty

我尝试创建一个tcp接收器,它可以通过某个端口获取数据并将其存储到文件中。我使用Apache Camel 2.16.1,Spring Boot 1.3.0-RELEASE和netty 4.0.33。

我的设置类似于camel-spring-boot启动程序中描述的,路由定义是这样的:

@Component
public class Routes {

  @Bean
  RoutesBuilder myRouter() {
    return new RouteBuilder() {
      @Override
      public void configure() throws Exception {
        from( "netty4:tcp://192.168.0.148:10001?sync=false&allowDefaultCodec=false&decoder=#decoder")
        .to("file:/temp/printjobs");
      }
    };
  }
}

我创建了一个如下所示的解码器:

public class RawPrinterDecoder extends MessageToMessageDecoder<ByteBuf> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg,
            List<Object> out) throws Exception {

        byte[] array = new byte[msg.readableBytes()];
        msg.getBytes(0, array);
        String string = new String(array);
        out.add(string);

        System.out.println("Received " + msg.readableBytes()
                + " in a buffer with maximum capacity of " + msg.capacity());
        System.out.println("50 first bytes I received: "
                + string.substring(0, 50));
    }
}

使用以下命令发送数据:

cat binaryfile | nc 192.168.0.148 10001

这条路线是建成的,但是当我使用它时,我无法使原来的binaryfile处于原始形状,而是接收到几个数据块:

Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 1024 in a buffer with maximum capacity of 1024
Received 16384 in a buffer with maximum capacity of 16384
Received 16384 in a buffer with maximum capacity of 16384
Received 16384 in a buffer with maximum capacity of 16384
Received 5357 in a buffer with maximum capacity of 16384

(正如你所看到的那样,我的原始文件是以增加大小的块处理的,大小为70839字节)

收到的每个块都存储在一个单独的文件中,因为我无法加入这些部分:

24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-1
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-3
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-5
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-7
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-9
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-11
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-13
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-15
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-17
24.11.2015  23:47             1.024 ID-Tower-53867-1448405258593-0-19
...

如何识别第一个和最后一个块并将它们连接到解码器中?我做了一个涉及getMaxMessagesPerRead()的方法,但这会返回16,我的文件被分成20个数据块。

0 个答案:

没有答案