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

The following examples show how to use org.springframework.kafka.support.serializer.JsonDeserializer. 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: KafkaConfig.java    From Mastering-Distributed-Tracing with MIT License 7 votes vote down vote up
private ConsumerFactory<String, Message> consumerFactory() throws Exception {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(ConsumerConfig.CLIENT_ID_CONFIG, clientId());
    props.put(ConsumerConfig.GROUP_ID_CONFIG, app.name);
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 1000);
    props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
    props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");

    return new TracingConsumerFactory<>( //
            new DefaultKafkaConsumerFactory<String, Message>( //
                    props, //
                    new StringDeserializer(), //
                    new JsonDeserializer<>(Message.class)));
}
 
Example #2
Source File: KafKaConfig.java    From gpmall with Apache License 2.0 6 votes vote down vote up
@Bean
public KafKaRegisterSuccConsumerFactory kafKaRegisterSuccConsumerFactory(){
    Map<String,Object>  consumerProperties = kafkaProperties.buildProducerProperties();
    consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,kafkaProperties.getBootstrapServers());
    consumerProperties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

    consumerProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG,"default");
    consumerProperties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,false);//消费者的自动提交方式关闭
    // consumerProperties.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG,10);//在一个的轮训方法中,返回的最大记录数
    consumerProperties.put("spring.json.trusted.packages" ,"com.gpmall.user.dal.entitys,com.gpmall.user.dal.*");
    /*
    earliest:当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,从头开始消费
    latest: 当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,消费新产生的该分区下的数据
    none: topic各分区都存在已提交的offset时,从offset后开始消费;只要有一个分区不存在已提交的offset,则抛出异常
    */
    consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
    return new KafKaRegisterSuccConsumerFactory(consumerProperties);
}
 
Example #3
Source File: KafkaConsumerConfig.java    From SpringAll with MIT License 6 votes vote down vote up
@Bean
public ConsumerFactory<String, Message> consumerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(
            ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
            bootstrapServers);
    props.put(
            ConsumerConfig.GROUP_ID_CONFIG,
            consumerGroupId);
    props.put(
            ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,
            autoOffsetReset);
    // props.put(
    //         ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
    //         StringDeserializer.class);
    // props.put(
    //         ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
    //         StringDeserializer.class);
    return new DefaultKafkaConsumerFactory<>(
            props,
            new StringDeserializer(),
            new JsonDeserializer<>(Message.class));
}
 
Example #4
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 #5
Source File: KafkaConfiguration.java    From spring-examples with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ConsumerFactory<String, KMessage> consumerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaAddress);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
    props.put(JsonDeserializer.VALUE_DEFAULT_TYPE, KMessage.class);
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    return new DefaultKafkaConsumerFactory<>(props);
}
 
Example #6
Source File: KafkaConsumerConfig.java    From personal_book_library_web_project with MIT License 5 votes vote down vote up
public ConsumerFactory<String, MailContext> mailContextConsumerFactory() {

      Map<String, Object> props = new HashMap<>();
      props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, environment.getProperty("kafka.bootstrap.address"));
      props.put(ConsumerConfig.GROUP_ID_CONFIG, environment.getProperty("consumer.group.name"));
      
      return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), new JsonDeserializer<>(MailContext.class));
  }
 
Example #7
Source File: KafkaConfiguration.java    From eventeum with Apache License 2.0 5 votes vote down vote up
@Bean
public ConsumerFactory<String, EventeumMessage> eventeumConsumerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, settings.getBootstrapAddresses());
    props.put(ConsumerConfig.GROUP_ID_CONFIG, settings.getGroupId());
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, settings.getRequestTimeoutMsConfig());
    props.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, settings.getRetryBackoffMsConfig());
    if ("PLAINTEXT".equals(settings.getSecurityProtocol())) {
        configurePlaintextSecurityProtocol(props);
    }
    return new DefaultKafkaConsumerFactory<>(props, null, new JsonDeserializer<>(EventeumMessage.class));
}
 
Example #8
Source File: KafkaConfiguration.java    From eventeum with Apache License 2.0 5 votes vote down vote up
@Bean
public ConsumerFactory<Object, Object> defaultConsumerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, settings.getBootstrapAddresses());
    props.put(ConsumerConfig.GROUP_ID_CONFIG, settings.getGroupId());
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

    return new DefaultKafkaConsumerFactory<>(props, null, new JsonDeserializer<>(Object.class));
}
 
Example #9
Source File: KafkaConfiguration.java    From spring-rdbms-cdc-kafka-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Bean
public Map<String, Object> consumerConfigs() {
  return ImmutableMap.<String, Object>builder()
      .put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers)
      .put(ConsumerConfig.GROUP_ID_CONFIG, groupId)
      .put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class)
      .put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class)
      .put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")
      .put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false")
      .put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "10")
      .build();
}
 
Example #10
Source File: JsonConfiguration.java    From grussell-spring-kafka with Apache License 2.0 5 votes vote down vote up
@Bean
public ConsumerFactory<String, Foo> consumerFactory() {
	Map<String, Object> props = new HashMap<>();
	props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.configProperties.getBrokerAddress());
	props.put(ConsumerConfig.GROUP_ID_CONFIG, "s1pGroup");
	props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
	props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 100);
	props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 15000);
	JsonDeserializer<Foo> jsonDeserializer = new JsonDeserializer<>(Foo.class);
	return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), jsonDeserializer);
}
 
Example #11
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 #12
Source File: ReceiverConfig.java    From spring-kafka with MIT License 5 votes vote down vote up
@Bean
public Map<String, Object> consumerConfigs() {
  Map<String, Object> props = new HashMap<>();
  props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
  props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
  props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
  props.put(ConsumerConfig.GROUP_ID_CONFIG, "json");

  return props;
}
 
Example #13
Source File: KafkaConfiguration.java    From spring-rdbms-cdc-kafka-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Bean
public ConsumerFactory<String, DebeziumEvent> consumerFactory() {
  return new DefaultKafkaConsumerFactory<>(consumerConfigs(),
                                           new StringDeserializer(),
                                           new JsonDeserializer<>(DebeziumEvent.class));
}
 
Example #14
Source File: TopolologyTestDriverKafkaStreamsInventoryCountTests.java    From spring-cloud-stream-samples with Apache License 2.0 4 votes vote down vote up
private void configureDeserializer(Deserializer<?> deserializer, Class<?> keyDefaultType, Class<?> valueDefaultType, boolean isKey) {
    Map<String, Object> deserializerConfig = new HashMap<>();
    deserializerConfig.put(JsonDeserializer.KEY_DEFAULT_TYPE, keyDefaultType);
    deserializerConfig.put(JsonDeserializer.VALUE_DEFAULT_TYPE, valueDefaultType);
    deserializer.configure(deserializerConfig, isKey);
}
 
Example #15
Source File: ReceiverConfig.java    From spring-kafka with MIT License 4 votes vote down vote up
@Bean
public ConsumerFactory<String, Car> consumerFactory() {
  return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
      new JsonDeserializer<>(Car.class));
}
 
Example #16
Source File: KafkaConsumerConfig.java    From tutorials with MIT License 4 votes vote down vote up
public ConsumerFactory<String, Greeting> greetingConsumerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "greeting");
    return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), new JsonDeserializer<>(Greeting.class));
}