このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring Integration 6.5.3 を使用してください!

接続をテスト

In some scenarios, it can be useful to send some kind of health check request when a connection is first opened. One such scenario might be when using a TCP フェールオーバークライアント接続ファクトリ so that we can fail over if the selected server allowed a connection to be opened but reports that it is not healthy.

この機能をサポートするには、クライアント接続ファクトリに connectionTest を追加します。

/**
 * Set a {@link Predicate} that will be invoked to test a new connection; return true
 * to accept the connection, false the reject.
 * @param connectionTest the predicate.
 * @since 5.3
 */
public void setConnectionTest(@Nullable Predicate<TcpConnectionSupport> connectionTest) {
    this.connectionTest = connectionTest;
}

To test the connection, attach a temporary listener to the connection within the test. If the test fails, the connection is closed and an exception thrown. When used with the TCP フェールオーバークライアント接続ファクトリ , this triggers trying the next server.

サーバーからの最初の応答のみがテストリスナーに送られます。

次の例では、PING を送信するときにサーバーが PONG と応答した場合、サーバーは正常であると見なされます。

Message<String> ping = new GenericMessage<>("PING");
byte[] pong = "PONG".getBytes();
clientFactory.setConnectionTest(conn -> {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicBoolean result = new AtomicBoolean();
    conn.registerTestListener(msg -> {
        if (Arrays.equals(pong, (byte[]) msg.getPayload())) {
            result.set(true);
        }
        latch.countDown();
        return false;
    });
    conn.send(ping);
    try {
        latch.await(10, TimeUnit.SECONDS);
    }
    catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return result.get();
});