-
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.
feat: 샵 좋아요 이벤트에 MessagePackSerializer, MessagePackDeserializer 추가
- Loading branch information
Showing
5 changed files
with
86 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
dependencies { | ||
implementation project(':bp-utils') | ||
implementation 'jakarta.validation:jakarta.validation-api:3.0.2' | ||
implementation 'org.msgpack:msgpack-core:0.9.9' | ||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2' | ||
implementation 'org.apache.kafka:kafka-clients:3.7.1' | ||
} |
32 changes: 32 additions & 0 deletions
32
...mmon/src/main/java/com/beuatify_project/bp_common/serializer/MessagePackDeserializer.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,32 @@ | ||
package com.beuatify_project.bp_common.serializer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.kafka.common.serialization.Deserializer; | ||
import org.msgpack.core.MessagePack; | ||
import org.msgpack.core.MessageUnpacker; | ||
|
||
public class MessagePackDeserializer<T> implements Deserializer<T> { | ||
|
||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
private final Class<T> targetClass; | ||
|
||
public MessagePackDeserializer(final Class<T> targetClass) { | ||
this.targetClass = targetClass; | ||
} | ||
|
||
@Override | ||
public T deserialize(String topic, byte[] data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(data)) { | ||
// JSON 문자열로 변환된 데이터를 다시 POJO로 역직렬화 | ||
String jsonString = unpacker.unpackString(); | ||
// JSON -> Object 변환 (Jackson 또는 Gson 사용 가능) | ||
return OBJECT_MAPPER.readValue(jsonString, targetClass); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error deserializing object from MessagePack", e); | ||
} | ||
} | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
bp-common/src/main/java/com/beuatify_project/bp_common/serializer/MessagePackSerializer.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,27 @@ | ||
package com.beuatify_project.bp_common.serializer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.kafka.common.serialization.Serializer; | ||
import org.msgpack.core.MessageBufferPacker; | ||
import org.msgpack.core.MessagePack; | ||
|
||
public class MessagePackSerializer<T> implements Serializer<T> { | ||
|
||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
@Override | ||
public byte[] serialize(final String s, final T data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
|
||
try (MessageBufferPacker packer = MessagePack.newDefaultBufferPacker()) { | ||
// Object를 JSON으로 변환 후 MessagePack으로 직렬화 | ||
packer.packString(OBJECT_MAPPER.writeValueAsString(data)); | ||
return packer.toByteArray(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to serialize object to MessagePack", e); | ||
} | ||
|
||
} | ||
} |
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