Skip to content

Commit

Permalink
Add typealias guides into README
Browse files Browse the repository at this point in the history
  • Loading branch information
chanhyeong committed Aug 28, 2024
1 parent 05e2552 commit 52ef79d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,41 @@ public class ExampleService {
}
```

## `typealias` extension

Eventeria provides the `typealias` extension to support following features.

- multiple data formats for single topic.
- publish the string of message type in fixed string, even if rename of class or relocate package.

When publishing `Message`, the message will contain `typealias` extension that you registered by `addSerializeTypeAlias` or `addCompatibleTypeAlias`. If not, full class name will be used.

When consuming `Message`, eventeria try to deserialize the message with `typealias` extension. You should register typealias by `addDeserializeTypeAlias` or `addCompatibleTypeAlias`.
If eventeria failed to find deserialize target type, it try to load class using ClassLoader with provided `typealias` string. If there is no matching class, consuming will be failed.

### Configuration Example

```java
@Bean
CloudEventMessageTypeAliasMapper cloudEventMessageTypeAliasMapper() {
CloudEventMessageTypeAliasMapper typeAliasMapper = new CloudEventMessageTypeAliasMapper();
// support typealias at both serialization / deserialization times
typeAliasMapper.addCompatibleTypeAlias(
PostCreatedEvent.class,
"com.navercorp.eventeria.guide.boot.domain.PostCreatedEvent"
);
// support typealias at serialization time only
typeAliasMapper.addSerializeTypeAlias(
SerializeOnlyTypeAliasPostCreatedEvent.class,
"com.navercorp.eventeria.guide.boot.domain.PostCreatedEventSerializeOnlyVersion"
);
return typeAliasMapper;
}
```

## License

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.navercorp.eventeria.guide.boot.domain.AfterPostCreationCommand.NotifyToSubscribers;
import com.navercorp.eventeria.guide.boot.domain.PostCreatedEvent;
import com.navercorp.eventeria.guide.boot.domain.SerializeOnlyTypeAliasPostCreatedEvent;
import com.navercorp.eventeria.messaging.contract.Message;
import com.navercorp.eventeria.messaging.contract.cloudevents.serializer.CloudEventMessageReaderWriter;
import com.navercorp.eventeria.messaging.contract.serializer.MessageSerializerDeserializer;
Expand All @@ -29,8 +30,6 @@
@RequiredArgsConstructor
public class MessageConfig {

private final ObjectMapper objectMapper;

static {
EventFormatProvider.getInstance()
.registerFormat(
Expand All @@ -40,6 +39,8 @@ public class MessageConfig {
);
}

private final ObjectMapper objectMapper;

@Bean
MessageSerializerDeserializer messageSerializerDeserializer() {
return new JacksonMessageSerializer(objectMapper);
Expand All @@ -54,6 +55,11 @@ CloudEventMessageTypeAliasMapper cloudEventMessageTypeAliasMapper() {
"com.navercorp.eventeria.guide.boot.domain.PostCreatedEvent"
);

typeAliasMapper.addSerializeTypeAlias(
SerializeOnlyTypeAliasPostCreatedEvent.class,
"com.navercorp.eventeria.guide.boot.domain.PostCreatedEventSerializeOnlyVersion"
);

typeAliasMapper.addCompatibleTypeAlias(
NotifyToSubscribers.class,
"com.navercorp.eventeria.guide.boot.domain.AfterPostCreationCommand$NotifyToSubscribers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public Post create(Post post) {
PostCreatedEvent.from(post)
);

springMessagePublisher.publish(
SerializeOnlyTypeAliasPostCreatedEvent.from(post)
);

return post;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,18 @@ public void publishAfterPostCreatedCommands(
)
);
}

public void publishAfterPostCreatedCommands(
SerializeOnlyTypeAliasPostCreatedEvent event
) {
log.info("[CONSUME][Programmatic][SerializeOnlyTypeAliasPostCreatedEvent] {}", event);

springMessagePublisher.publish(
List.of(
UpdateUserStatistic.of(event.getPostId(), event.getWriterId()),
RefreshPostRanking.from(event.getPostId()),
ApplySearchIndex.from(event.getPostId())
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.navercorp.eventeria.guide.boot.domain;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import com.navercorp.eventeria.messaging.contract.event.AbstractDomainEvent;

@Getter
@ToString(callSuper = true)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SerializeOnlyTypeAliasPostCreatedEvent extends AbstractDomainEvent {

private long postId;
private String writerId;

public SerializeOnlyTypeAliasPostCreatedEvent(long postId, String writerId) {
super(String.valueOf(postId));
this.postId = postId;
this.writerId = writerId;
}

public static SerializeOnlyTypeAliasPostCreatedEvent from(Post post) {
return new SerializeOnlyTypeAliasPostCreatedEvent(post.id(), post.writerId());
}

@Override
public String getSourceType() {
return Post.class.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.navercorp.eventeria.guide.boot.domain.PostCreatedEvent;
import com.navercorp.eventeria.guide.boot.domain.PostEventHandler;
import com.navercorp.eventeria.guide.boot.domain.SerializeOnlyTypeAliasPostCreatedEvent;
import com.navercorp.eventeria.messaging.contract.cloudevents.converter.CloudEventToMessageConverter;
import com.navercorp.eventeria.messaging.spring.cloud.stream.binding.InboundChannelBinder;
import com.navercorp.eventeria.messaging.spring.integration.channel.SpringMessageHandler;
Expand Down Expand Up @@ -98,6 +99,7 @@ MessagePayloadTypeRouter postEventRouter(
) {
return MessagePayloadTypeRouter.register(INBOUND_BEAN_NAME)
.route(PostCreatedEvent.class, postEventHandler::publishAfterPostCreatedCommands)
.route(SerializeOnlyTypeAliasPostCreatedEvent.class, postEventHandler::publishAfterPostCreatedCommands)
.done();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void registerPost() {
}

private void assertPublishedMessageCount() {
int expectedPostEventCount = 1;
int expectedPostEventCount = 1 + 1; // PostCreatedEvent + SerializeOnlyTypeAliasPostCreatedEvent
int expectedAfterPostCommandCount = 3;
int expectedNotifyCommandCount = 1;

Expand All @@ -88,7 +88,11 @@ private void assertPublishedMessageCount() {
throw new RuntimeException(e);
}

// PostCreatedEvent + SerializeOnlyTypeAliasPostCreatedEvent published
then(actualPostEventCounter.get()).isEqualTo(expectedPostEventCount);

// continuous command published for PostCreatedEvent only.
// Because failed to deserialize SerializeOnlyTypeAliasPostCreatedEvent
then(actualAfterPostCommandCounter.get()).isEqualTo(expectedAfterPostCommandCount);
then(actualNotifyCommandCounter.get()).isEqualTo(expectedNotifyCommandCount);
}
Expand Down

0 comments on commit 52ef79d

Please sign in to comment.