이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 319. Thrift DataFormat
Available as of Camel version 2.20
Camel provides a Data Format to serialize between Java and the Apache Thrift . The project’s site details why you may wish to https://thrift.apache.org/. Apache Thrift is language-neutral and platform-neutral, so messages produced by your Camel routes may be consumed by other language implementations.
319.1. Thrift Options 링크 복사링크가 클립보드에 복사되었습니다!
The Thrift dataformat supports 3 options which are listed below.
Name | Default | Java Type | Description |
---|---|---|---|
instanceClass |
| Name of class to use when unarmshalling | |
contentTypeFormat |
|
| Defines a content type format in which thrift message will be serialized/deserialized from(to) the Java been. The format can either be native or json for either native binary thrift, json or simple json fields representation. The default value is binary. |
contentTypeHeader |
|
| Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc. |
319.2. Content type format 링크 복사링크가 클립보드에 복사되었습니다!
It’s possible to parse JSON message to convert it to the Thrift format and unparse it back using native util converter. To use this option, set contentTypeFormat value to 'json' or call thrift with second parameter. If default instance is not specified, always use native binary Thrift format. The simple JSON format is write-only (marshal) and produces a simple output format suitable for parsing by scripting languages. The sample code shows below:
from("direct:marshal") .unmarshal() .thrift("org.apache.camel.dataformat.thrift.generated.Work", "json") .to("mock:reverse");
from("direct:marshal")
.unmarshal()
.thrift("org.apache.camel.dataformat.thrift.generated.Work", "json")
.to("mock:reverse");
319.3. Thrift overview 링크 복사링크가 클립보드에 복사되었습니다!
This quick overview of how to use Thrift. For more detail see the complete tutorial
319.4. Defining the thrift format 링크 복사링크가 클립보드에 복사되었습니다!
The first step is to define the format for the body of your exchange. This is defined in a .thrift file as so:
tutorial.thrift
319.5. Generating Java classes 링크 복사링크가 클립보드에 복사되었습니다!
The Apache Thrift provides a compiler which will generate the Java classes for the format we defined in our .thrift file.
You can also run the compiler for any additional supported languages you require manually.
thrift -r --gen java -out ../java/ ./tutorial-dataformat.thrift
This will generate separate Java class for each type defined in .thrift file, i.e. struct or enum. The generated classes implement org.apache.thrift.TBase which is required by the serialization 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 org.apache.thrift.TBase.
319.6. Java DSL 링크 복사링크가 클립보드에 복사되었습니다!
You can use create the ThriftDataFormat instance and pass it to Camel DataFormat marshal and unmarshal API like this.
ThriftDataFormat format = new ThriftDataFormat(new Work()); from("direct:in").marshal(format); from("direct:back").unmarshal(format).to("mock:reverse");
ThriftDataFormat format = new ThriftDataFormat(new Work());
from("direct:in").marshal(format);
from("direct:back").unmarshal(format).to("mock:reverse");
Or use the DSL thrift() passing the unmarshal default instance or default instance class name like this.
319.7. Spring DSL 링크 복사링크가 클립보드에 복사되었습니다!
The following example shows how to use Thrift to unmarshal using Spring configuring the thrift data type
319.8. Dependencies 링크 복사링크가 클립보드에 복사되었습니다!
To use Thrift in your camel routes you need to add the a dependency on camel-thrift which implements this data format.