Apache camel 将消息从路由结束发送到路由开始

Apache camel 将消息从路由结束发送到路由开始,apache-camel,Apache Camel,如何将消息从路由的末尾发送到路由的开头(即从生产者端点发送到消费者端点) 我的producer端点是一个套接字服务器,偶尔一个连接的客户端会发送一条消息,需要转发到另一端 从(“socks:client:localhost:3456”)到(“socks:server:localhost:3333”) 我被困在以下代码中的messageReceived: public class SocksProducer extends DefaultAsyncProducer { private List&

如何将消息从路由的末尾发送到路由的开头(即从生产者端点发送到消费者端点)

我的producer端点是一个套接字服务器,偶尔一个连接的客户端会发送一条消息,需要转发到另一端

从(“socks:client:localhost:3456”)到(“socks:server:localhost:3333”)

我被困在以下代码中的
messageReceived

public class SocksProducer extends DefaultAsyncProducer {

private List<Channel> connectedClients = new CopyOnWriteArrayList<Channel>();

public SocksProducer(SocksEndpoint socksEndpoint, int port) {
    super(socksEndpoint);
    ServerBootstrap bootstrap = new ServerBootstrap(
            new NioServerSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));

    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.receiveBufferSize", 1048576);
    bootstrap.setOption("child.sendBufferSize", 1048576);
    bootstrap.getPipeline().addLast("encode", new StringEncoder());
    bootstrap.getPipeline().addLast("decoder", new StringDecoder());
    bootstrap.getPipeline().addLast("echo", new ClientDataHandler());
    bootstrap.bind(new InetSocketAddress(port));
}

@Override
public boolean process(Exchange exchange, AsyncCallback arg1) {
    String msg = exchange.getIn().getBody(String.class);
    connectedClients.forEach((channel) -> channel.write(msg));
    return true;
}

class ClientDataHandler extends SimpleChannelUpstreamHandler {

    @Override
    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
            throws Exception {
        super.channelClosed(ctx, e);
        connectedClients.remove(ctx.getChannel());
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx,
            ChannelStateEvent e) throws Exception {
        super.channelConnected(ctx, e);
        connectedClients.add(ctx.getChannel());
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        Exchange exchange = getEndpoint().createExchange();
        exchange.getIn().setBody(e.getMessage());
        // try {
        // getProcessor().process(exchange);
        // } finally {
        // // log exception if an exception occurred and was not handled
        // if (exchange.getException() != null) {
        // getExceptionHandler().handleException(
        // "Error processing exchange", exchange,
        // exchange.getException());
        // }
        // }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        // Close the connection when an exception is raised.
        e.getCause().printStackTrace();
        log.error("", e.getCause());
        e.getChannel().close();
    }
}
}
公共类SocksProducer扩展了DefaultAsyncProducer{
private List connectedClients=new CopyOnWriteArrayList();
公共SocksProducer(SocksEndpoint SocksEndpoint,int端口){
超级(socksEndpoint);
ServerBootstrap bootstrap=新的ServerBootstrap(
新NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
setOption(“child.tcpNoDelay”,true);
setOption(“child.receiveBufferSize”,1048576);
setOption(“child.sendBufferSize”,1048576);
bootstrap.getPipeline().addLast(“encode”,新的StringEncoder());
bootstrap.getPipeline().addLast(“解码器”,新的StringDecoder());
bootstrap.getPipeline().addLast(“echo”,newclientdatahandler());
bind(新的InetSocketAddress(端口));
}
@凌驾
公共布尔进程(Exchange、异步回调arg1){
String msg=exchange.getIn().getBody(String.class);
connectedClients.forEach((channel)->channel.write(msg));
返回true;
}
类ClientDataHandler扩展了SimpleChannelUpstreamHandler{
@凌驾
公共无效channelClosed(ChannelHandlerContext ctx,ChannelStateEvent e)
抛出异常{
超级通道关闭(ctx,e);
connectedClients.remove(ctx.getChannel());
}
@凌驾
公共无效信道连接(ChannelHandlerContext ctx,
ChannelStateEvent(事件e)引发异常{
超级信道连接(ctx,e);
connectedClients.add(ctx.getChannel());
}
@凌驾
收到公共无效消息(ChannelHandlerContext ctx,MessageEvent e){
Exchange=getEndpoint().createExchange();
exchange.getIn().setBody(如getMessage());
//试一试{
//getProcessor().进程(交换);
//}最后{
////如果发生异常且未处理,则记录异常
//if(exchange.getException()!=null){
//getExceptionHandler().handleException(
//“错误处理交换”,交换,
//exchange.getException());
// }
// }
}
@凌驾
公共无效例外情况清单(ChannelHandlerContext ctx,例外情况e){
//引发异常时关闭连接。
e、 getCause().printStackTrace();
log.error(“,e.getCause());
e、 getChannel().close();
}
}
}

如果你有一本《骆驼在行动》的书,请阅读第10章第10.6节克劳斯,我想我必须买一本你的书:-)受到@ClausIbsen的启发,修订版什么时候出版?