@Test
 @SuppressWarnings({"unchecked", "rawtypes"})
 public void testProducerListenerSet() throws Exception {
   KafkaProducerContext producerContext = new KafkaProducerContext();
   Serializer serializer = Mockito.mock(Serializer.class);
   ProducerMetadata<Object, Object> producerMetadata =
       new ProducerMetadata<>("default", Object.class, Object.class, serializer, serializer);
   Producer producer = Mockito.mock(Producer.class);
   ProducerConfiguration<Object, Object> producerConfiguration =
       new ProducerConfiguration<>(producerMetadata, producer);
   ProducerListener producerListener = mock(ProducerListener.class);
   producerConfiguration.setProducerListener(producerListener);
   Map<String, ProducerConfiguration<?, ?>> producerConfigurations =
       Collections.<String, ProducerConfiguration<?, ?>>singletonMap(
           "default", producerConfiguration);
   producerContext.setProducerConfigurations(producerConfigurations);
   KafkaProducerMessageHandler handler = new KafkaProducerMessageHandler(producerContext);
   handler.handleMessage(
       MessageBuilder.withPayload("somePayload")
           .setHeader(KafkaHeaders.PARTITION_ID, 2)
           .setHeader(KafkaHeaders.MESSAGE_KEY, "someKey")
           .build());
   final ArgumentCaptor<Callback> argument = ArgumentCaptor.forClass(Callback.class);
   verify(producer).send(any(ProducerRecord.class), argument.capture());
   Callback callback = argument.getValue();
   assertThat(callback, CoreMatchers.instanceOf(ProducerListenerInvokingCallback.class));
   DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(callback);
   assertEquals(fieldAccessor.getPropertyValue("topic"), "default");
   assertEquals(fieldAccessor.getPropertyValue("partition"), 2);
   assertEquals(fieldAccessor.getPropertyValue("key"), "someKey");
   assertEquals(fieldAccessor.getPropertyValue("payload"), "somePayload");
   assertSame(fieldAccessor.getPropertyValue("producerListener"), producerListener);
   verifyNoMoreInteractions(producer);
 }
    @Override
    protected void handleMessageInternal(Message<?> message) throws Exception {
      int targetPartition;
      if (partitioningMetadata.isPartitionedModule()) {
        targetPartition = determinePartition(message, partitioningMetadata);
      } else {
        targetPartition = roundRobin() % numberOfKafkaPartitions;
      }

      if (Mode.embeddedHeaders.equals(mode)) {
        MessageValues transformed = serializePayloadIfNecessary(message);
        byte[] messageToSend =
            embeddedHeadersMessageConverter.embedHeaders(
                transformed, KafkaMessageChannelBinder.this.headersToMap);
        producerConfiguration.send(topicName, targetPartition, null, messageToSend);
      } else if (Mode.raw.equals(mode)) {
        Object contentType = message.getHeaders().get(MessageHeaders.CONTENT_TYPE);
        if (contentType != null && !contentType.equals(MediaType.APPLICATION_OCTET_STREAM_VALUE)) {
          logger.error(
              "Raw mode supports only "
                  + MediaType.APPLICATION_OCTET_STREAM_VALUE
                  + " content type"
                  + message.getPayload().getClass());
        }
        if (message.getPayload() instanceof byte[]) {
          producerConfiguration.send(
              topicName, targetPartition, null, (byte[]) message.getPayload());
        } else {
          logger.error(
              "Raw mode supports only byte[] payloads but value sent was of type "
                  + message.getPayload().getClass());
        }
      }
    }