mirror of
https://github.com/valitydev/sink-common-lib.git
synced 2024-11-06 02:15:23 +00:00
add serialization (#1)
This commit is contained in:
parent
a410ae5870
commit
d4554df3e5
78
.gitignore
vendored
Normal file
78
.gitignore
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
# Created by .ignore support plugin (hsz.mobi)
|
||||
.eunit
|
||||
deps
|
||||
*.o
|
||||
*.beam
|
||||
*.plt
|
||||
erl_crash.dump
|
||||
ebin/*.beam
|
||||
rel/example_project
|
||||
.concrete/DEV_MODE
|
||||
.rebar
|
||||
target/
|
||||
pom.xml.tag
|
||||
pom.xml.releaseBackup
|
||||
pom.xml.versionsBackup
|
||||
pom.xml.next
|
||||
release.properties
|
||||
dependency-reduced-pom.xml
|
||||
buildNumber.properties
|
||||
.mvn/timing.properties
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff:
|
||||
.idea/
|
||||
.idea/workspace.xml
|
||||
.idea/tasks.xml
|
||||
.idea/dictionaries
|
||||
.idea/vcs.xml
|
||||
.idea/jsLibraryMappings.xml
|
||||
|
||||
# Sensitive or high-churn files:
|
||||
.idea/dataSources.ids
|
||||
.idea/dataSources.xml
|
||||
.idea/dataSources.local.xml
|
||||
.idea/sqlDataSources.xml
|
||||
.idea/dynamic.xml
|
||||
.idea/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
.idea/gradle.xml
|
||||
.idea/libraries
|
||||
|
||||
# Mongo Explorer plugin:
|
||||
.idea/mongoSettings.xml
|
||||
|
||||
*.iws
|
||||
*.ipr
|
||||
*.iml
|
||||
|
||||
|
||||
# IntelliJ
|
||||
/out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
*.class
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
env.list
|
4
.gitmodules
vendored
Normal file
4
.gitmodules
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
[submodule "build_utils"]
|
||||
path = build_utils
|
||||
url = git@github.com:rbkmoney/build_utils.git
|
||||
branch = master
|
13
Jenkinsfile
vendored
Normal file
13
Jenkinsfile
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
#!groovy
|
||||
build('thrift-common-lib', 'docker-host') {
|
||||
checkoutRepo()
|
||||
loadBuildUtils()
|
||||
|
||||
def javaLibPipeline
|
||||
runStage('load JavaLib pipeline') {
|
||||
javaLibPipeline = load("build_utils/jenkins_lib/pipeJavaLib.groovy")
|
||||
}
|
||||
|
||||
def buildImageTag = "fcf116dd775cc2e91bffb6a36835754e3f2d5321"
|
||||
javaLibPipeline(buildImageTag)
|
||||
}
|
@ -1,2 +1,4 @@
|
||||
# eventsink-common-lib
|
||||
Библиотека с общими классами для взаимодействия с процессинговыми событиями eventsink/sinkievent из дамзеля и машингана
|
||||
# thrift-common-lib
|
||||
Библиотека для переиспользования вздаимодействия с протоколами, обрабатываемые machinegun'ном:
|
||||
- eventsink (hellgate)
|
||||
- sinkievent (machinegun)
|
||||
|
1
build_utils
Submodule
1
build_utils
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit ea4aa042f482551d624fd49a570d28488f479e93
|
102
pom.xml
Normal file
102
pom.xml
Normal file
@ -0,0 +1,102 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>parent</artifactId>
|
||||
<version>1.0.2</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<artifactId>thrift-common-lib</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>8</java.version>
|
||||
<lombok.version>1.18.4</lombok.version>
|
||||
<kafka.clients.version>2.1.0</kafka.clients.version>
|
||||
<damsel.version>1.278-3abdcb4</damsel.version>
|
||||
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
|
||||
<woody.thrift.version>1.1.15</woody.thrift.version>
|
||||
<damsel.version>1.291-02be09e</damsel.version>
|
||||
<machinegun-proto.version>1.12-ebae56f</machinegun-proto.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.25</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney.woody</groupId>
|
||||
<artifactId>woody-thrift</artifactId>
|
||||
<version>${woody.thrift.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- rbk -->
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>damsel</artifactId>
|
||||
<version>${damsel.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.rbkmoney</groupId>
|
||||
<artifactId>machinegun-proto</artifactId>
|
||||
<version>${machinegun-proto.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.benas</groupId>
|
||||
<artifactId>random-beans</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.2</version>
|
||||
<configuration>
|
||||
<destFile>${sonar.jacoco.reportPath}</destFile>
|
||||
<append>true</append>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>agent</id>
|
||||
<goals>
|
||||
<goal>prepare-agent</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,23 @@
|
||||
package com.rbkmoney.thrift.common.exception;
|
||||
|
||||
public class ParseException extends RuntimeException {
|
||||
|
||||
public ParseException() {
|
||||
}
|
||||
|
||||
public ParseException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ParseException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ParseException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ParseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.rbkmoney.thrift.common.exception;
|
||||
|
||||
public class ThriftBinaryDeserializationException extends RuntimeException {
|
||||
|
||||
public ThriftBinaryDeserializationException() {
|
||||
}
|
||||
|
||||
public ThriftBinaryDeserializationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ThriftBinaryDeserializationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ThriftBinaryDeserializationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ThriftBinaryDeserializationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.rbkmoney.thrift.common.exception;
|
||||
|
||||
public class ThriftBinarySerializationException extends RuntimeException {
|
||||
|
||||
public ThriftBinarySerializationException() {
|
||||
}
|
||||
|
||||
public ThriftBinarySerializationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ThriftBinarySerializationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ThriftBinarySerializationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ThriftBinarySerializationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.rbkmoney.thrift.common.parser;
|
||||
|
||||
public interface Parser<F, T> {
|
||||
|
||||
T parse(F data);
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.rbkmoney.thrift.common.parser.impl;
|
||||
|
||||
import com.rbkmoney.machinegun.eventsink.MachineEvent;
|
||||
import com.rbkmoney.thrift.common.exception.ParseException;
|
||||
import com.rbkmoney.thrift.common.parser.Parser;
|
||||
import com.rbkmoney.thrift.common.serialization.BinaryDeserializer;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class MachineEventParser<T> implements Parser<MachineEvent, T> {
|
||||
|
||||
private final BinaryDeserializer<T> deserializer;
|
||||
|
||||
@Override
|
||||
public T parse(MachineEvent data) {
|
||||
try {
|
||||
byte[] bin = data.getData().getBin();
|
||||
return deserializer.deserialize(bin);
|
||||
} catch (Exception e) {
|
||||
log.error("Exception when parse message e: ", e);
|
||||
throw new ParseException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.rbkmoney.thrift.common.parser.impl;
|
||||
|
||||
import com.rbkmoney.damsel.payment_processing.EventPayload;
|
||||
import com.rbkmoney.thrift.common.serialization.BinaryDeserializer;
|
||||
|
||||
public class PaymentEventPayloadMachineEventParser extends MachineEventParser<EventPayload> {
|
||||
|
||||
public PaymentEventPayloadMachineEventParser(BinaryDeserializer<EventPayload> deserializer) {
|
||||
super(deserializer);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.rbkmoney.thrift.common.parser.impl;
|
||||
|
||||
import com.rbkmoney.damsel.payout_processing.EventPayload;
|
||||
import com.rbkmoney.thrift.common.serialization.BinaryDeserializer;
|
||||
|
||||
public class PayoutEventPayloadMachineEventParser extends MachineEventParser<EventPayload> {
|
||||
|
||||
public PayoutEventPayloadMachineEventParser(BinaryDeserializer<EventPayload> deserializer) {
|
||||
super(deserializer);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.rbkmoney.thrift.common.serialization;
|
||||
|
||||
public interface BinaryDeserializer<T> {
|
||||
|
||||
T deserialize(byte[] bin);
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.rbkmoney.thrift.common.serialization;
|
||||
|
||||
public interface BinarySerializer<T> {
|
||||
|
||||
byte[] serialize(T data);
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.rbkmoney.thrift.common.serialization.impl;
|
||||
|
||||
import com.rbkmoney.thrift.common.exception.ThriftBinaryDeserializationException;
|
||||
import com.rbkmoney.thrift.common.serialization.BinaryDeserializer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.thrift.TBase;
|
||||
import org.apache.thrift.TDeserializer;
|
||||
import org.apache.thrift.TException;
|
||||
|
||||
@Slf4j
|
||||
public abstract class AbstractThriftBinaryDeserializer<T extends TBase> implements BinaryDeserializer<T> {
|
||||
|
||||
private final ThreadLocal<TDeserializer> thriftDeserializer = ThreadLocal.withInitial(TDeserializer::new);
|
||||
|
||||
protected T deserialize(byte[] bin, T t) {
|
||||
log.debug("Deserialize, bin[]: {}", bin.length);
|
||||
try {
|
||||
thriftDeserializer.get().deserialize(t, bin);
|
||||
} catch (TException e) {
|
||||
log.error("Error when deserialize data", e);
|
||||
throw new ThriftBinaryDeserializationException(e);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.rbkmoney.thrift.common.serialization.impl;
|
||||
|
||||
import com.rbkmoney.damsel.payment_processing.EventPayload;
|
||||
|
||||
public class PaymentEventPayloadDeserializer extends AbstractThriftBinaryDeserializer<EventPayload> {
|
||||
|
||||
@Override
|
||||
public EventPayload deserialize(byte[] bin) {
|
||||
return deserialize(bin, new EventPayload());
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.rbkmoney.thrift.common.serialization.impl;
|
||||
|
||||
import com.rbkmoney.damsel.payment_processing.EventPayload;
|
||||
|
||||
public class PaymentEventPayloadSerializer extends ThriftBinarySerializer<EventPayload> {
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.rbkmoney.thrift.common.serialization.impl;
|
||||
|
||||
import com.rbkmoney.damsel.payout_processing.EventPayload;
|
||||
|
||||
public class PayoutEventPayloadDeserializer extends AbstractThriftBinaryDeserializer<EventPayload> {
|
||||
|
||||
@Override
|
||||
public EventPayload deserialize(byte[] bin) {
|
||||
return deserialize(bin, new EventPayload());
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.rbkmoney.thrift.common.serialization.impl;
|
||||
|
||||
import com.rbkmoney.damsel.payment_processing.EventPayload;
|
||||
|
||||
public class PayoutEventPayloadSerializer extends ThriftBinarySerializer<EventPayload> {
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package com.rbkmoney.thrift.common.serialization.impl;
|
||||
|
||||
import com.rbkmoney.thrift.common.exception.ThriftBinarySerializationException;
|
||||
import com.rbkmoney.thrift.common.serialization.BinarySerializer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.thrift.TBase;
|
||||
import org.apache.thrift.TException;
|
||||
import org.apache.thrift.TSerializer;
|
||||
|
||||
@Slf4j
|
||||
public class ThriftBinarySerializer<T extends TBase> implements BinarySerializer<T> {
|
||||
|
||||
private final ThreadLocal<TSerializer> thriftSerializer = ThreadLocal.withInitial(TSerializer::new);
|
||||
|
||||
@Override
|
||||
public byte[] serialize(T data) {
|
||||
log.debug("Serialize, data: {}", data);
|
||||
try {
|
||||
return thriftSerializer.get().serialize(data);
|
||||
} catch (TException e) {
|
||||
log.error("Error when serialize data: {} ", data, e);
|
||||
throw new ThriftBinarySerializationException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.rbkmoney.thrift.common.parser;
|
||||
|
||||
import com.rbkmoney.damsel.payment_processing.EventPayload;
|
||||
import com.rbkmoney.machinegun.eventsink.MachineEvent;
|
||||
import com.rbkmoney.machinegun.msgpack.Value;
|
||||
import com.rbkmoney.thrift.common.parser.impl.PaymentEventPayloadMachineEventParser;
|
||||
import com.rbkmoney.thrift.common.serialization.impl.PaymentEventPayloadDeserializer;
|
||||
import com.rbkmoney.thrift.common.serialization.impl.PaymentEventPayloadSerializer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static io.github.benas.randombeans.api.EnhancedRandom.random;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ParserTest {
|
||||
|
||||
@Test
|
||||
public void parserTest() {
|
||||
EventPayload expectedEventPayload = EventPayload.invoice_changes(Collections.emptyList());
|
||||
|
||||
PaymentEventPayloadSerializer serializer = new PaymentEventPayloadSerializer();
|
||||
byte[] serializeEventPayload = serializer.serialize(expectedEventPayload);
|
||||
|
||||
MachineEvent machineEvent = random(MachineEvent.class, "data");
|
||||
machineEvent.setData(Value.bin(serializeEventPayload));
|
||||
|
||||
PaymentEventPayloadMachineEventParser parser = new PaymentEventPayloadMachineEventParser(new PaymentEventPayloadDeserializer());
|
||||
EventPayload actualEventPayload = parser.parse(machineEvent);
|
||||
|
||||
assertEquals(expectedEventPayload, actualEventPayload);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.rbkmoney.thrift.common.serialization;
|
||||
|
||||
import com.rbkmoney.damsel.payment_processing.EventPayload;
|
||||
import com.rbkmoney.thrift.common.serialization.impl.PaymentEventPayloadDeserializer;
|
||||
import com.rbkmoney.thrift.common.serialization.impl.PaymentEventPayloadSerializer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class SerializationTest {
|
||||
|
||||
@Test
|
||||
public void serializationTest() {
|
||||
EventPayload expectedEventPayload = EventPayload.invoice_changes(Collections.emptyList());
|
||||
|
||||
PaymentEventPayloadSerializer serializer = new PaymentEventPayloadSerializer();
|
||||
byte[] serializeEventPayload = serializer.serialize(expectedEventPayload);
|
||||
|
||||
PaymentEventPayloadDeserializer deserializer = new PaymentEventPayloadDeserializer();
|
||||
EventPayload actualEventPayload = deserializer.deserialize(serializeEventPayload);
|
||||
|
||||
assertEquals(expectedEventPayload, actualEventPayload);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user