My 2nd Camel Ride

  • After 1st Camel ride, you should have a running camel route, please stop it.

  • Now we are going to reformat the xml file to object, then object to JSON as output file format

  • Create a POJO named "TradeOrder"

Right click in project explore, click "new"  and click on "class" on the panel

package org.blogdemo.example;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class TradeOrder implements Serializable {
 private static final long serialVersionUID = -2812468154835390309L;
 String name;
 String custId;
 Integer vip;
 String stockId;
 Integer shares;
  Double cost;
  public String getName() {return name;}
 public void setName(String name) {this.name = name;}
  public String getCustId() {return custId;}
  public void setCustId(String custId) {this.custId = custId;}
  
public Integer getVip() {return vip;}
public void setVip(Integer vip) {this.vip = vip;}
public String getStockId() {return stockId;}
public void setStockId(String stockId) {this.stockId =stockId;}
public Integer getShares() {return shares;}
public void setShares(Integer shares) {this.shares = shares;}
public Double getCost() {return cost;}
public void setCost(Double cost) {this.cost = cost;}
public static long getSerialversionuid() {
  return serialVersionUID;
}
public String toString(){
  return "name:["+name+"] custId:["+custId+"] vip:["+vip+"] stockId:["+stockId+"] shares["+shares+"]";
}
}

Add a file that indicates the input format

Create a fold by right click on example project in project explorer, click on "New" and choose "Folder"

Insert folder name as : xsdfiles

Right click on the "xsdfiles" folder, choose "New" and then click on "File"

Name the file as : stocktrading.xsd

And enter the following content:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="stocktrading">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="name"/>
        <xs:element type="xs:string" name="custId"/>
        <xs:element type="xs:byte" name="vip"/>
        <xs:element type="xs:string" name="stockId"/>
        <xs:element type="xs:byte" name="shares"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Modify the route to 

  • File Endpoint
    • Directory Name: myxml
    • Delete:true
  • Log Endpoint
    • Message: ${in.header.CamelFileName} with content -> ${body}
  • SetHeader
    • headerName: CamelFileName
    • language: simple
    • expression: ${date:now:yyyyMMddhhmmss}-read.xml

 

REMOVE:

  • File Endpoint
    • Directory Name: donexml 

Transform XML file to POJO for further processing:

  •  Data Transformation                                                                                                         
    • Transform ID: transOrder
    • Dozer file path: transOrder.xml 
    • Source Type: XML
    • Target Type: Java

 

Configure source type of the file:

  • XML Type Definition: XML Schema
  • Source File: xsdfiles/stocktrading.xsd

Configure Target Type:

  • Target class: org.blogdemo.example.TradOrder
  • Log Endpoint
    • Message:${body.name} has brought Stock ${body.stockId} with shares ${body.shares}

  • Marshal (JSON)
    • Unmarshal Type Name: org.blogdemo.example.TradeOrder
    • Library: Jackson
    • PrettyPrint: true

 

 

  • Log Endpoint                        
    • Message: JSON ${body}
  •  File Endpoint
    • uri: file://donexml                                                                                                       

 

Add the dependencies to your pom.xml

    <dependency>

         <groupId>org.apache.camel</groupId>

         <artifactId>camel-jackson</artifactId>

          <version>2.15.1.redhat-620133</version>

     </dependency>

Remove the un-needed dependencies

    <dependency> 

      <groupId>org.apache.camel</groupId> 

      <artifactId>camel-test-blueprint</artifactId> 

      <version>2.15.1.redhat-620133</version> 

    </dependency> 

Now you can start running it!

APPENDIX - order01.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Red Hat Stock Exchange and Trading Systems-->
<stocktrading>
    <name>Christina Lin</name>
    <custId>A123456789</custId>
    <vip>1</vip>
    <stockId>XYZ</stockId>
    <shares>11</shares>        
</stocktrading>

APPENDIX - order02.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Red Hat Stock Exchange and Trading Systems-->
<stocktrading>
    <name>Frank Jarrod</name>
    <custId>F475678678</custId>
    <vip>4</vip>
    <stockId>ABC</stockId>
    <shares>20</shares>        
</stocktrading>

APPENDIX - order03.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Red Hat Stock Exchange and Trading Systems-->
<multiple>
    <stocktrading>
        <name>John Osborn</name>
        <custId>K34567854</custId>
        <vip>1</vip>
        <stockId>KUJ</stockId>
        <shares>25</shares>        
    </stocktrading>
    <stocktrading>
        <name>Sarah Fox</name>
        <custId>C3456767878</custId>
        <vip>5</vip>
        <stockId>SOU</stockId>
        <shares>10</shares>        
    </stocktrading>
</multiple>

2nd Camel Ride completed!

Made with Slides.com