org.apache.camel.dataformat.avro.AvroDataFormat Java Examples

The following examples show how to use org.apache.camel.dataformat.avro.AvroDataFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AvroDataFormatProducer.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Produces
AvroDataFormat produceAvroDataFormat(InjectionPoint injectionPoint) {
    Member member = injectionPoint.getMember();
    if (member instanceof Field) {
        Field field = (Field) member;
        if (!Modifier.isStatic(member.getModifiers()) && field.getAnnotation(BuildTimeAvroDataFormat.class) != null) {
            String injectedFieldId = member.getDeclaringClass().getName() + "." + member.getName();
            Schema schema = schemaRegistry.get(injectedFieldId);
            return new AvroDataFormat(schema);
        }
    }
    String message = "AvroDataFormat beans can only be injected into non-static field annotated with @";
    throw new IllegalArgumentException(message + BuildTimeAvroDataFormat.class.getName());
}
 
Example #2
Source File: AvroDataFormatTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarshalUnmarshal() throws Exception {

    DataFormat avro = new AvroDataFormat(getSchema());
    GenericRecord input = new GenericData.Record(getSchema());
    input.put("name", "Kermit");

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").marshal(avro).unmarshal(avro);
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        GenericRecord result = producer.requestBody("direct:start", input, GenericRecord.class);
        Assert.assertEquals("Kermit", result.get("name").toString());
    } finally {
        camelctx.close();
    }
}
 
Example #3
Source File: AvroRoute.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() {

    from("direct:marshalUsingBuildTimeAvroDataFormat").marshal(buildTimeAvroDataFormat);
    from("direct:unmarshalUsingBuildTimeAvroDataFormat").unmarshal(buildTimeAvroDataFormat);

    AvroDataFormat configureTimeAvroDataFormat = new AvroDataFormat(getSchema());
    from("direct:marshalUsingConfigureTimeAvroDataFormat").marshal(configureTimeAvroDataFormat);
    from("direct:unmarshalUsingConfigureTimeAvroDataFormat").unmarshal(configureTimeAvroDataFormat);

    from("direct:marshalUsingAvroDsl").marshal().avro();
    from("direct:unmarshalUsingInstanceClassNameAvroDsl").unmarshal().avro(Value.class.getName());
    from("direct:unmarshalUsingSchemaAvroDsl").unmarshal().avro(Value.SCHEMA$);
}
 
Example #4
Source File: AvroDataFormatAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Bean(name = "avro-dataformat-factory")
@ConditionalOnMissingBean(AvroDataFormat.class)
public DataFormatFactory configureAvroDataFormatFactory() throws Exception {
    return new DataFormatFactory() {
        @Override
        public DataFormat newInstance() {
            AvroDataFormat dataformat = new AvroDataFormat();
            if (CamelContextAware.class
                    .isAssignableFrom(AvroDataFormat.class)) {
                CamelContextAware contextAware = CamelContextAware.class
                        .cast(dataformat);
                if (contextAware != null) {
                    contextAware.setCamelContext(camelContext);
                }
            }
            try {
                Map<String, Object> parameters = new HashMap<>();
                IntrospectionSupport.getProperties(configuration,
                        parameters, null, false);
                CamelPropertiesHelper.setCamelProperties(camelContext,
                        dataformat, parameters, false);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            if (ObjectHelper.isNotEmpty(customizers)) {
                for (DataFormatCustomizer<AvroDataFormat> customizer : customizers) {
                    boolean useCustomizer = (customizer instanceof HasId)
                            ? HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.avro.customizer",
                                    ((HasId) customizer).getId())
                            : HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.avro.customizer");
                    if (useCustomizer) {
                        LOGGER.debug(
                                "Configure dataformat {}, with customizer {}",
                                dataformat, customizer);
                        customizer.customize(dataformat);
                    }
                }
            }
            return dataformat;
        }
    };
}