Search in sources :

Example 1 with AvroDataFormat

use of org.apache.camel.dataformat.avro.AvroDataFormat in project wildfly-camel by wildfly-extras.

the class AvroDataFormatTest method testMarshalUnmarshal.

@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.stop();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) AvroDataFormat(org.apache.camel.dataformat.avro.AvroDataFormat) AvroDataFormat(org.apache.camel.dataformat.avro.AvroDataFormat) DataFormat(org.apache.camel.spi.DataFormat) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with AvroDataFormat

use of org.apache.camel.dataformat.avro.AvroDataFormat in project camel by apache.

the class AvroDataFormatAutoConfiguration method configureAvroDataFormatFactory.

@Bean(name = "avro-dataformat-factory")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(AvroDataFormat.class)
public DataFormatFactory configureAvroDataFormatFactory(final CamelContext camelContext, final AvroDataFormatConfiguration configuration) {
    return new DataFormatFactory() {

        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);
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), dataformat, parameters);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            return dataformat;
        }
    };
}
Also used : DataFormatFactory(org.apache.camel.spi.DataFormatFactory) CamelContextAware(org.apache.camel.CamelContextAware) HashMap(java.util.HashMap) AvroDataFormat(org.apache.camel.dataformat.avro.AvroDataFormat) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

AvroDataFormat (org.apache.camel.dataformat.avro.AvroDataFormat)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 CamelContext (org.apache.camel.CamelContext)1 CamelContextAware (org.apache.camel.CamelContextAware)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 DataFormat (org.apache.camel.spi.DataFormat)1 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)1 Test (org.junit.Test)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1