Skip to content

Commit

Permalink
camel-jackson-avro data format native support
Browse files Browse the repository at this point in the history
Fixes #2610
  • Loading branch information
jamesnetherton committed Jun 9, 2021
1 parent c323779 commit d6a8ecf
Show file tree
Hide file tree
Showing 22 changed files with 474 additions and 90 deletions.
14 changes: 9 additions & 5 deletions docs/modules/ROOT/pages/reference/extensions/jackson-avro.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
= Avro Jackson
:linkattrs:
:cq-artifact-id: camel-quarkus-jackson-avro
:cq-native-supported: false
:cq-status: Preview
:cq-status-deprecation: Preview
:cq-native-supported: true
:cq-status: Stable
:cq-status-deprecation: Stable
:cq-description: Marshal POJOs to Avro and back using Jackson.
:cq-deprecated: false
:cq-jvm-since: 2.0.0
:cq-native-since: n/a
:cq-native-since: 2.0.0

[.badges]
[.badge-key]##JVM since##[.badge-supported]##2.0.0## [.badge-key]##Native##[.badge-unsupported]##unsupported##
[.badge-key]##JVM since##[.badge-supported]##2.0.0## [.badge-key]##Native since##[.badge-supported]##2.0.0##

Marshal POJOs to Avro and back using Jackson.

Expand All @@ -24,6 +24,10 @@ Please refer to the above link for usage and configuration details.

== Maven coordinates

https://code.quarkus.io/?extension-search=camel-quarkus-jackson-avro[Create a new project with this extension on code.quarkus.io, window="_blank"]

Or add the coordinates to your existing project:

[source,xml]
----
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page
:cq-artifact-id: camel-quarkus-jackson-avro
:cq-artifact-id-base: jackson-avro
:cq-native-supported: false
:cq-status: Preview
:cq-native-supported: true
:cq-status: Stable
:cq-deprecated: false
:cq-jvm-since: 2.0.0
:cq-native-since: n/a
:cq-native-since: 2.0.0
:cq-camel-part-name: avro-jackson
:cq-camel-part-title: Avro Jackson
:cq-camel-part-description: Marshal POJOs to Avro and back using Jackson.
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion extensions-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
<module>ignite</module>
<module>iota</module>
<module>irc</module>
<module>jackson-avro</module>
<module>jasypt</module>
<module>java-joor-dsl</module>
<module>jbpm</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson-avro</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
package org.apache.camel.quarkus.component.jackson.avro.deployment;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.pkg.steps.NativeBuild;
import org.apache.camel.quarkus.core.JvmOnlyRecorder;
import org.jboss.logging.Logger;

class JacksonAvroProcessor {
Expand All @@ -33,14 +29,4 @@ class JacksonAvroProcessor {
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

/**
* Remove this once this extension starts supporting the native mode.
*/
@BuildStep(onlyIf = NativeBuild.class)
@Record(value = ExecutionTime.RUNTIME_INIT)
void warnJvmInNative(JvmOnlyRecorder recorder) {
JvmOnlyRecorder.warnJvmInNative(LOG, FEATURE); // warn at build time
recorder.warnJvmInNative(FEATURE); // warn at runtime
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@
<modules>
<module>deployment</module>
<module>runtime</module>
<module>integration-test</module>
</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<properties>
<camel.quarkus.jvmSince>2.0.0</camel.quarkus.jvmSince>
<camel.quarkus.nativeSince>2.0.0</camel.quarkus.nativeSince>
</properties>

<dependencyManagement>
Expand All @@ -51,6 +52,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson-avro</artifactId>
Expand Down
1 change: 1 addition & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<module>influxdb</module>
<module>ipfs</module>
<module>jackson</module>
<module>jackson-avro</module>
<module>jackson-protobuf</module>
<module>jacksonxml</module>
<module>jaxb</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-build-parent-it</artifactId>
<artifactId>camel-quarkus-integration-tests</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../../../poms/build-parent-it/pom.xml</relativePath>
</parent>

<artifactId>camel-quarkus-jackson-avro-integration-test</artifactId>
<name>Camel Quarkus :: Avro Jackson :: Integration Test</name>
<artifactId>camel-quarkus-integration-test-jackson-avro</artifactId>
<name>Camel Quarkus :: Integration Tests :: Avro Jackson</name>
<description>Integration tests for Camel Quarkus Avro Jackson extension</description>

<dependencyManagement>
Expand All @@ -43,6 +42,10 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson-avro</artifactId>
Expand All @@ -65,6 +68,19 @@
</dependency>

<!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson-avro-deployment</artifactId>
Expand All @@ -79,4 +95,34 @@
</exclusions>
</dependency>
</dependencies>

<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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 org.apache.camel.quarkus.component.jackson.avro.it;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
import org.apache.avro.Schema;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.jackson.SchemaResolver;
import org.apache.camel.component.jackson.avro.JacksonAvroDataFormat;

@Path("/jackson-avro")
@ApplicationScoped
public class JacksonAvroResource {

@Inject
ProducerTemplate producerTemplate;

@Path("/marshal")
@POST
public Response avroJacksonMarshal(String message) {
Pojo pojo = new Pojo(message);
byte[] result = producerTemplate.requestBody("direct:marshal-pojo", pojo, byte[].class);
return Response.ok(result).build();
}

@Path("/marshal/list")
@POST
public Response avroJacksonMarshalForList(String message) {
List<Pojo> pojos = new ArrayList<>();
for (String text : message.split(" ")) {
pojos.add(new Pojo(text));
}
byte[] result = producerTemplate.requestBody("direct:marshal-pojo-list", pojos, byte[].class);
return Response.ok(result).build();
}

@Path("/unmarshal/{type}")
@POST
@Consumes("application/octet-stream")
@SuppressWarnings("unchecked")
public Response avroJacksonUnMarshal(@PathParam("type") String type, byte[] message) {
Response.ResponseBuilder builder = Response.ok();
String directUri = "direct:unmarshal-" + type;

if (type.equals("pojo") || type.equals("defined-dataformat")) {
Pojo result = producerTemplate.requestBody(directUri, message, Pojo.class);
builder.entity(result.getText());
} else if (type.equals("json-node")) {
JsonNode result = producerTemplate.requestBody(directUri, message, JsonNode.class);
builder.entity(result.at("/text").asText());
} else if (type.equals("pojo-list")) {
List<Pojo> result = producerTemplate.requestBody(directUri, message, List.class);
StringJoiner joiner = new StringJoiner(" ");
result.stream()
.map(Pojo::getText)
.forEach(joiner::add);
builder.entity(joiner.toString());
} else {
throw new IllegalArgumentException("Unknown unmarshal type: " + type);
}

return builder.build();
}

@Named
public SchemaResolver avroSchemaResolver() throws IOException {
return createSchemaResolver("/pojo.avsc");
}

@Named
public JacksonAvroDataFormat jacksonAvroDataFormat() {
JacksonAvroDataFormat dataFormat = new JacksonAvroDataFormat();
dataFormat.setAutoDiscoverObjectMapper(true);
dataFormat.setAutoDiscoverSchemaResolver(true);
dataFormat.setUnmarshalType(Pojo.class);
return dataFormat;
}

@Named
public JacksonAvroDataFormat jacksonAvroDataFormatForList() throws IOException {
SchemaResolver schemaResolver = createSchemaResolver("/pojo-list.avsc");
JacksonAvroDataFormat dataFormat = new JacksonAvroDataFormat();
dataFormat.setUnmarshalType(Pojo.class);
dataFormat.setUseList(true);
dataFormat.setSchemaResolver(schemaResolver);
return dataFormat;
}

@Named
public AvroMapper avroMapper() {
AvroMapper avroMapper = new AvroMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Pojo.class, new StringAppendingDeserializer());
avroMapper.registerModule(module);
return avroMapper;
}

private SchemaResolver createSchemaResolver(String schemaPath) throws IOException {
InputStream resource = JacksonAvroResource.class.getResourceAsStream(schemaPath);
Schema raw = new Schema.Parser().setValidate(true).parse(resource);
AvroSchema schema = new AvroSchema(raw);
return ex -> schema;
}
}

0 comments on commit d6a8ecf

Please sign in to comment.