39.8. 完成后关闭频道
当充当服务器时,有时您想要在频道后关闭该频道,例如,客户端转换已完成。
只需设置 endpoint 选项 disconnect=true 即可完成此操作。
但是,您也可以在每条消息上指示 Camel,如下所示:
要指示 Camel 关闭频道,您应该添加一个将密钥 CamelNettyCloseChannelWhenComplete 设置为布尔值 true 的标头。
例如,以下示例会在将 bye 消息写回客户端后关闭该频道:
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);
}
}
});
添加自定义频道管道工厂以完全控制所创建的管道。