spring-cloud-stream icon indicating copy to clipboard operation
spring-cloud-stream copied to clipboard

Duplicate @StreamListener exception when Implementing an interface

Open TarekSaid opened this issue 10 years ago • 8 comments

using:

  • spring-boot-starter-parent 1.4.1.RELEASE;
  • spring-cloud-dependencies Camden.RELEASE
  • spring-cloud-stream-dependencies 1.0.3.RELEASE
  • spring-cloud-starter-stream-rabbit

Given that I have the following interfaces:

public interface Iceberg {
    String INPUT = "input";

    @Input(Iceberg.INPUT)
    SubscribableChannel input();
}

public interface ISink {
    void sighted(String msg);
}

And the following implementation:

@EnableBinding(Iceberg.class)
public class Titanic implements ISink {
  @Override
  @StreamListener(Iceberg.INPUT)
  public void sighted(String msg) {
    System.out.println("Oh no! " + msg);
  }

Then I get org.springframework.beans.factory.BeanInitializationException: Duplicate @StreamListener mapping for 'input' on Titanic#sighted[1 args] already existing for Titanic#sighted[1 args]

But I should see on the console "Oh no! Iceberg sighted" when I send "Iceberg sighted".

TarekSaid avatar Oct 10 '16 22:10 TarekSaid

Depends what your Iceberg interface is. Do you have another @EnableBinding or a @StreamListener in your application ?

mbogoevici avatar Oct 10 '16 22:10 mbogoevici

I noticed that your component is both a configuration class (@EnableBinding is meta-annotated with @Configuration) and a bean. I would suggest separating the configuration from the bean definition

@EnableBinding(Iceberg.class) // move this from Titanic.class
public class TitanicConfiguration {

    @Bean   
    public ISink titanic() {
          result new Titanic();
    }
}

mbogoevici avatar Oct 10 '16 23:10 mbogoevici

@mbogoevici Oh, that might be it. I was following a tutorial in which they're all under a single SpringBootApplication main class.

TarekSaid avatar Oct 10 '16 23:10 TarekSaid

@mbogoevici nevermind, that wasn't it. Separating them doesn't work. Furthermore, If I remove the interface ISink and the @Override annotation, it works.

TarekSaid avatar Oct 10 '16 23:10 TarekSaid

@TarekSaid Could you check what is different between your project and

https://github.com/mbogoevici/gh-issues/tree/master/gh-683 ?

I've tried to reproduce this issue, but can't. Thanks!

mbogoevici avatar Oct 10 '16 23:10 mbogoevici

@mbogoevici hey, I guess what's different in my case is that it's a multi-maven project. Check out the repository https://github.com/TarekSaid/co.synergyspace, specifically the projects posts-api and posts-impl, interface IBusinessConsumer and class BusinessConsumer. If you remove the implementation or switch to @ServiceActivator(inputChannel = Sink.INPUT), it should clean, run the integration tests and install.

TarekSaid avatar Oct 11 '16 00:10 TarekSaid

Ok, thanks - I'll take a look. Seems like your project is indeed creating duplicate subscriptions on the input channel, but can't tell exactly what until I build this locally.

mbogoevici avatar Oct 11 '16 15:10 mbogoevici

@mbogoevici Hi, I haven't debugged the code so I don't know what's the root cause of the problem, but it seems it has to do with implementing a generic interface (such as public interface IBusinessConsumer<T extends Business>).

After I removed the generic parameter from the interface, it started working.

TarekSaid avatar Oct 13 '16 05:10 TarekSaid