Camel provide a pluggable DataFormat for a wide range of
data models.
At the time of writing this post, below data formats are
supported by camel.
Standard JVM object marshalling
a.
Serialization
b.
String
Object marshalling
a.
Avro
b.
Boon
c.
Hessian
d.
JSON
e.
Protobuf
f.
YAML
Object/XML marshalling
a.
Castor
b.
JAXB
c.
XmlBeans
d.
XStream
e.
JiBX
f.
Jackson XML
Object/XML/Webservice marshalling
a.
SOAP
Direct JSON / XML marshalling
a.
XmlJson
Flat data structure marshalling
a.
BeanIO
b.
Bindy
c.
CSV
d.
EDI
e.
Flatpack DataFormat
f.
uniVocity-parsers formats
Domain specific marshalling
a.
HL7 DataFormat
Compression
a.
GZip data format
b.
Zip DataFormat
c.
Zip File DataFormat
d.
LZF Data Format
e.
Tar DataFormat
Security
a.
Crypto
b.
PGP
c.
XMLSecurity DataFormat
Misc.
a.
Base64
b.
Custom DataFormat - to use your own custom
implementation
c.
MIME-Multipart
d.
RSS
e.
TidyMarkup
f.
Syslog
g.
ICal
h.
Barcode - to read and generate barcodes
(QR-Code, PDF417, ...)
I will explain csv file marshalling example here. In
later posts, I will cover couple of data formats followed by custom data
format.
Let’s implement an application, such that it reads a csv
file that contains number of orders (each row of csv file represents an order),
we need to read each order and send them as list of orders to the jms queue.
I am going to use ActiveMQ for this tutorial.
Setup ActiveMQ
I would recommend you to go through my below post to
setup the activeMQ.
Once you setup activeMQ, loginto the admin console by
hitting below url.
Use below credentials to login to the admin console.
UserName: admin
Password: admin
You can see below kind of user interface.
Setup Eclipse Maven
Project
Create new maven project 'camelCSVDataFormat'. Project
structure looks like below.
Open pom.xml and update maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>camelCSVDataFormat</groupId> <artifactId>camelCSVDataFormat</artifactId> <version>1</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jms</artifactId> <version>2.22.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.22.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-csv --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-csv</artifactId> <version>2.22.1</version> </dependency> </dependencies> </project>
Create new package com.sample.app.routes and define FileCopyRoute.java.
FileCopyRoute.java
package com.sample.app.routes; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; public class FileCopyRoute extends RouteBuilder { @Override public void configure() throws Exception { String sourceFolder = "file:C:\\Users\\Public\\orders\\csvFiles"; String jmsOrderQ = "demoJMS:queue:orderQueue"; from(sourceFolder).unmarshal().csv().to(jmsOrderQ); from(jmsOrderQ).process(new Processor() { @Override public void process(Exchange exchange) throws Exception { String data = exchange.getIn().getBody(String.class); System.out.println("Received order : " + data); } }); } }
C:\\Users\\Public\\orders\\csvFiles contain one csv file
'orders.csv'.
orders.csv
order123,prod54,5
order123,prod768,1
order124,prod1,8
order125,prod1,2
Define class Application.java in package com.sample.app.
Application.java
package com.sample.app; import java.util.concurrent.TimeUnit; import javax.jms.ConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.camel.CamelContext; import org.apache.camel.component.jms.JmsComponent; import org.apache.camel.impl.DefaultCamelContext; import com.sample.app.routes.FileCopyRoute; public class Application { public static void main(String args[]) throws Exception { CamelContext context = new DefaultCamelContext(); ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); context.addComponent("demoJMS", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); context.addRoutes(new FileCopyRoute()); context.start(); TimeUnit.MINUTES.sleep(1); context.stop(); } }
unmarshal method read the csv file line by line and store
all lines in the message body as a java.util.List<List> type.
Run Application.java, you can see below messages in
console.
Received order : [[order123, prod54, 5], [order123,
prod768, 1], [order124, prod1, 8], [order125, prod1, 2]]
You can observe the same in activeMq queues window.
Total project structure looks like below.
No comments:
Post a Comment