-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from musabbozkurt/adding-spring-cloud-stream-bi…
…nder-kafka spring-cloud-stream-binder-kafka is added
- Loading branch information
Showing
21 changed files
with
512 additions
and
4 deletions.
There are no files selected for viewing
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/mb/inventorymanagementservice/queue/QueueChannels.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,11 @@ | ||
package com.mb.inventorymanagementservice.queue; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class QueueChannels { | ||
|
||
public static final String INTERNAL_EVENT_PRODUCER = "internalEventProducer-in-0"; | ||
public static final String USER_CREATED_EVENT_PRODUCER = "userCreatedEvent-in-0"; | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/mb/inventorymanagementservice/queue/consumer/ConsumerConfig.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,45 @@ | ||
package com.mb.inventorymanagementservice.queue.consumer; | ||
|
||
import com.mb.inventorymanagementservice.queue.event.InternalEvent; | ||
import com.mb.inventorymanagementservice.queue.event.UserCreatedEvent; | ||
import com.mb.inventorymanagementservice.utils.Constants; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.Message; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class ConsumerConfig { | ||
|
||
private final ConsumerStrategyExecutor consumerStrategyExecutor; | ||
|
||
@Bean | ||
public Function<Flux<Message<InternalEvent>>, Flux<Message<InternalEvent>>> internalEventProducer() { | ||
return internalEvent -> internalEvent | ||
.map(internalEventMessage -> { | ||
InternalEvent internalEventMessagePayload = internalEventMessage.getPayload(); | ||
log.info("Received an internal event. internalEventProducer - event: {}, eventType: {}, eventTypeHeader: {}", | ||
internalEventMessagePayload, | ||
internalEventMessagePayload.getEventType(), | ||
internalEventMessage.getHeaders().get(Constants.EVENT_TYPE_HEADER_KEY)); | ||
return internalEventMessage; | ||
}); | ||
} | ||
|
||
@Bean | ||
public Consumer<Message<InternalEvent>> internalEventConsumer() { | ||
return message -> consumerStrategyExecutor.dispatchEvent(message.getPayload(), message.getHeaders()); | ||
} | ||
|
||
@Bean | ||
public Consumer<Message<UserCreatedEvent>> userCreatedEvent() { | ||
return message -> consumerStrategyExecutor.dispatchEvent(message.getPayload(), message.getHeaders()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/mb/inventorymanagementservice/queue/consumer/ConsumerStrategy.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,10 @@ | ||
package com.mb.inventorymanagementservice.queue.consumer; | ||
|
||
import com.mb.inventorymanagementservice.queue.event.Event; | ||
|
||
public interface ConsumerStrategy { | ||
|
||
void execute(Event event); | ||
|
||
boolean canExecute(Event event); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/mb/inventorymanagementservice/queue/consumer/ConsumerStrategyExecutor.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.mb.inventorymanagementservice.queue.consumer; | ||
|
||
import com.mb.inventorymanagementservice.queue.event.Event; | ||
import com.mb.inventorymanagementservice.utils.Constants; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.messaging.MessageHeaders; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class ConsumerStrategyExecutor { | ||
|
||
private final List<ConsumerStrategy> strategies; | ||
|
||
public void dispatchEvent(Event event, MessageHeaders messageHeaders) { | ||
log.info("Received an event. dispatchEvent - event: {}, eventType: {}, eventTypeHeader: {}", event, event.getEventType(), messageHeaders.get(Constants.EVENT_TYPE_HEADER_KEY)); | ||
for (ConsumerStrategy strategy : strategies) { | ||
if (strategy.canExecute(event)) { | ||
strategy.execute(event); | ||
return; | ||
} | ||
} | ||
log.warn("No suitable strategy found. dispatchEvent - event: {}", event); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...inventorymanagementservice/queue/consumer/internal/InternalEventConsumerStrategyImpl.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,24 @@ | ||
package com.mb.inventorymanagementservice.queue.consumer.internal; | ||
|
||
import com.mb.inventorymanagementservice.queue.consumer.ConsumerStrategy; | ||
import com.mb.inventorymanagementservice.queue.event.Event; | ||
import com.mb.inventorymanagementservice.queue.event.InventoryManagementServiceEventType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@AllArgsConstructor | ||
public class InternalEventConsumerStrategyImpl implements ConsumerStrategy { | ||
|
||
@Override | ||
public void execute(Event event) { | ||
log.info("Received an internal event. execute - event: {}", event); | ||
} | ||
|
||
@Override | ||
public boolean canExecute(Event event) { | ||
return InventoryManagementServiceEventType.INTERNAL_EVENT.equals(event.getEventType()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...entorymanagementservice/queue/consumer/internal/UserCreatedEventConsumerStrategyImpl.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,24 @@ | ||
package com.mb.inventorymanagementservice.queue.consumer.internal; | ||
|
||
import com.mb.inventorymanagementservice.queue.consumer.ConsumerStrategy; | ||
import com.mb.inventorymanagementservice.queue.event.Event; | ||
import com.mb.inventorymanagementservice.queue.event.InventoryManagementServiceEventType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@AllArgsConstructor | ||
public class UserCreatedEventConsumerStrategyImpl implements ConsumerStrategy { | ||
|
||
@Override | ||
public void execute(Event event) { | ||
log.info("Received a user created event. execute - event: {}", event); | ||
} | ||
|
||
@Override | ||
public boolean canExecute(Event event) { | ||
return InventoryManagementServiceEventType.USER_CREATED_EVENT.equals(event.getEventType()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/mb/inventorymanagementservice/queue/event/BaseEventDto.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.mb.inventorymanagementservice.queue.event; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import java.util.UUID; | ||
|
||
@Data | ||
@SuperBuilder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class BaseEventDto { | ||
|
||
@Builder.Default | ||
private UUID id = UUID.randomUUID(); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/mb/inventorymanagementservice/queue/event/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,6 @@ | ||
package com.mb.inventorymanagementservice.queue.event; | ||
|
||
public interface Event { | ||
|
||
EventType getEventType(); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/mb/inventorymanagementservice/queue/event/EventType.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,6 @@ | ||
package com.mb.inventorymanagementservice.queue.event; | ||
|
||
public interface EventType { | ||
|
||
String name(); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/mb/inventorymanagementservice/queue/event/InternalEvent.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,22 @@ | ||
package com.mb.inventorymanagementservice.queue.event; | ||
|
||
import lombok.*; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import java.util.UUID; | ||
|
||
@Data | ||
@SuperBuilder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@ToString(callSuper = true) | ||
@EqualsAndHashCode(callSuper = true) | ||
public class InternalEvent extends BaseEventDto implements Event { | ||
|
||
private UUID randomId; | ||
|
||
@Override | ||
public EventType getEventType() { | ||
return InventoryManagementServiceEventType.INTERNAL_EVENT; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...va/com/mb/inventorymanagementservice/queue/event/InventoryManagementServiceEventType.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,6 @@ | ||
package com.mb.inventorymanagementservice.queue.event; | ||
|
||
public enum InventoryManagementServiceEventType implements EventType { | ||
INTERNAL_EVENT, | ||
USER_CREATED_EVENT | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/mb/inventorymanagementservice/queue/event/UserCreatedEvent.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,21 @@ | ||
package com.mb.inventorymanagementservice.queue.event; | ||
|
||
import com.mb.inventorymanagementservice.data.entity.User; | ||
import lombok.*; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@Data | ||
@SuperBuilder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@ToString(callSuper = true) | ||
@EqualsAndHashCode(callSuper = true) | ||
public class UserCreatedEvent extends BaseEventDto implements Event { | ||
|
||
private User user; | ||
|
||
@Override | ||
public EventType getEventType() { | ||
return InventoryManagementServiceEventType.USER_CREATED_EVENT; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/mb/inventorymanagementservice/queue/producer/EventProducer.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,8 @@ | ||
package com.mb.inventorymanagementservice.queue.producer; | ||
|
||
import com.mb.inventorymanagementservice.queue.event.Event; | ||
|
||
public interface EventProducer { | ||
|
||
void publishEvent(String bindingName, Event event); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/mb/inventorymanagementservice/queue/producer/impl/EventProducerImpl.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,33 @@ | ||
package com.mb.inventorymanagementservice.queue.producer.impl; | ||
|
||
import com.mb.inventorymanagementservice.queue.event.Event; | ||
import com.mb.inventorymanagementservice.queue.producer.EventProducer; | ||
import com.mb.inventorymanagementservice.utils.Constants; | ||
import com.mb.inventorymanagementservice.utils.TransactionUtils; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cloud.stream.function.StreamBridge; | ||
import org.springframework.integration.support.MessageBuilder; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class EventProducerImpl implements EventProducer { | ||
|
||
private final StreamBridge streamBridge; | ||
|
||
@Override | ||
public void publishEvent(String bindingName, Event event) { | ||
Message<Event> message = MessageBuilder | ||
.withPayload(event) | ||
.setHeader(Constants.EVENT_TYPE_HEADER_KEY, event.getEventType()) | ||
.build(); | ||
|
||
TransactionUtils.onCommit(() -> { | ||
log.info("Publishing event in event producer. publishEvent - event: {}", event); | ||
streamBridge.send(bindingName, message); | ||
}); | ||
} | ||
} |
Oops, something went wrong.