org.springframework.kafka.support.serializer.JsonSerializer Java Examples

The following examples show how to use org.springframework.kafka.support.serializer.JsonSerializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringBootKafkaStreamsInventoryCountTests.java    From spring-cloud-stream-samples with Apache License 2.0 6 votes vote down vote up
@BeforeEach
void setUp() {

    Map<String, Object> props = new HashMap<>();

    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, broker.getBrokersAsString());
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    AbstractInventoryUpdateEventGenerator eventGenerator = new
            KafkaTemplateInventoryUpdateEventGenerator(props, INPUT_TOPIC);
    setEventGenerator(eventGenerator);

    Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(GROUP_NAME, "true", broker);
    consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    consumerProps.put(ConsumerConfig.CLIENT_ID_CONFIG, "test");
    consumerProps.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 1000);
    consumerProps.put(JsonDeserializer.TRUSTED_PACKAGES, KafkaStreamsInventoryCountTests.class.getPackage().getName());
    consumerProps.put(JsonDeserializer.KEY_DEFAULT_TYPE, ProductKey.class);
    consumerProps.put(JsonDeserializer.VALUE_DEFAULT_TYPE, InventoryCountEvent.class);
    consumerProps.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, "false");
    cf = new DefaultKafkaConsumerFactory<>(consumerProps);

    consumer = cf.createConsumer(GROUP_NAME);
    consumer.subscribe(Collections.singleton(OUTPUT_TOPIC));
}
 
Example #2
Source File: KafkaConfiguration.java    From spring-examples with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ProducerFactory producerFactory() {
    Map<String, Object> config = new HashMap<>();
    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaAddress);
    config.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
    config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    return new DefaultKafkaProducerFactory(config);
}
 
Example #3
Source File: KafKaConfig.java    From gpmall with Apache License 2.0 5 votes vote down vote up
@Bean
public KafKaRegisterSuccProducerFactory kafKaRegisterSuccProducerFactory(){
    Map<String,Object>  producerProperties = kafkaProperties.buildProducerProperties();
    producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,kafkaProperties.getBootstrapServers());
    producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,JsonSerializer.class);
    producerProperties.put(ProducerConfig.ACKS_CONFIG,"-1");
    return new KafKaRegisterSuccProducerFactory(producerProperties);
}
 
Example #4
Source File: KafkaProducerConfig.java    From personal_book_library_web_project with MIT License 5 votes vote down vote up
@Bean
public ProducerFactory<String, MailContext> mailContextProducerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, environment.getProperty("kafka.bootstrap.address"));
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    configProps.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 5000);
    return new DefaultKafkaProducerFactory<>(configProps);
}
 
Example #5
Source File: KafkaConfiguration.java    From eventeum with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<String, EventeumMessage> eventeumProducerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, settings.getBootstrapAddresses());
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    configProps.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
    configProps.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, settings.getRequestTimeoutMsConfig());
    configProps.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, settings.getRetryBackoffMsConfig());
    configProps.put("retries", settings.getRetries());
    if ("PLAINTEXT".equals(settings.getSecurityProtocol())) {
        configurePlaintextSecurityProtocol(configProps);
    }
    return new DefaultKafkaProducerFactory<>(configProps);
}
 
Example #6
Source File: KafkaConfig.java    From Mastering-Distributed-Tracing with MIT License 5 votes vote down vote up
private ProducerFactory<String, Message> producerFactory() throws Exception {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(ProducerConfig.CLIENT_ID_CONFIG, clientId());
    ProducerFactory<String, Message> producer = //
            new DefaultKafkaProducerFactory<String, Message>(props, //
                    new StringSerializer(), //
                    new JsonSerializer<Message>());
    return new TracingProducerFactory<String, Message>(producer, tracer);
}
 
Example #7
Source File: KafkaProducerConfig.java    From SpringAll with MIT License 5 votes vote down vote up
@Bean
public ProducerFactory<String, Message> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(
            ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
            bootstrapServers);
    configProps.put(
            ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
            StringSerializer.class);
    configProps.put(
            ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
            JsonSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}
 
Example #8
Source File: KafkaChannelAutoConfiguration.java    From servicecomb-pack with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public KafkaMessagePublisher kafkaMessagePublisher() {
  Map<String, Object> map = Maps.newHashMap();
  map.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap_servers);
  map.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
  map.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
  map.put(ProducerConfig.RETRIES_CONFIG, retries);
  map.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
  map.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
  return new KafkaMessagePublisher(topic,
      new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(map)));
}
 
Example #9
Source File: KafkaMessageBrokerConfiguration.java    From piper with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<String, Object> producerFactory(ObjectMapper aObjectMapper, KafkaProperties aKafkaProperties) {
  return new DefaultKafkaProducerFactory<>(
      producerConfigs(aKafkaProperties),
      new StringSerializer(),
      new JsonSerializer<>(aObjectMapper));
}
 
Example #10
Source File: KafkaMessageBrokerConfiguration.java    From piper with Apache License 2.0 5 votes vote down vote up
@Bean
public Map<String, Object> producerConfigs(KafkaProperties aKafkaProperties) {
  Map<String, Object> props = aKafkaProperties.buildProducerProperties();

  props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
  props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);

  return props;
}
 
Example #11
Source File: JsonConfiguration.java    From grussell-spring-kafka with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<String, Foo> producerFactory() {
	Map<String, Object> props = new HashMap<>();
	props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, this.configProperties.getBrokerAddress());
	props.put(ProducerConfig.RETRIES_CONFIG, 0);
	props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
	props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
	props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
	props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
	props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
	return new DefaultKafkaProducerFactory<>(props);
}
 
Example #12
Source File: KafkaStreamsInventoryCountTests.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void init(EmbeddedKafkaBroker embeddedKafka) {
    Map<String, Object> props = new HashMap<>();

    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, embeddedKafka.getBrokersAsString());
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    setEventGenerator(new KafkaTemplateInventoryUpdateEventGenerator(props, INPUT_TOPIC));

    Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(GROUP_NAME, "true", embeddedKafka);
    consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    consumerProps.put(ConsumerConfig.CLIENT_ID_CONFIG, "test");
    consumerProps.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 1000);
    consumerProps.put(JsonDeserializer.TRUSTED_PACKAGES, KafkaStreamsInventoryCountTests.class.getPackage().getName());
    consumerProps.put(JsonDeserializer.KEY_DEFAULT_TYPE, ProductKey.class);
    consumerProps.put(JsonDeserializer.VALUE_DEFAULT_TYPE, InventoryCountEvent.class);
    consumerProps.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, "false");
    cf = new DefaultKafkaConsumerFactory<>(consumerProps);


    /*
     * Disabling caching makes the test run faster, and more consistent behavior with the TopologyTestDriver.
     * More messages are produced on the output topic.
     */
    context = new SpringApplicationBuilder(KafkaStreamsInventoryCountApplication.class)
            .properties(
                    "spring.cloud.stream.kafka.streams.binder.brokers=" + embeddedKafka.getBrokersAsString(),
                    "spring.cloud.stream.kafka.streams.binder.configuration.commit.interval.ms=1000",
                    "spring.cloud.stream.kafka.streams.binder.configuration.cache.max.bytes.buffering=0")
            .run();
}
 
Example #13
Source File: SenderConfig.java    From spring-kafka with MIT License 5 votes vote down vote up
@Bean
public Map<String, Object> producerConfigs() {
  Map<String, Object> props = new HashMap<>();
  props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
  props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
  props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);

  return props;
}
 
Example #14
Source File: KafkaProducerConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ProducerFactory<String, Greeting> greetingProducerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}