Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions camel/CSVExample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">


<modelVersion>4.0.0</modelVersion>

<groupId>ibmi.example</groupId>
<artifactId>ibm-i-csv-example</artifactId>
<packaging>jar</packaging>
<version>1.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<camel.version>3.11.0</camel.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-parent</artifactId>
<version>${camel.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-fop</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-as2</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bindy</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>net.sf.jt400</groupId>
<artifactId>jt400</artifactId>
<version>10.6</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jt400</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-pdf</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-componentdsl</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-csv</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>ibmi.example.CSVExample</mainClass>
<includePluginDependencies>false</includePluginDependencies>
</configuration>
</plugin>

</plugins>
</build>

<name>CSV Creation Example</name>
</project>
178 changes: 178 additions & 0 deletions camel/CSVExample/src/main/java/ibmi/example/CSVExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package ibmi.example;

/**
* Standard imported libraries
*/
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.springframework.stereotype.Component;
import org.apache.camel.dataformat.csv.CsvDataFormat;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* IBM i specific imported libraries for connecting to the DB2 database
*/
import com.ibm.as400.access.AS400JDBCDataSource;

/**
* This is a CSV example for creating a CSV from a query and doing different operations with it as a result
*/
public class CSVExample {

public static void main(final String... args) throws Exception {

/**
* Values for configuring application. Setting constants at the top of the app for configuration
* and then using variables within the app allow you to configure your app for multiple situations
* without having to dig through code and make changes within the code.
*
* Options are:
* standardcsvexample - Just creating a standard CSV, no options
* pipedelimitedexample - Creating a pipe deliminited example and removing double quotes from strings
* displayindividualfieldoutput - Selecting just the first row from the query results, and outputting
* to the screen, no file written
*
*/
final String whatexample = "standardcsvexample";

/**
* This is the location we want our file stored in after it is created. Note that you can start from
* root with a leading forward slash, or a subdirectory of where the program is called like the example
* below
*/
final String filelocation = "tmp/";


/**
* Standard for a Camel deployment. Start by getting a CamelContext object.
*/
CamelContext context = new DefaultCamelContext();

/**
* This sets up the connection for local IBM i Here is where we will store
* connection information for the application
*/
AS400JDBCDataSource localDS = new AS400JDBCDataSource("localhost", "*CURRENT", "*CURRENT");

/**
* These values below exist if you are not journaling your IBM i table that you are querying
*/
//localDS.setTransactionIsolation("none");

/**
* This binds the localDS we set above to the route jt400
*/
context.getRegistry().bind("jt400", localDS);

/**
* Setting our Marshall options for CSV
*/


/**
* What example do we want to accomplish? Set the variable above to choose the example
*/
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("timer://ibmiexamplecheckinterval?period=5000")
/**
* This is our example query. Here we are pulling results from the system.
* You set the body of our program (which is a mutable object used to contain the results throughout).
* Here I have set it to a constant, but you can use the simple() function to set some dynamic values
* if needed in your own example. Once you have set the body, you send to JT400. The results are then
* stored in our body, replacing the query we set it to before.
*/
.setBody(constant("select * from QSYS2.NETSTAT_INFO"))
.to("jdbc:jt400")
/**
* Now we have queried some results! We want to send it to a CSV for output.
*/
.to("direct:" + whatexample);
}
});

/**
* This will do a standard output of values to CSV
*/
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
// The from should match our to in the calling route. This connects our two routes
from("direct:standardcsvexample")
// Marshall is how we are going to divide up each result from the row and what to do
.marshal()
// This creates our CSV from the results that were marshalled
.csv()
// This specific header we are setting is the name of our file
.setHeader("CamelFileName", constant("CSVExample.csv"))
// This is where we are storing the file. This is set at the top by constants
.to("file:" + filelocation);
}
});

/**
* This will show how to adjust the pipe delimiter
*/
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:pipedelimitedexample")
.marshal()
.csv()
.setHeader("CamelFileName", simple("${header.examplefilename}"))
.to("file:" + filelocation);
}
});

/**
* This is an example of how to take individual values from the first row of our query,
* set them to a header (where we tend to store individual values throughout our app),
* and output those values to the screen.
*/
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:displayindividualfieldoutput")
// This will take our multiple rows of results, split them up, and give us the ability to acces them one by one
// If there are no results from the query, the application will just stop
.split(body())
// Here we set the header of two values from our results. Notice we use the simple() function
// To access values from our query ${body[COLUMN_HEADER]}
.setHeader("localportname", simple("${body[LOCAL_PORT_NAME]}"))
.setHeader("RoundTripVariance", simple("${body[ROUND_TRIP_VARIANCE]}"))
// Now we will run a process to print the values out to the screen for each row
.process((exchange) -> {
System.out.println("Local Port Name: " + exchange.getIn().getHeader("localportname"));
System.out.println("Round Trip Variance: " + exchange.getIn().getHeader("RoundTripVariance"));
});
}
});


/**
* This actually "starts" the route, so Camel will start monitoring and routing
* activity here.
*/
context.start();

/**
* This runs the check every 5 seconds if it is stopped
*/
while (!context.isStopped()) {
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}

}

}
6 changes: 6 additions & 0 deletions camel/CSVExample/src/main/resources/log42j2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
appender.out.type = Console
appender.out.name = out
appender.out.layout.type = PatternLayout
appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
rootLogger.level = INFO
rootLogger.appenderRef.out.ref = out