Red Hat
Feb 29, 2016
by Christina Lin
This series of blog is based on building an auto dealership management system on Fuse Integration Service. We will be creating three major functions in the system.
  • Sales report tracking 
  • Vehicle inventory status
  • Customer IoT Service
We will be exporting a sales report to a web page, provide current inventory status of available cars through web service. And collect customer data from IoT devices on their car then alert close by shops. It would be better if you have some basic knowledge of Apache Camel before you begin, because I will not explain it in a great detail, I'll put my focus on how it works with the base platform, OpenShift. For Camel basic, you can check out my previous JBoss Fuse workshop. 

Alright, let's see what we are doing on part two, this is the second microservices of our case, and this service is the simplest out of all of them. It simple returns the current inventory status of the cars which is generated and stored in the application. Then it publishes this service via a web restful endpoint and return the result back in json format. 
And we are going to create 2 web services out of this, first one that returns the inventory status base on vehicle id and second one will be taking vehicle sales price range as the parameter, and return the inventory status of vehicles within price range.

So, first step is to create java bean that generate the mock result of inventory. And then map it in Camel. 

Java Bean

package com.redhat.fis.dms.mockprocessor;

import java.util.ArrayList;
import java.util.List;

import com.redhat.fis.dms.model.Vehicle;

public class VehicleGenerator {

private List<Vehicle> carpool;

public VehicleGenerator(){
generateCarpool();
}

public List<Vehicle> avaliablePriceRangeVehicle(Integer min, Integer max){
List<Vehicle> avaliableVehicles = new ArrayList<Vehicle>(); for( Vehicle car:carpool){
if(car.getPrice()>=min && car.getPrice()<=max)
avaliableVehicles.add(car);
}
return avaliableVehicles;
}

public Vehicle getVehicle(String vehicleId){
for( Vehicle car:carpool){
if(car.getVehicleId().equalsIgnoreCase(vehicleId))
return car;
}
return null;
}

private void generateCarpool(){
carpool = new ArrayList<Vehicle>();
//.......
}


}

In Camel context xml, register the bean, 

<bean id="vehicleGenerator" class="com.redhat.fis.dms.mockprocessor.VehicleGenerator"/>

Complete code can be found here

Route returns the inventory status with vehicle id:



<route id="getVehicle">
 <from uri="direct:getVehicle"/>
 <bean ref="vehicleGenerator" method="getVehicle(${headers.vehicleid})"/>
 <marshal>
  <json prettyPrint="true" library="Jackson"/>
 </marshal>
 <log message="${body}"/>
</route>

Route returns the inventory status of price range:




<route id="availableVehicle">
 <from uri="direct:availableVehicle"/>
 <bean ref="vehicleGenerator" method="avaliablePriceRangeVehicle(${headers.minprice},${headers.maxprice})"/>
 <marshal>
  <json prettyPrint="true" library="Jackson"/>
 </marshal>
 <log message="${body}"/>
</route>

Web Service 
This will expose a webservice with port 9191.
<restConfiguration component="jetty" port="9191"/>
<rest path="/AutoDMS">
 <get uri="/availableVehicle/pricerange/{minprice}/{maxprice}">
  <to uri="direct:availableVehicle"/>
 </get>
 <get uri="/getVehicle/{vehicleid}">
  <to uri="direct:getVehicle"/>
 </get>
</rest>

To expose the port on Docker, we need to set it up in the pom.xml, in the docker-maven-plugin, add 







That's all. We can then publish and deploy this service on to OpenShift platform, if you would like to know more detail about this, please check on part one of the series. In command line, under the project folder, login to OpenShift with it's client tool. Make sure you create a project called demo and deploy the mq-basic application on it, if you have not previously done so. This will save us from the need to set Kubernetes settings. Then simply type in console.
  • mvn -Pf8-local-deploy
And you will see the application deployed on OpenShift.




If you want to test it, here is where you can test the two webservices, 

  • http://inventoryservice-demo.cdk.10.1.2.2.xip.io/AutoDMS/availableVehicle/pricerange/20000/40000

  • http://inventoryservice-demo.cdk.10.1.2.2.xip.io/AutoDMS/getVehicle/vno03

In the application console, we can monitor how the routes are doing, 


To make things more interesting, say if the demand for this service has grow, we want to have more resource to support the load, all we have to do is to scale up is simply add more pod in the replication controller. 



And it will automatically add resource and balance the load to each pod. So how do we make sure every time when we redeploy it will create the number of pod we added? Simple, add the setting in the configuration in pom.xml. 

  • <fabric8.replicas>2</fabric8.replicas>

That's all for part two!




Original Post