Skip to content

Cannot applied correctly TcpConnectorInterceptor on sending with server connection #8609

Closed
@kazuki43zoo

Description

@kazuki43zoo

In what version(s) of Spring Integration are you seeing this issue?

5.5.17 (Probably 6.0.x line too)

Describe the bug

If define multiple TcpConnectionInterceptor, cannot applied correctly TcpConnectorInterceptor on sending a message with server connection. As actual result, it has been applied only the first interceptor(interceptor that wrapped the real TcpConnection). Probably c1aa9b7 's changed are affect this behavior.

To Reproduce

For example, When following beans are defined, characterAppendingInterceptor1 has been applied but characterAppendingInterceptor2 has not been applied on sending a message. As a side note, When receive a message, all interceptors are applied.

  @Bean
  @Order(1)
  public TcpConnectionInterceptorFactory characterAppendingInterceptor1() {
    return new CharacterAppendingInterceptor.Factory("1");
  }

  @Bean
  @Order(2) // This interceptor cannot applied on sending
  public TcpConnectionInterceptorFactory characterAppendingInterceptor2() {
    return new CharacterAppendingInterceptor.Factory("2");
  }

  @Bean
  public TcpConnectionInterceptorFactoryChain tcpConnectionInterceptorFactoryChain(
      List<TcpConnectionInterceptorFactory> tcpConnectionInterceptorFactories) {
    TcpConnectionInterceptorFactoryChain chain = new TcpConnectionInterceptorFactoryChain();
    chain.setInterceptor(tcpConnectionInterceptorFactories.toArray(new TcpConnectionInterceptorFactory[0]));
    return chain;
  }

  @Bean
  public AbstractServerConnectionFactory serverConnectionFactory(
      TcpConnectionInterceptorFactoryChain chain) {
    return Tcp.netServer(1234)
        .deserializer(TcpCodecs.crlf())
        .interceptorFactoryChain(chain)
        .get();
  }

Expected behavior

We expected all interceptors are applied on sending a message. As workaround, I add the following TcpConnectorInterceptor, it work fine as expected behavior.

// The patch interceptor for binding a connection that wrapped all interceptor to TcpSender(in this case TcpSendingMessageHandler)
public class PatchInterceptor extends TcpConnectionInterceptorSupport {

  private PatchInterceptor(ApplicationEventPublisher publisher) {
    super(publisher);
  }

  @Override public void addNewConnection(TcpConnection connection) {
    super.addNewConnection(connection);
    // Call addNewConnection of TcpSender with wrapped connection(interceptor)
    Optional.ofNullable(getSender()).ifPresent(x -> x.addNewConnection(getTheConnection()));
  }

  static class Factory extends ApplicationObjectSupport implements TcpConnectionInterceptorFactory {

    @Override public TcpConnectionInterceptorSupport getInterceptor() {
      return new PatchInterceptor(obtainApplicationContext());
    }

  }

}

Sample

Just wait a few time!! I will add the repro project in My GitHub repository at after.

How to reproduce:
Please run the SpiGh8609ApplicationTests#connectAndReceive.

FYI:

  • SpiGh8609ApplicationWithPatchTests : Tests for applying workaround patch interceptor
  • SpiGh8609ApplicationWithoutInterceptorTests : Tests for no interceptors

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions