Integrate with Sage X3 Cloud Data using Apache Camel2

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Create a simple Java app that uses Apache Camel routing and the CData JDBC Driver to copy Sage X3 Cloud data to a JSON file on disk.

Apache Camel is an open source integration framework that allows you to integrate various systems consuming or producing data. When paired with the CData JDBC Driver for Sage X3 Cloud, you can write Java apps that use Camel routes that integrate with live Sage X3 Cloud data. This article explains how to create an app in NetBeans that connects, queries, and routes Sage X3 Cloud data to a JSON file.

With built-in optimized data processing, the CData JDBC Driver offers unmatched performance for interacting with live Sage X3 Cloud data. When you issue complex SQL queries to Sage X3 Cloud, the driver pushes supported SQL operations, like filters and aggregations, directly to Sage X3 Cloud and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations). Its built-in dynamic metadata querying allows you to work with and analyze Sage X3 Cloud data using native data types.

Creating A New Maven/Java Project

Follow the steps below to create a new Java project and add the appropriate dependencies:

  1. Open NetBeans and create a new project.
  2. Select Maven from the categories list and Java Application from the projects list, then click Next.
  3. Name the project (and adjust any other properties) and click Finish.
  4. In the source package, create a new Java class (we used App.java for this article) and add the main method to the class.

Adding Project Dependencies

With the project created, we can start adding the dependencies needed to work with live Sage X3 Cloud data from our App. If you have not already done so, install Maven in your environment, as it is required to add the JAR file for the CData JDBC Driver to your project.

Installing the CData JDBC Driver for Sage X3 Cloud with Maven

  1. Download the CData JDBC Driver for Sage X3 Cloud installer, unzip the package, and run the JAR file to install the driver.
  2. Use Maven to install the JDBC Driver as a connector.
    mvn install:install-file 
    	-Dfile="C:\Program Files\CData\CData JDBC Driver for Sage X3 Cloud 2019\lib\cdata.jdbc.sagex3cloud.jar" 
    	-DgroupId="org.cdata.connectors" 
    	-DartifactId="cdata-sagex3cloud-connector" 
    	-Dversion="19" 
    	-Dpackaging=jar
    

Once the JDBC Driver is installed, we can add dependencies to our project. To add a dependency, you can either edit the pom.xml file or right-click the dependencies folder and click Add Dependency. The properties for each dependency follow, but you can search through the available libraries by typing the name of the dependency in the Query box in the Add Dependency wizard.

Selecting a dependency

Required Dependencies

DependencyGroup IDArtifact IDVersion
camel-coreorg.apache.camelcamel-core3.0.0
camel-jacksonorg.apache.camelcamel-jackson3.0.0
camel-jdbcorg.apache.camelcamel-jdbc3.0.0
camel-jsonpathorg.apache.camelcamel-jsonpath3.0.0
cdata-sagex3cloud-connectororg.cdata.connectorscdata-salesforce-connector19
commons-dbcp2org.apache.commonscommons-dbcp22.7.0
slf4j-log4j12org.slf4jslf4j-log4j121.7.30
log4jorg.apache.logging.log4jlog4j2.12.1

Accessing Sage X3 Cloud Data in Java Apps with Camel

After adding the required dependencies, we can use the Java DSL (Domain Specific Language) to create routes with access to live Sage X3 Cloud data. Code snippets follow. Download the sample project (zip file) to follow along (make note of the TODO comments).

Start by importing the necessary classes into our main class.

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.SimpleRegistry;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.log4j.BasicConfigurator;

Then in the main method, we configure logging, create a new BasicDataSource and add it to the registry, create a new CamelContext, and finally add a route to the context. In this sample, we route Sage X3 Cloud data to a JSON file.

Configure Logging

BasicConfigurator.configure();

Create a BasicDataSource

Create a BasicDataSource and set the driver class name (cdata.jdbc.salesforce.SalesforceDriver) and URL (using the required connection properties).

Sage X3 Cloud uses the OAuth 2.0 Client Credentials flow, and an X-API-Key is also required for API access. Set AuthScheme to OAuth and specify the following connection properties:

  • URL: The base URL of your Sage X3 Cloud instance.
  • OAuthAccessTokenUrl: The OAuth token endpoint (e.g., https://your-auth-domain/oauth/token).
  • OAuthClientId: Your OAuth application client ID.
  • OAuthClientSecret: Your OAuth application client secret.
  • Audience: The API audience value for the token request.
  • XAPIKey: The X-API-Key provided by your Sage X3 Cloud administrator.
  • Folder: The Sage X3 folder name (e.g., SEED). This folder is used as the default schema.
  • Folders (optional): A comma-separated list of Sage X3 folders (e.g., SEED,PERF). Each folder is exposed as a separate schema, so you can query across folders with the Schema.Table syntax.

The driver obtains an access token with the Client Credentials flow and sends it with the X-API-Key on every API request. With InitiateOAuth set to GETANDREFRESH (the default), the driver acquires and refreshes the token automatically.

BasicDataSource basic = new BasicDataSource();
basic.setDriverClassName("cdata.jdbc.sagex3cloud.SageX3CloudDriver");
basic.setUrl("jdbc:sagex3cloud:AuthScheme=OAuth;URL=https://x3server/;OAuthAccessTokenUrl=https://auth-domain/oauth/token;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;Audience=https://api-audience;XAPIKey=your_api_key;Folder=SEED;");

The CData JDBC Driver includes a built-in connection string designer to help you configure the connection URL.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Sage X3 Cloud JDBC Driver. Either double-click the JAR file or execute the jar file from the command line.

java -jar cdata.jdbc.sagex3cloud.jar

Fill in the connection properties and copy the connection string to the clipboard.

Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)

Add the BasicDataSource to the Registry and Create a CamelContext

SimpleRegistry reg = new SimpleRegistry();
reg.bind("myDataSource", basic);

CamelContext context = new DefaultCamelContext(reg);

Add Routing to the CamelContext

The routing below uses a timer component to run one time and passes a SQL query to the JDBC Driver. The results are marshaled as JSON (and formatted for pretty print) and passed to a file component to write to disk as a JSON file.

context.addRoutes(new RouteBuilder() {
	@Override
	public void configure() {
		from("timer://foo?repeatCount=1")
			.setBody(constant("SELECT * FROM Account LIMIT 10"))
			.to("jdbc:myDataSource")
			.marshal().json(true)
			.to("file:C:\\Users\\USER\\Documents?fileName=account.json");
	}
});

Managing the CamelContext Lifecycle

With the route defined, start the CamelContext to begin the lifecycle. In this example, we wait 10 seconds and then shut down the context.

context.start();
Thread.sleep(10000);
context.stop();

Free Trial, Sample Project & Technical Support

Now, you have a working Java application that uses Camel to route data from Sage X3 Cloud to a JSON file. Download a free, 30-day trial of the CData JDBC Driver for Sage X3 Cloud and the sample project (make note of the TODO comments) and start working with your live Sage X3 Cloud data in Apache Camel. Reach out to our Support Team if you have any questions.

Ready to get started?

Download a free trial of the Sage X3 Cloud Driver to get started:

 Download Now

Learn more:

Sage X3 Cloud Icon Sage X3 Cloud JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Sage X3 Cloud.