My 4th Camel Ride
Christina Lin
Now we are going to actually calculate the actual cost for the shares, create a Java bean by right click on the org.blogdemo.example under src/main/java. Select "New", "Class".
And enter "MyBean" as the name of your class.
package org.blogdemo.example;
import java.util.HashMap;
public class MyBean {
HashMap<String, Double> stockprice = new HashMap<String, Double>();
public MyBean(){stockpriceSetup();}
public TradeOrder calculateCost(TradeOrder order){
order.setCost(stockprice.get(order.getStockId())*order.getShares());
System.out.println("Helloe! :["+order.getCost()+"]");
return order;
}
private void stockpriceSetup(){
stockprice.put("ABC", 10.55);
stockprice.put("XYZ", 33.70);
stockprice.put("KUJ", 100.20);
stockprice.put("SOU", 3.50);
}
}
Register your bean by adding it to your camel context
<bean id="costAdvisor" class="org.blogdemo.example.MyBean" />
Reference the bean that does calculation in your route by removing the links between data transformation and choice
DO NOT SAVE YET !!!!
- File Endpoint
- uri: file://myxml?delete=true
- Log Endpoint
- Message: ${in.header.CamelFileName} with content -> ${body}
- Data Transformation
- .......
- Bean
- Method: calculateCost
- Bean Name: costAdvisor
Link the new bean node in between ref:transOrder and choice nodes.
SAVE.
- Choice
- When
- language: simple
- expression ${body.vip} >= 3
- SetHeader
- headerName: CamelFileName
- language: simple
- expression: ${date:now:yyyyMMddhhmmss}-read.json
- Marshal
- Libaray :Jackson
- marshalTypeName: org.blogdemo.example.TradeOrder
- End Point
- uri: file://donexml
- When
Log
Log
-
- Otherwise
- SetHeader
- headerName: CamelFileName
- language: simple
- expression: ${date:now:yyyyMMddhhmmss}-read.xml
-
Convert
- type: java.lang.String
- File Endpoint
- uri: file://donexml
And now, we would like to send json content to a messaging queue. Add AMQ bean registry into the container.
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="brokerURL"
value="vm://localhost?broker.persistent=false"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
Replace the File endpoint with AMQ Endpoint
file://donejson to activemq:queue:vip
Modify A-MQ dependency in POM.xml
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
<version>5.11.0.redhat-620133</version>
</dependency>
Remove the existing
file:donejson node
Run the application
See Appendix in Camel Ride 2 or 3 for testing xml files
4th Camel Ride completed!
My 4th Camel Ride
By weimeilin
My 4th Camel Ride
- 8,169