103.9. 完成时关闭频道
当充当服务器时,有时希望在关闭频道时(例如,客户端转换已完成)。
您可以通过设置 endpoint 选项 disconnect=true 来完成此操作。
但是,您也可以根据如下消息指示 Camel:
要指示 Camel 关闭频道,您应该添加一个带有键 CamelNettyCloseChannelWhenComplete 设置为布尔值 true 的标头。
例如,以下示例会在将消息写入客户端后关闭频道:
from("netty:tcp://0.0.0.0:8080").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
exchange.getOut().setBody("Bye " + body);
// some condition which determines if we should close
if (close) {
exchange.getOut().setHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true);
}
}
});
添加自定义频道管道工厂,以全面控制创建的管道。