224.6. 带有 sync=true 的示例

在下一个示例中,我们有一个更常见的用例,在端口 6201 上公开 TCP 服务也使用文本代码。但是,这次我们想要返回响应,因此我们将消费者的 同步 选项设置为 true

from("mina2:tcp://localhost:" + port2 + "?textline=true&sync=true").process(new Processor() {
    public void process(Exchange exchange) throws Exception {
        String body = exchange.getIn().getBody(String.class);
        exchange.getOut().setBody("Bye " + body);
    }
});

然后,我们通过使用 template.requestBody () 方法发送一些数据并检索响应来测试示例。随着我们知道的响应是 字符串,我们将其转换为 字符串,可以说在处理器代码逻辑中动态设置的响应是。

String response = (String)template.requestBody("mina2:tcp://localhost:" + port2 + "?textline=true&sync=true", "World");
assertEquals("Bye World", response);