-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/79 auto register event classes (#80)
* Add @event annotation * Add EventClassRegister * Add @event annotations on event DTO classes * Combine KafkaConfigurations for proucer and consumer * Update docstrings
- Loading branch information
1 parent
3364676
commit 416930d
Showing
46 changed files
with
270 additions
and
295 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/application/bill/event/BillCreated.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
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/application/bill/event/BillDeleted.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
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/application/bill/event/BillPayed.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
36 changes: 36 additions & 0 deletions
36
bill/src/main/java/com/tungstun/bill/port/messaging/config/KafkaConfig.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,36 @@ | ||
package com.tungstun.bill.port.messaging.config; | ||
|
||
|
||
import com.tungstun.common.messaging.KafkaConfigBase; | ||
import com.tungstun.common.messaging.KafkaMessageProducer; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
import org.springframework.kafka.config.KafkaListenerContainerFactory; | ||
import org.springframework.kafka.listener.CommonLoggingErrorHandler; | ||
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer; | ||
|
||
@Configuration | ||
public class KafkaConfig extends KafkaConfigBase { | ||
private static final String TOPIC = "bill"; | ||
|
||
@Bean | ||
public NewTopic bill() { | ||
return new NewTopic(TOPIC, 1, (short) 1); | ||
} | ||
|
||
@Bean | ||
public KafkaMessageProducer kafkaMessageProducer() { | ||
return createMessageProducer(TOPIC); | ||
} | ||
|
||
@Bean | ||
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> | ||
kafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||
factory.setCommonErrorHandler(new CommonLoggingErrorHandler()); | ||
factory.setConsumerFactory(defaultConsumerFactory()); | ||
return factory; | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
bill/src/main/java/com/tungstun/bill/port/messaging/config/KafkaConsumerConfig.java
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
bill/src/main/java/com/tungstun/bill/port/messaging/config/KafkaProducerConfig.java
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/port/messaging/in/person/message/PersonCreated.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
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/port/messaging/in/person/message/PersonDeleted.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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package com.tungstun.bill.port.messaging.in.person.message; | ||
|
||
import com.tungstun.common.messaging.Event; | ||
|
||
@Event | ||
public record PersonDeleted( | ||
Long id) { | ||
} |
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/port/messaging/in/person/message/PersonUpdated.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
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/port/messaging/in/product/message/ProductCreated.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
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/port/messaging/in/product/message/ProductDeleted.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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package com.tungstun.bill.port.messaging.in.product.message; | ||
|
||
import com.tungstun.common.messaging.Event; | ||
|
||
@Event | ||
public record ProductDeleted( | ||
Long id) { | ||
} |
3 changes: 3 additions & 0 deletions
3
bill/src/main/java/com/tungstun/bill/port/messaging/in/product/message/ProductUpdated.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
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
16 changes: 16 additions & 0 deletions
16
common/src/main/java/com/tungstun/common/messaging/Event.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,16 @@ | ||
package com.tungstun.common.messaging; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Methods annotated with {@code @Event} are registered for serialization and deserialization of the class. | ||
*/ | ||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
@Documented | ||
@Bean | ||
public @interface Event { | ||
} |
46 changes: 46 additions & 0 deletions
46
common/src/main/java/com/tungstun/common/messaging/EventClassRegister.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,46 @@ | ||
package com.tungstun.common.messaging; | ||
|
||
import org.reflections.Reflections; | ||
import org.springframework.kafka.annotation.KafkaHandler; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Static function class that contains a set of all classes used as event DTO's. | ||
*/ | ||
public class EventClassRegister { | ||
private static Set<Class<?>> eventClasses; | ||
|
||
/** | ||
* Reflectively gets and registers all event DTO classes if eventClasses has not been initialized yet. | ||
* Method gets all classes annotated with {@code @Event} and all parameter classes annotated with {@code @KafkaHandler} | ||
* | ||
* @return Set of event classes. | ||
*/ | ||
public static Set<Class<?>> getEventClasses() { | ||
if (eventClasses == null) { | ||
eventClasses = new Reflections("com.tungstun") | ||
.getTypesAnnotatedWith(Event.class); | ||
|
||
new Reflections("com.tungstun") | ||
.getSubTypesOf(KafkaMessageConsumer.class) | ||
.parallelStream() | ||
.flatMap(consumerClass -> Stream.of(consumerClass.getDeclaredMethods())) | ||
.filter(consumerMethod -> Arrays | ||
.stream(consumerMethod.getDeclaredAnnotations()) | ||
.map(Annotation::annotationType) | ||
.anyMatch(annotation -> annotation.equals(KafkaHandler.class))) | ||
.flatMap(consumerMethod -> Stream.of(consumerMethod.getParameterTypes())) | ||
.forEach(eventClasses::add); | ||
} | ||
return Collections.unmodifiableSet(eventClasses); | ||
} | ||
|
||
private EventClassRegister() { | ||
// Static class, there is no reason to construct an instance | ||
} | ||
} |
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.