...
This will generate a single Java class named AddressBookProtos which contains inner classes for Person and AddressBook. Builders are also implemented for you. The generated classes implement com.google.protobuf.Message which is required by the serialisation mechanism. For this reason it important that only these classes are used in the body of your exchanges. Camel will throw an exception on route creation if you attempt to tell the Data Format to use a class that does not implement com.google.protobuf.Message. Use the generated builders to translate the data from any of your existing domain classes.
Java DSL
TODO
Spring DSL
...
You can use create the ProtobufDataFormat instance and pass it to Camel DataFormat marshal and unmarsha API like this.
Code Block |
---|
ProtobufDataFormat format = new ProtobufDataFormat(Person.getDefaultInstance());
from("direct:in").marshal(format);
from("direct:back").unmarshal(format).to("mock:reverse");
|
Or use the DSL protobuf() passing the unmarshal default instance or default instance class name like this.
Code Block |
---|
// You don't need to specify the default instance for protobuf marshaling
from("direct:marshal").marshal().protobuf();
from("direct:unmarshalA").unmarshal().
protobuf("org.apache.camel.dataformat.protobuf.generated.AddressBookProtos$Person").
to ("mock:reverse");
from("direct:unmarshalB").unmarshal().protobuf(Person.getDefaultInstance()).to("mock:reverse");
|
Spring DSL
The following example shows how to use Castor to unmarshal using Spring configuring the protobuf data type
Code Block |
---|
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:start"/>
<unmarshal>
<protobuf instanceClass="org.apache.camel.dataformat.protobuf.generated.AddressBookProtos$Person" />
</unmarshal>
<to uri="mock:result"/>
</route>
</camelContext>
|
Dependencies
To use Protobuf in your camel routes you need to add the a dependency on camel-protobuf which implements this data format.
If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).
Code Block |
---|
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-protobuf</artifactId>
<version>2.2.0</version>
</dependency>
|