Versions Compared

Key

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

...

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). 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
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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.List;
import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.camel.example.reportincident.InputReportIncident;
import org.apache.camel.example.reportincident.model.Incident;

public class WebService {

	private static final transient Log LOG = LogFactory.getLog(WebService.class);

	public void process(Exchange exchange) throws ParseException {
        
		InputReportIncident webincident = (InputReportIncident)exchange.getIn().getBody(); (1)
		
		LOG.debug("Incident received from : " + webincident.getFamilyName() + ", " + webincident.getGivenName());
		LOG.debug("Incident info : " + webincident.getIncidentId() + ", at : " + webincident.getIncidentDate());
		LOG.debug("Incident details : " + webincident.getDetails() + ", summary : " + webincident.getSummary());
  
		
		List<Map<String, Incident>> models = new ArrayList<Map<String, Incident>>();
		Map<String, Incident> model = new HashMap<String, Incident>();
			
		// Convert the InputReportIncident into an Incident 
		Incident incident = new Incident(); (2)
		
		DateFormat format = new SimpleDateFormat( "dd-mm-yyyy" );
		incident.setIncidentDate(format.parse(webincident.getIncidentDate()));
		
		incident.setDetails(webincident.getDetails());
		incident.setEmail(webincident.getEmail());
		incident.setFamilyName(webincident.getFamilyName());
		incident.setGivenName(webincident.getGivenName());
		incident.setIncidentRef(webincident.getIncidentId());
		incident.setPhone(webincident.getPhone());
		incident.setSummary(webincident.getSummary());
		
		// Get Header origin from message
		String origin = (String) exchange.getIn().getHeader("origin"); (3)

		// Specify current Date
		format = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss" );
                String currentDate = format.format( new Date() );
                Date creationDate = format.parse( currentDate );
        
                incident.setCreationDate(creationDate);
                incident.setCreationUser(origin);
		
                LOG.debug("Incident created from web service : " + incident.toString());
        
		// and place it in a model (cfr camel-bindy)
		model.put(Incident.class.getName(), incident); (4)
		models.add(model);
		
	        // replace with our input
		exchange.getOut().setBody(models); (5)
		
		// propagate the header
		exchange.getOut().setHeader("origin", origin); (6)
         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.List;
import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.camel.example.reportincident.InputReportIncident;
import org.apache.camel.example.reportincident.model.Incident;

public class WebService {

	private static final transient Log LOG = LogFactory.getLog(WebService.class);

	public void process(Exchange exchange) throws ParseException {
        
		InputReportIncident webincident = (InputReportIncident)exchange.getIn().getBody(); (1)
		
		LOG.debug("Incident received from : " + webincident.getFamilyName() + ", " + webincident.getGivenName());
		LOG.debug("Incident info : " + webincident.getIncidentId() + ", at : " + webincident.getIncidentDate());
		LOG.debug("Incident details : " + webincident.getDetails() + ", summary : " + webincident.getSummary());
  
		
		List<Map<String, Incident>> models = new ArrayList<Map<String, Incident>>();
		Map<String, Incident> model = new HashMap<String, Incident>();
			
		// Convert the InputReportIncident into an Incident 
		Incident incident = new Incident(); (2)
		
		DateFormat format = new SimpleDateFormat( "dd-mm-yyyy" );
		incident.setIncidentDate(format.parse(webincident.getIncidentDate()));
		
		incident.setDetails(webincident.getDetails());
		incident.setEmail(webincident.getEmail());
		incident.setFamilyName(webincident.getFamilyName());
		incident.setGivenName(webincident.getGivenName());
		incident.setIncidentRef(webincident.getIncidentId());
		incident.setPhone(webincident.getPhone());
		incident.setSummary(webincident.getSummary());
		
		// Get Header origin from message
		String origin = (String) exchange.getIn().getHeader("origin"); (3)

		// Specify current Date
		format = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss" );
                String currentDate = format.format( new Date() );
                Date creationDate = format.parse( currentDate );
        
                incident.setCreationDate(creationDate);
                incident.setCreationUser(origin);
		
                LOG.debug("Incident created from web service : " + incident.toString());
        
		// and place it in a model (cfr camel-bindy)
		model.put(Incident.class.getName(), incident); (4)
		models.add(model);
		
	        // replace with our input
		exchange.getOut().setBody(models); (5)
		
		// propagate the header
		exchange.getOut().setHeader("origin", origin); (6)
         
     }

}

Remarks :
(1) - The message coming from the WebService and copied in the InputReportIncident class is retrieved from the body using the method exchange.getIn().getBody()
(2) - We create using the model a class Incident where we will put webservice's InputReportIncident
(3) - We get the Header field ('Origin') to check the origin of the messages (webservice). This info will be persisted in the DB
(4) - The model incident is added to the Map and List objects required by Camel Bindy
(5) - The model is added to the body object of the message that we send it OUT
(6) - Thbe header parameter is also propagated for the next endpoint

...

Now that evverything is in place, we can create the three routes that we need to implement the architecture that we have presented in the introduction of this tutorial

From File(s) to queue

This first route will be used by Apache Camel to read files deposited in the directory. Here is the explanation of the route

Code Block
xmlxml
xml

	<camel:camelContext trace="true" xmlns="http://camel.apache.org/schema/osgi"> (1)

		<camel:route> 
			<camel:from uri="file://d:/temp/data/?move=d:/temp/done/${file:name}.bak" /> (2)
			<camel:setHeader headerName="origin"> (3)
				<camel:constant>file</camel:constant>
			</camel:setHeader>
			<camel:unmarshal ref="bindyDataformat" /> (4)
	<camelContext trace="true" xmlns="http://camel.apache.org/schema/osgi"> (1)

		<camel:route> 
			<camel:fromto uri="filequeuingservice://d:/temp/data/?move=d:/temp/done/${file:name}queue:in" /> (25)
			<camel:setHeader headerName="origin"> (3)
				<camel:constant>file</camel:constant>
			</camel:setHeader>
			<camel:unmarshal ref="bindyDataformat" /> (4)
			<camel:to uri="queuingservice:queue:in" /> (5)
		</camel:route>
...

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

From Webservices to queue

</camel:route>
...

Remarks :
(1) - camel:camelContext tag is used to instantiate the camelcontext at the launch of the bundle. The trace parameter is defined as true so the tracing will be available on the console
(2) - The from uri="file" informs Camel that a file component must be started and it will listen for incoming files deposited in the directory d:/temp/data/. When the file is processed (end of the Camel route), then it is moved to the directory d:/temp/done where the file is renamed (.bak extension is added). During the process of the file and till the route is not finished, the file will be locked
(3) - A header is added to the message with the property origin setted to file
(4) - To parse the content of the CSV file into a collection of incident objects, we use the action unmarshall where the reference provided corresponds to the bean BindyCsvDataFormat instantiated by Spring
(5) - The result of the parsing process is copied as a message in the queue:in

From Webservices to queue

The second route will read incoming web services call, extract the XML messages from the web services, transform it into an InputReportIncident, send the object to a bean who will convert it into an Incident object and put the result in the queue:in.

Code Block
xml
xml
...		 
		<camel:route>
			<camel:from uri="cxf:bean:reportIncident" /> (1)
			<camel:setHeader headerName="origin"> (2)
				<camel:constant>webservice</camel:constant>
			</camel:setHeader>
			<camel:convertBodyTo type="org.apache.camel.example.reportincident.InputReportIncident" /> (3)
			<camel:to uri="bean:webservice" /> (4)
			<camel:inOnly uri="queuingservice:queue:in" /> (5)
			<camel:transform>transform> (6)
				<camel:method bean="feedback" method="setOk" />
			</camel:transform>
		</camel:route>
...

Remarks :
(1) - The CXF component will be used by Camel to receive incoming web services calls
(2) - A header is added to the message with the property origin setted to webservice
(3) - The content of the SOAP envelop is extract and the XML messages is mapped to the InputReportIncident class
(4) - A message contaning the header, InputReportIncident object is send the bean "Webservice" where the object will be transformed into an Incident object
(5) - The message is placed into a queue which is a inOnly so no OUT message will be send back to the bean webservice
(6) -

From queue to DB

Code Block
xml
xml
...		 
	
		<camel:route>
			<camel:from uri="queuingservice:queue:in" />
			<camel:to uri="bean:incidentSaver?method=process" />
		</camel:route>
	</camelContext>

...