diff --git a/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/DefaultSerializer.java b/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/DefaultSerializer.java new file mode 100644 index 00000000..47ea9411 --- /dev/null +++ b/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/DefaultSerializer.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.openmessaging.samples.producer; + +import java.io.IOException; + +import io.openmessaging.api.serialization.Serializer; + + +public class DefaultSerializer implements Serializer { + + @Override + public byte[] serialize(String topic, T t) { + return new byte[0]; + } + + @Override + public void close() throws IOException { + // no ops + } +} diff --git a/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/GenericTransactionProducerApp.java b/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/GenericTransactionProducerApp.java index f19b296e..aecb74df 100644 --- a/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/GenericTransactionProducerApp.java +++ b/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/GenericTransactionProducerApp.java @@ -83,7 +83,7 @@ public void run() { MessageSample messageSample = new MessageSample("Bob"); Message genericMessage = producer.messageBuilder().withTopic("NS://topicA") - .withValue(messageSample).withKey("messageKey").withTags("TagA").build(); + .withValue(messageSample).withKey("messageKey").withTags("TagA").withSerializationType("json").build(); SendResult sendResult = producer.send(genericMessage, new GenericLocalTransactionExecuter() { @Override diff --git a/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/JsonSerializer.java b/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/JsonSerializer.java new file mode 100644 index 00000000..cd004174 --- /dev/null +++ b/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/JsonSerializer.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.openmessaging.samples.producer; + +import java.io.IOException; + +import io.openmessaging.api.serialization.Serializer; + +public class JsonSerializer implements Serializer { + + @Override + public byte[] serialize(String topic, T body) { + return new byte[0]; + } + + @Override + public void close() throws IOException { + // no ops + } +} diff --git a/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/MessageBuilderImpl.java b/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/MessageBuilderImpl.java index f4cb44ac..81c5bbb4 100644 --- a/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/MessageBuilderImpl.java +++ b/openmessaging-api-samples/src/main/java/io/openmessaging/samples/producer/MessageBuilderImpl.java @@ -18,9 +18,7 @@ import io.openmessaging.api.Message; import io.openmessaging.api.MessageBuilder; -import io.openmessaging.api.OMSBuiltinKeys; import io.openmessaging.api.serialization.Serializer; -import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -45,9 +43,7 @@ public class MessageBuilderImpl implements MessageBuilder { public MessageBuilderImpl(Properties properties) throws Exception { this.properties = properties; - Class clazz = Class.forName(properties.getProperty(OMSBuiltinKeys.ENDPOINT)); - Constructor constructor = (Constructor) clazz.getDeclaredConstructor(); - this.serializer = (Serializer) constructor.newInstance(); + this.serializer = new DefaultSerializer<>(); } @Override public MessageBuilder withTopic(String topic) { @@ -80,6 +76,13 @@ public MessageBuilderImpl(Properties properties) throws Exception { return this; } + @Override public MessageBuilder withSerializationType(String serializationType) { + if (serializationType.equalsIgnoreCase("json")) { + this.serializer = new JsonSerializer(); + } + return this; + } + @Override public String getTopic() { return this.topic; } diff --git a/openmessaging-api/src/main/java/io/openmessaging/api/MessageBuilder.java b/openmessaging-api/src/main/java/io/openmessaging/api/MessageBuilder.java index bdb5ea48..6ce6a11d 100644 --- a/openmessaging-api/src/main/java/io/openmessaging/api/MessageBuilder.java +++ b/openmessaging-api/src/main/java/io/openmessaging/api/MessageBuilder.java @@ -73,6 +73,14 @@ public interface MessageBuilder { */ MessageBuilder withValue(T t); + /** + * Used for serializing message + * + * @param serializationType serialization type + * @return {@link MessageBuilder} + */ + MessageBuilder withSerializationType(String serializationType); + /** * Get the topic which this {@code MessageBuilder} belongs to. *