2011-12-07 2 views
4

나는 Netty의 초보자입니다.Netty Camel 샘플

일부 샘플을 찾고 있습니다. (가급적이면 Camel Netty 구성 요소 및 스프링을 사용해야 만하지만 필요하지는 않습니다.)

특히 TCP 메시지를 사용하는 샘플 Netty 앱입니다.

또한 어떻게이 netty 앱을 테스트 할 수있는 JUnit 테스트를 작성할 수 있습니까?

덕분에, 다르

답변

5

나는 아직도 낙타와 통합하려는 가정합니다. 먼저 camel documentation을 살펴 보겠습니다. 그게 당신을 실망 시키면, 실험을 시작해야합니다. 나는 Camel Processor를 Netty Server로 만든 예가 하나 있습니다. Netty 구성 요소는 From 엔드 포인트가 소비하는 서버이고 To 엔드 포인트가 생성되는 클라이언트 인 것과 같이 작동합니다. 서버 인 To 엔드 포인트가 필요하고 구성 요소가이를 지원하지 않았습니다. 나는 Camel Processor를 초기화 할 때 Netty Server를 시작한 Spring Bean으로 간단히 구현했습니다. JBoss Netty documentation and samples은 아주 좋습니다. 그것들을 밟아 나가는 것이 가치가있다.

내 슬림화 된 예입니다. 연결된 모든 클라이언트에 메시지를 보내는 서버입니다. 당신의 Netty를 처음 사용하는 경우 내가보기 엔 내가 위의 링크 된 샘플을 통과 제안 :

public class NettyServer implements Processor { 

private final ChannelGroup channelGroup = new DefaultChannelGroup(); 
private NioServerSocketChannelFactory serverSocketChannelFactory = null; 
private final ExecutorService executor = Executors.newCachedThreadPool(); 

private String listenAddress = "0.0.0.0"; // overridden by spring-osgi value 
private int listenPort = 51501; // overridden by spring-osgi value 

@Override 
public void process(Exchange exchange) throws Exception { 
    byte[] bytes = (byte[]) exchange.getIn().getBody(); 
    // send over the wire 
    sendMessage(bytes); 
} 

public synchronized void sendMessage(byte[] message) { 
    ChannelBuffer cb = ChannelBuffers.copiedBuffer(message); 
    //writes to all clients connected. 
    this.channelGroup.write(cb); 
} 

private class NettyServerHandler extends SimpleChannelUpstreamHandler { 

    @Override 
    public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { 
     super.channelOpen(ctx, e); 
     //add client to the group. 
     NettyServer.this.channelGroup.add(e.getChannel()); 

    } 

    // Perform an automatic recon. 
    @Override 
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { 
     super.channelConnected(ctx, e); 
     // do something here when a clien connects. 
    } 

    @Override 
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { 
     // Do something when a message is received... 
    } 

    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { 
     // Log the exception/ 
    } 

} 

private class PublishSocketServerPipelineFactory implements ChannelPipelineFactory { 

    @Override 
    public ChannelPipeline getPipeline() throws Exception { 
     // need to set the handler. 
     return Channels.pipeline(new NettyServerHandler()); 
    } 
} 

// called by spring to start the server 
public void init() { 

    try { 
     this.serverSocketChannelFactory = new NioServerSocketChannelFactory(this.executor, this.executor); 
     final ServerBootstrap serverBootstrap = new ServerBootstrap(this.serverSocketChannelFactory); 
     serverBootstrap.setPipelineFactory(new PublishSocketServerPipelineFactory()); 
     serverBootstrap.setOption("reuseAddress", true); 
     final InetSocketAddress listenSocketAddress = new InetSocketAddress(this.listenAddress, this.listenPort); 
     this.channelGroup.add(serverBootstrap.bind(listenSocketAddress)); 

    } catch (Exception e) { 

    } 
} 

// called by spring to shut down the server. 
public void destroy() { 

    try { 
     this.channelGroup.close(); 
     this.serverSocketChannelFactory.releaseExternalResources(); 
     this.executor.shutdown(); 
    } catch (Exception e) { 
    } 
} 

// injected by spring 
public void setListenAddress(String listenAddress) { 
    this.listenAddress = listenAddress; 
} 

// injected by spring 
public void setListenPort(int listenPort) { 
    this.listenPort = listenPort; 
} 

}

0

낙타 자료는 예제하지만 그물코 구성 요소에 대한 간단한없이 많이있다.

Netty 구성 요소는 메시지를 소비하고 클라이언트에 응답을 보내도록 소켓 서버를 설정하는 데 사용할 수 있습니다. 웹 검색 시간 후, 나는 보여주기 위해 간단한 낙타의 Netty 안녕하세요 세계 예제로 내 자신의 tutorial using netty component in camel를 만들 :

  1. 을 처리하는 POJO 클래스를 사용하여 TCP 메시지
  2. 를받을 낙타의 그물코 구성 요소를 사용하여 메시지를 수신하고 응답을 작성하십시오.
  3. 클라이언트로 응답을 보냅니다.