title: Spring Cloud Stream doesn't use Kafka channel binder to send a message
body: <p><strong>I'm trying to achieve following thing:</strong></p>
  
  <p>Use Spring Cloud Stream 2.1.3.RELEASE with Kafka binder to send a message to an input channel and achieve publish subscribe behaviour where every consumer will be notified and be able to handle a message sent to a Kafka topic.</p>
  
  <p>I understand that in Kafka if every consumer belongs to its own consumer group, will be able to read every message from a topic.
  In my case spring creates an anonymous unique consumer group to every instance of my spring boot application running. The spring boot application has only one stream listener configured to listen to the input channel.</p>
  
  <p><strong>Test example case:</strong></p>
  
  <p>Configured an example Spring Cloud Stream app with an input channel which is bound to a Kafka topic.
  Using a spring rest controller to send a message to the input channel expecting that message will be delivered to every spring boot application instance running.
  In both applications on startup I can see that kafka partition is assigned properly.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>However, when I send a message using output().send() then spring doesn't even send a message to the Kafka topic configured, instead, in the same thread it triggers @StreamListener method of the same application instance.</p>
  
  <p>During debugging I see that spring code has two handlers of the message. The StreamListenerMessageHandler and KafkaProducerMessageHandler.
  Spring simply chains them and if first handler ends with success then it will not even go further. StreamListenerMessageHandler simply invokes my @StreamListener method in the same thread and message never reaches Kafka.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Is this by design, and in that case why is that? How can I achieve behaviour mentioned at the beginning of the post?</p>
  
  <p>PS.
  If I use KafkaTemplate and @KafkaListener method then it works as I want. Message is sent to Kafka topic and both application instances receive message and handles it in Kafka listener annotated method.</p>
  
  <p><strong>Code:</strong></p>
  
  <p>The stream listener method is configured the following way:</p>
  
  <pre><code>@SpringBootApplication
  @EnableBinding(Processor.class)
  @EnableTransactionManagement
  public class ProcessorApplication {
  
  private Logger logger = 
  LoggerFactory.getLogger(this.getClass().getName());
  
  private PersonRepository repository;
  
  public ProcessorApplication(PersonRepository repository) {
  
      this.repository = repository;
  }
  
  public static void main(String[] args) {
      SpringApplication.run(ProcessorApplication.class, args);
  
  }
  
  @Transactional
  @StreamListener(Processor.INPUT)
  @SendTo(Processor.OUTPUT)
  public PersonEvent process(PersonEvent data) {
      logger.info("Received event={}", data);
      Person person = new Person();
      person.setName(data.getName());
  
      logger.info("Saving person={}", person);
  
      Person savedPerson = repository.save(person);
  
      PersonEvent event = new PersonEvent();
      event.setName(savedPerson.getName());
      event.setType("PersonSaved");
      logger.info("Sent event={}", event);
      return event;
     }
    }
  </code></pre>
  
  <p>Sending a message to the input channel:</p>
  
  <pre><code>@RestController()
  @RequestMapping("/persons")
  public class PersonController {
  
  @Autowired
  private Sink sink;
  
  
    @PostMapping("/events")
    public void createPerson(@RequestBody PersonEvent event) {
  
        sink.input().send(MessageBuilder.withPayload(event).build());
    }
  
  
  }
  </code></pre>
  
  <p>Spring Cloud Stream config:</p>
  
  <pre><code>spring:
    cloud.stream:
      bindings:
          output.destination: person-event-output
          input.destination: person-event-input
  </code></pre>