-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8d5ea8
commit 453be61
Showing
9 changed files
with
10,531 additions
and
83 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/hoangtien2k3/shopappbackend/components/MyKafkaListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.hoangtien2k3.shopappbackend.components; | ||
|
||
import com.hoangtien2k3.shopappbackend.models.Category; | ||
import com.hoangtien2k3.shopappbackend.utils.Const; | ||
import org.springframework.kafka.annotation.KafkaHandler; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
@KafkaListener(id = Const.KAFKA_ID, topics = {Const.KAFKA_TOPIC_INSERT_CATEGORY, Const.KAFKA_TOPIC_GET_ALL_CATEGORY}) | ||
public class MyKafkaListener { | ||
|
||
@KafkaListener(topics = Const.KAFKA_TOPIC_INSERT_CATEGORY) | ||
public void listenerCategory(Category category) { | ||
System.out.println("Received Category: " + category); | ||
} | ||
|
||
@KafkaListener(topics = Const.KAFKA_TOPIC_GET_ALL_CATEGORY) | ||
public void listenListOfCategory(List<Category> categories) { | ||
System.out.println("Received list of Category: " + categories); | ||
} | ||
|
||
@KafkaHandler(isDefault = true) | ||
public void unknown(Category category) { | ||
System.out.println("Received unknown: " + category); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
.../java/com/hoangtien2k3/shopappbackend/components/converters/CategoryMessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.hoangtien2k3.shopappbackend.components.converters; | ||
|
||
import com.hoangtien2k3.shopappbackend.models.Category; | ||
import org.springframework.kafka.support.converter.JsonMessageConverter; | ||
import org.springframework.kafka.support.mapping.DefaultJackson2JavaTypeMapper; | ||
import org.springframework.kafka.support.mapping.Jackson2JavaTypeMapper; | ||
|
||
import java.util.Collections; | ||
|
||
public class CategoryMessageConverter extends JsonMessageConverter { | ||
public CategoryMessageConverter() { | ||
super(); | ||
DefaultJackson2JavaTypeMapper typeMapper = new DefaultJackson2JavaTypeMapper(); | ||
typeMapper.setTypePrecedence(Jackson2JavaTypeMapper.TypePrecedence.TYPE_ID); | ||
typeMapper.addTrustedPackages("com.hoangtien2k3.shopappbackend"); | ||
typeMapper.setIdClassMapping(Collections.singletonMap("category", Category.class)); | ||
this.setTypeMapper(typeMapper); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/hoangtien2k3/shopappbackend/configurations/KafkaCofiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.hoangtien2k3.shopappbackend.configurations; | ||
|
||
import com.hoangtien2k3.shopappbackend.utils.Const; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.annotation.EnableKafka; | ||
import org.springframework.kafka.core.KafkaOperations; | ||
import org.springframework.kafka.listener.CommonErrorHandler; | ||
import org.springframework.kafka.listener.DeadLetterPublishingRecoverer; | ||
import org.springframework.kafka.listener.DefaultErrorHandler; | ||
import org.springframework.util.backoff.FixedBackOff; | ||
|
||
@Configuration | ||
@EnableKafka | ||
public class KafkaCofiguration { | ||
|
||
@Bean | ||
public CommonErrorHandler errorHandler(KafkaOperations<Object, Object> kafkaOperations) { | ||
return new DefaultErrorHandler( | ||
new DeadLetterPublishingRecoverer(kafkaOperations), new FixedBackOff(1000L, 2) | ||
); | ||
} | ||
|
||
@Bean | ||
public NewTopic insertACategoryTopic() { | ||
return new NewTopic(Const.KAFKA_TOPIC_INSERT_CATEGORY, 1, (short) 1); | ||
} | ||
|
||
@Bean | ||
public NewTopic getAllCategoryTopic() { | ||
return new NewTopic(Const.KAFKA_TOPIC_GET_ALL_CATEGORY, 1, (short) 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.