Apache Camel Marshalling/Unmarshalling for XML and JSON data format| CodeUsingJava

Apache Camel Marshalling/Unmarshalling for XML and JSON

Overview

Using Example to understand Apache Camel marshalling and unmarshalling uisng activemq

Table Of Contents :


Technology Stack

We will be making use of-
  • Java 1.8
  • Apache Camel 3.0.0-M2
  • Maven
  • Camel-jms 2.14.1
  • Activemq-camel 5.10.1
  • Activemq-broker 5.10.1
  • Activemq-client 5.10.1
  • Activemq-pool 5.10.1
  • Camel-jackson 2.14.1
  • Camel-spring 2.13.0
  • Camel-jaxb 2.13.0

Let us get started

In this section, we will introduce Apache Camel's marshalling and unmarshalling.

We're going to read json data in this case, unmarshal it to a java object, change it and then send it to the queue in xml format. The project that we are going to create is as follows-

activemq

The dependencies of pom.xml are as follows-


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.codeusingjava</groupId>
	<artifactId>camel-marshalling-unmarshalling-activemq</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.14.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jms</artifactId>
			<version>2.14.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-camel</artifactId>
			<version>5.10.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
			<version>5.10.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-client</artifactId>
			<version>5.10.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
			<version>5.10.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jackson</artifactId>
			<version>2.14.1</version>
		</dependency>


		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring</artifactId>
			<version>2.13.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jaxb</artifactId>
			<version>2.13.0</version>
		</dependency>

	</dependencies>
</project>


Here we will unmarshalling, marshalling object of Student class.

package com.codeusingjava.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {

	private String studentName;
	private int studentId;
	
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public int getStudentId() {
		return studentId;
	}
	public void setStudentId(int studentId) {
		this.studentId = studentId;
	}
	
}


Next we describe the Camel Processor class in which the unmarshalled object will be modified

package com.codeusingjava.myprocessor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

import com.codeusingjava.model.Student;

public class StudentProcessor implements Processor{

	public void process(Exchange exchange) throws Exception {

		Student stud = exchange.getIn().getBody(Student.class);
		stud.setStudentName("Code_using_java");
		exchange.getIn().setBody(stud);
	}

}


Next we build a Camel Route class. A route is like Camel's instructions on how to transfer your messages from one place to another. We will create StudentRouteBuilder.java file which will move files to ActiveMQ from c:/inputFolder. We'll also be unmarshalling the input, changing it, then marshaling it and sending it to the queue. Here we use the JMS component that we will later define in the main class.

package com.codeusingjava.myroute;

import javax.xml.bind.JAXBContext;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.converter.jaxb.JaxbDataFormat;

import com.codeusingjava.model.Student;
import com.codeusingjava.myprocessor.StudentProcessor;

public class StudentRouteBuilder extends RouteBuilder {

	@Override
	public void configure() throws Exception {

		// XML Data Format
		JaxbDataFormat xmlDataFormat = new JaxbDataFormat();
		JAXBContext con = JAXBContext.newInstance(Student.class);
		xmlDataFormat.setContext(con);

		// JSON Data Format
		JacksonDataFormat jsonDataFormat = new JacksonDataFormat(Student.class);

		from("file:C:/inputFolder").doTry().unmarshal(jsonDataFormat).process(new StudentProcessor())
		.marshal(xmlDataFormat).to("jms:queue:codeusingjava").doCatch(Exception.class)
		.process(new Processor() {
			public void process(Exchange exchange) throws Exception {
				Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
				System.out.println(cause);
			}
		});
	
	}
}


When Camel is started, it creates an object of CamelContext containing the definition of the starting route. Below we create the default camel context and load the StudentRouteBuilder created route. Our main class will be as follows:

package com.codeusingjava.main;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

import com.codeusingjava.myroute.StudentRouteBuilder;

public class StudentMainApp {

	public static void main(String[] args) {
		StudentRouteBuilder routeBuilder = new StudentRouteBuilder();
        CamelContext ctx = new DefaultCamelContext();
        
      //configure jms component        
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://0.0.0.0:61616");
        ctx.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        
        try {
            ctx.addRoutes(routeBuilder);
            ctx.start();
            Thread.sleep(5 * 60 * 1000);
            ctx.stop();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
	}

}


We will have a file student.json in C://inputFolder as below:

{"studentName":"Code_using_java","studentId":"101"}

Now we need to start ActiveMQ

activeMQ_start

you can go to http://localhost:8161/admin/queues.jsp, see there are no active queues on this page. camel-activemq
Next run as a java application the MainApp class and refresh the activeMQ page above. We'll see the creation of a new queue called codeusingjava with a xml message in it.
queue-output

outputJsontoXml

Download the code of origin-

Download marshal unmarshal using activemq-
Apache Camel ActiveMQ Marshal and Unmarshal Example