2014-09-06 185 views
1

我想用Camel NettyComponent與在Vert.x中編寫的SocketServer進行通信。從駱駝到Vertx插座的雙向通信服務器

這是我的服務器代碼:

public class NettyExampleServer { 

    public final Vertx vertx; 

    public static final Logger logger = LoggerFactory.getLogger(NettyExampleServer.class); 

    public static int LISTENING_PORT = 15692; 

    public NettyExampleServer(Vertx vertx) { 
     this.vertx = vertx; 
    } 

    private NetServer netServer; 

    private List<String> remoteAddresses = new CopyOnWriteArrayList<String>(); 
    private final AtomicInteger disconnections = new AtomicInteger(); 

    public int getDisconnections(){ 
     return disconnections.get(); 
    } 

    public List<String> getRemoteAddresses(){ 
     return Collections.unmodifiableList(remoteAddresses); 
    } 

    public void run(){ 
     netServer = vertx.createNetServer(); 
     netServer.connectHandler(new Handler<NetSocket>() { 
      @Override 
      public void handle(final NetSocket socket) { 
       remoteAddresses.add(socket.remoteAddress().toString()); 
       socket.closeHandler(new Handler<Void>() { 
        @Override 
        public void handle(Void event) { 
         disconnections.incrementAndGet(); 
        } 
       }); 
       socket.dataHandler(new Handler<Buffer>() { 
        @Override 
        public void handle(Buffer event) { 
         logger.info("I received {}",event); 
         socket.write("I am answering"); 
        } 
       }); 
      } 
     }); 
     netServer.listen(LISTENING_PORT); 



    } 

    public void stop(){ 
     netServer.close(); 
    } 
} 

我試圖建立一個路由如下所示:

public class NettyRouteBuilder extends RouteBuilder { 

    public static final String PRODUCER_BUS_NAME = "producerBus"; 
    public static final String CONSUMER_BUS_NAME = "receiverBus"; 

    private Processor processor = new Processor(){ 
     @Override 
     public void process(Exchange exchange) throws Exception { 
      exchange.setPattern(ExchangePattern.InOut); 
     } 
    }; 

    @Override 
    public void configure() throws Exception { 
     from("vertx:" + PRODUCER_BUS_NAME).process(processor).to("netty:tcp://localhost:"+ NettyExampleServer.LISTENING_PORT + "?textline=true&lazyChannelCreation=true&option.child.keepAlive=true").to("vertx:"+CONSUMER_BUS_NAME); 

    } 
} 

我的測試表明:

  • 如果我消除了處理器在路線上,交付成功但服務器沒有迴應
  • 如果我保留處理器,數據將傳遞到服務器,但由於未收到數據而引發異常。

我在這裏創建了一個小項目:https://github.com/edmondo1984/netty-camel-vertx。我如何使用Camel Netty組件創建雙向路由?

+0

不知道這是否會有所幫助,但Netty路由是IN/OUT消息交換模式,即請求,回覆。沒有先獲得請求,服務器就不能發送消息。當我有機會時,我會看看這個項目。 – Namphibian 2014-09-08 01:47:50

+0

服務器收到請求並在套接字上寫回,但客戶端沒有收到任何東西 – Edmondo1984 2014-09-08 06:43:19

+0

當我有一點時,Cool會檢查代碼。目前在反向工程中陷入遺留應用程序地獄。 – Namphibian 2014-09-08 07:37:22

回答