Home > Enterprise >  Key and Value in ProducerFactory<K,​V> of Spring Boot Kafka
Key and Value in ProducerFactory<K,​V> of Spring Boot Kafka

Time:01-10

I'm newbie to Apache Kafka and Spring Boot. I see that almost class have same structure like ProducerFactory<K,​V>, KafkaTemplate<K, V>,... I try to read the docs, but it no explaintion, it just say that k - key and v - value. Someone tell me the meaningfulness of this Generic ?

CodePudding user response:

I'm not a great expert either, but being Generics they take the type you decide to give them, put like this they just act as placeholders.

Written like that they reminded me of maps and in fact I found this example on the net:

public ProducerFactory<String, String> producerFactory() {

        // set the producer properties
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers);
        properties.put(ProducerConfig.RETRIES_CONFIG, 0);
        properties.put(ProducerConfig.BATCH_SIZE_CONFIG, 4096);
        properties.put(ProducerConfig.LINGER_MS_CONFIG, 1);
        properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 40960);
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        return new DefaultKafkaProducerFactory<String, String>(properties);
    }

Credits

CodePudding user response:

You will need to understand Generics.

In its simple terms, by defining KafkaTemplate<K,V> they are allowing you to call it in anyway you prefer:

Example:

 private final KafkaTemplate<String, String> kafkaTemplate;
 private final KafkaTemplate<Long, String> kafkaTemplate;
 private final KafkaTemplate<String, Order> kafkaTemplate;

As you can see, the K (Key) can be anything, and V (value) can be anything.

CodePudding user response:

Reading a book or the official documentation is more helpful to understand the generics in java .

Starting with the example of generic class and trying to make it easy to understand , it is a class with variables types of Attributes , the exact type will be given later when you really use the class for example to create instances ..

class Person<T> {
private T t ;

public Person (T t){
this.t=t;
}
}

When you use the class Person you know that any instance (object) of type Person have an instance variable but the exact type of that attribute is not given yet , you should give it when you use the class , for example when you like to create an instance you should write like that :

new Person<Dog>(new Dog()); // like that your new Person have a Dog 

Creating another Person who have a Cat will be like that :

new Person<Cat>(new Cat()); // the other Person have a Cat

this is just a simple explanation but the subject need to read it from a book really .

CodePudding user response:

Having a look at the Generics Tutorial:

Type Parameter Naming Conventions

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S,U,V etc. - 2nd, 3rd, 4th types You'll see these names used throughout the Java SE API and the rest of this lesson.

But not only in Java tutorial, we can find these conventions through all of "conventional java development".

Further:

Invoking and Instantiating a Generic Type

To reference the generic Box class from within your code, you must perform a generic type invocation, which replaces T with some concrete value, such as Integer:

Box<Integer> integerBox;

You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument — Integer in this case — to the Box class itself.

Type Parameter and Type Argument Terminology: Many developers use the terms "type parameter" and "type argument" interchangeably, but these terms are not the same. When coding, one provides type arguments in order to create a parameterized type. Therefore, the T in Foo<T> is a type parameter and the String in Foo<String> f is a type argument. This lesson observes this definition when using these terms.

Like any other variable declaration, this code does not actually create a new Box object. It simply declares that integerBox will hold a reference to a "Box of Integer", which is how Box<Integer> is read.

An invocation of a generic type is generally known as a parameterized type.

To instantiate this class, use the new keyword, as usual, but place <Integer> between the class name and the parenthesis:

Box<Integer> integerBox = new Box<Integer>();

...

  •  Tags:  
  • Related