Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

All the infrastructure is in place, so we can start to describe the beans that we will use

3) Beans reference

5 beans will be used by our application :

  • BindyCsvDataFormat : to generate the model, marshall (= parse a CSV file in to incidents objects) or unmarshall (= create a CSV file from incidents objects)
  • IncidentSaver : proxy service who will extract from the body of the message the Incidents objects and call the reportinicident.Service bundle to save in the DB the incidents
  • WebService : service who will receive messages from WebServices, extract them from the body message and transform them into Incident objects
  • Feedback : simple class who will send a message back to the webservice

So, adapt the camel-context.xml file :

Code Block
xml
xml

	<bean id="bindyDataformat" class="org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat">
		<constructor-arg type="java.lang.String" value="org.apache.camel.example.reportincident.model" /> (1)
	</bean>

	<bean id="incidentSaver" class="org.apache.camel.example.reportincident.internal.IncidentSaver">
		<property name="incidentService">
			<osgi:reference interface="org.apache.camel.example.reportincident.service.IncidentService"/> (2)
		</property>
	</bean>
	
	<bean id="webservice" class="org.apache.camel.example.reportincident.internal.WebService" />
	<bean id="feedback" class="org.apache.camel.example.reportincident.internal.Feedback" />
	
	<osgi:reference id="queuingservice" interface="org.apache.camel.Component" /> (3)

Remarks :

(1) - The name of the package containing the class of the model must be provided as parameter
(2) - We inject into our proxy service using OSGI service reference, the interface IncidentService
(3) - We need also an OSGI service reference to the queue engine using the interface org.apache.camel.Component

We will quickly review the three classes that we will created for our project in the directory org.apache.camel.example.reportincident.internal :

a) IncidentSaver

Code Block

package org.apache.camel.example.reportincident.internal;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.camel.Exchange;
import org.apache.camel.example.reportincident.model.Incident;
import org.apache.camel.example.reportincident.service.IncidentService;

public class IncidentSaver {

	private static final transient Log LOG = LogFactory.getLog(IncidentSaver.class);
	
	private IncidentService incidentService = null;

	public void process(Exchange exchange) throws ParseException {

		int count = 0;

		List<Map<String, Object>> models = new ArrayList<Map<String, Object>>(); (1)
		Map<String, Object> model = new HashMap<String, Object>();

		// Get models from message
		models = (List<Map<String, Object>>) exchange.getIn().getBody(); (2)
		
		// Get Header origin from message
		String origin = (String) exchange.getIn().getHeader("origin"); (3)
		LOG.debug("Header origin : " + origin);

		Iterator<Map<String, Object>> it = models.iterator();
		
		// Specify current Date
                DateFormat format = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss" );
                String currentDate = format.format( new Date() );
                Date creationDate = format.parse( currentDate );
        
		while (it.hasNext()) {

			model = it.next();
			
			LOG.debug("Model retrieved");

			for (String key : model.keySet()) {
				
				LOG.debug("Object retrieved : " + model.get(key).toString());
				
				// Retrieve incident from model
				Incident incident = (Incident) model.get(key); (4)
				incident.setCreationDate(creationDate);
		                incident.setCreationUser(origin);
		        
				LOG.debug("Count : " + count + ", " + incident.toString());
				
				// Save Incident
				incidentService.saveIncident(incident); (5)
				LOG.debug("Incident saved");
			}

			count++;
		}

		LOG.debug("Nber of CSV records received by the csv bean : " + count);

	}
	
        // Property used to inject service implementation (6)
	public void setIncidentService(IncidentService incidentService) {
		this.incidentService = incidentService;
	}
	
}

Remarks :
(1) - We instantiate List and Map classes that we will use to extract objects of our incident model
(2) - Using the method exchange.getIn().getBody(), we extract the objects from the message and put them in a List
(3) - We get the Header field ('Origin') to check the origin of the messages (file or webservice). This info will be persisted in the DB
(4) - The object incident is retrieved from the Map model. In our case, the key is unique because we have only created one model class
(5) - The incident is saved in the database by calling the OSGI service IncidentService.saveIncident
(6) - The field setIncidentService is used by Spring to inject dependency with OSGI service

b) WebService

Code Block

Remarks :
(1) -
(2) -
(3) -
(4) -

c) Feedback

Code Block

Remarks :
(1) -
(2) -
(3) -
(4) -

4) Routing

Web

Packaging and deployment

...