From 126ed3943afd211c2bf2ca44e8fb15e165a7cc1b Mon Sep 17 00:00:00 2001 From: King Harrison Date: Mon, 13 Sep 2021 11:16:22 -0400 Subject: [PATCH 01/10] Create pom.xml --- camel/CSVExample/pom.xml | 155 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 camel/CSVExample/pom.xml diff --git a/camel/CSVExample/pom.xml b/camel/CSVExample/pom.xml new file mode 100644 index 00000000..ca4624dd --- /dev/null +++ b/camel/CSVExample/pom.xml @@ -0,0 +1,155 @@ + + + + + 4.0.0 + + ibmi.example + ibm-i-csv-example + jar + 1.0 + + + UTF-8 + UTF-8 + 3.11.0 + + + + + + org.apache.camel + camel-parent + ${camel.version} + import + pom + + + + + + + org.apache.camel + camel-core + + + org.apache.camel + camel-main + + + org.apache.camel + camel-fop + ${camel.version} + + + org.apache.camel + camel-as2 + ${camel.version} + + + org.apache.camel + camel-bindy + ${camel.version} + + + org.apache.camel + camel-ftp + ${camel.version} + + + org.apache.camel.springboot + camel-spring-boot + ${camel.version} + + + net.sf.jt400 + jt400 + 10.6 + + + org.apache.camel + camel-jdbc + ${camel.version} + + + org.apache.camel + camel-jt400 + ${camel.version} + + + org.apache.camel + camel-stream + ${camel.version} + + + org.apache.camel + camel-pdf + ${camel.version} + + + org.apache.camel + camel-componentdsl + ${camel.version} + + + org.apache.camel + camel-csv + ${camel.version} + + + org.apache.logging.log4j + log4j-api + runtime + + + org.apache.logging.log4j + log4j-core + runtime + + + org.apache.logging.log4j + log4j-slf4j-impl + runtime + + + org.apache.camel + camel-test + test + + + + + install + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-resources-plugin + 3.2.0 + + UTF-8 + + + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + ibmi.example.CSVExample + false + + + + + + + CSV Creation Example + From 07348c0595a4778d6ecbed7f165d1e9c9413ec4d Mon Sep 17 00:00:00 2001 From: King Harrison Date: Mon, 13 Sep 2021 11:18:33 -0400 Subject: [PATCH 02/10] Create CSVExample.java --- .../main/java/ibmi/example/CSVExample.java | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 camel/CSVExample/src/main/java/ibmi/example/CSVExample.java diff --git a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java new file mode 100644 index 00000000..9acfb695 --- /dev/null +++ b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java @@ -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(); + } + } + + } + +} From 385708874a9eb89debd7649943022da37d1ee078 Mon Sep 17 00:00:00 2001 From: King Harrison Date: Mon, 13 Sep 2021 11:20:30 -0400 Subject: [PATCH 03/10] Create log42j2.properties --- camel/CSVExample/src/main/resources/log42j2.properties | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 camel/CSVExample/src/main/resources/log42j2.properties diff --git a/camel/CSVExample/src/main/resources/log42j2.properties b/camel/CSVExample/src/main/resources/log42j2.properties new file mode 100644 index 00000000..df3daa59 --- /dev/null +++ b/camel/CSVExample/src/main/resources/log42j2.properties @@ -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 From 29a51ae5c6c79c02d6c42ec65e7c8eaf28602bd4 Mon Sep 17 00:00:00 2001 From: Jesse Gorzinski Date: Thu, 14 Oct 2021 11:53:35 -0500 Subject: [PATCH 04/10] fixup tab-delimited example --- .../src/main/java/ibmi/example/CSVExample.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java index 9acfb695..044c59b9 100644 --- a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java +++ b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java @@ -8,6 +8,7 @@ import org.apache.camel.impl.DefaultCamelContext; import org.springframework.stereotype.Component; import org.apache.camel.dataformat.csv.CsvDataFormat; +import org.apache.commons.csv.QuoteMode; import java.sql.Date; import java.util.ArrayList; @@ -39,7 +40,7 @@ public static void main(final String... args) throws Exception { * to the screen, no file written * */ - final String whatexample = "standardcsvexample"; + final String whatexample = args.length > 0 ? args[0].trim() : "standardcsvexample"; /** * This is the location we want our file stored in after it is created. Note that you can start from @@ -116,7 +117,10 @@ public void configure() { .to("file:" + filelocation); } }); - + final CsvDataFormat csvPipeDelimited = new CsvDataFormat(); + csvPipeDelimited.setQuoteMode(QuoteMode.NONE); + csvPipeDelimited.setEscape('\\'); + csvPipeDelimited.setDelimiter('|'); /** * This will show how to adjust the pipe delimiter */ @@ -124,10 +128,9 @@ public void configure() { @Override public void configure() { from("direct:pipedelimitedexample") - .marshal() - .csv() - .setHeader("CamelFileName", simple("${header.examplefilename}")) - .to("file:" + filelocation); + .marshal(csvPipeDelimited) + .setHeader("CamelFileName", constant("CSVPipeDelimtedExample.csv")) + .to("file:" + filelocation); } }); From 7f0d27b873f3a8b86e8a0d7b57d96ebb28d98969 Mon Sep 17 00:00:00 2001 From: Jesse Gorzinski Date: Thu, 14 Oct 2021 11:54:12 -0500 Subject: [PATCH 05/10] fileLocation readability (not to confuse with /tmp) --- camel/CSVExample/src/main/java/ibmi/example/CSVExample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java index 044c59b9..583e14d2 100644 --- a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java +++ b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java @@ -47,7 +47,7 @@ public static void main(final String... args) throws Exception { * root with a leading forward slash, or a subdirectory of where the program is called like the example * below */ - final String filelocation = "tmp/"; + final String filelocation = "./tmp/"; /** From 6f052768aa38fa1062c5a861fde88fe50bb7e4b0 Mon Sep 17 00:00:00 2001 From: Jesse Gorzinski Date: Thu, 14 Oct 2021 11:55:28 -0500 Subject: [PATCH 06/10] remove unused imports --- .../CSVExample/src/main/java/ibmi/example/CSVExample.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java index 583e14d2..fa91c854 100644 --- a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java +++ b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java @@ -6,16 +6,9 @@ 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 org.apache.commons.csv.QuoteMode; -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 */ From 2cc05c7ae6ef512dacbe5559bdc1fa26fa55d9b9 Mon Sep 17 00:00:00 2001 From: Jesse Gorzinski Date: Thu, 14 Oct 2021 12:15:57 -0500 Subject: [PATCH 07/10] have route only run once --- .../CSVExample/src/main/java/ibmi/example/CSVExample.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java index fa91c854..37cd03f6 100644 --- a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java +++ b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java @@ -46,7 +46,7 @@ public static void main(final String... args) throws Exception { /** * Standard for a Camel deployment. Start by getting a CamelContext object. */ - CamelContext context = new DefaultCamelContext(); + final CamelContext context = new DefaultCamelContext(); /** * This sets up the connection for local IBM i Here is where we will store @@ -75,7 +75,7 @@ public static void main(final String... args) throws Exception { context.addRoutes(new RouteBuilder() { @Override public void configure() { - from("timer://ibmiexamplecheckinterval?period=5000") + from("timer://ibmiexamplecheckinterval?repeatCount=1") /** * 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). @@ -88,7 +88,8 @@ public void configure() { /** * Now we have queried some results! We want to send it to a CSV for output. */ - .to("direct:" + whatexample); + .to("direct:" + whatexample) + .process((exchange) -> {new Thread(() -> { context.stop();}).start();}); } }); From d05a754d4fd28f2c4c4f48dba40b40839692d320 Mon Sep 17 00:00:00 2001 From: Jesse Gorzinski Date: Thu, 14 Oct 2021 12:20:15 -0500 Subject: [PATCH 08/10] remove jt400 from pom.xml --- camel/CSVExample/pom.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/camel/CSVExample/pom.xml b/camel/CSVExample/pom.xml index ca4624dd..98ce6c14 100644 --- a/camel/CSVExample/pom.xml +++ b/camel/CSVExample/pom.xml @@ -61,11 +61,6 @@ camel-spring-boot ${camel.version} - - net.sf.jt400 - jt400 - 10.6 - org.apache.camel camel-jdbc From b4b1fe69261299bd19ce32eb437ac0938660d997 Mon Sep 17 00:00:00 2001 From: Jesse Gorzinski Date: Thu, 14 Oct 2021 12:23:44 -0500 Subject: [PATCH 09/10] pull context stop into own route --- .../src/main/java/ibmi/example/CSVExample.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java index 37cd03f6..9bfa9da3 100644 --- a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java +++ b/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java @@ -89,7 +89,7 @@ public void configure() { * Now we have queried some results! We want to send it to a CSV for output. */ .to("direct:" + whatexample) - .process((exchange) -> {new Thread(() -> { context.stop();}).start();}); + .to("direct:stopcontext"); } }); @@ -151,6 +151,14 @@ public void configure() { }); } }); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:stopcontext") + .process((exchange) -> {new Thread(() -> { context.stop();}).start();}); + } + }); /** From 197b21c129d7a6bf4a1bf22c5c8717452c13ed6c Mon Sep 17 00:00:00 2001 From: Jesse Gorzinski Date: Thu, 14 Oct 2021 12:52:27 -0500 Subject: [PATCH 10/10] rename CSVExample dir --- camel/{CSVExample => csv_example}/pom.xml | 0 .../src/main/java/ibmi/example/CSVExample.java | 0 .../src/main/resources/log42j2.properties | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename camel/{CSVExample => csv_example}/pom.xml (100%) rename camel/{CSVExample => csv_example}/src/main/java/ibmi/example/CSVExample.java (100%) rename camel/{CSVExample => csv_example}/src/main/resources/log42j2.properties (100%) diff --git a/camel/CSVExample/pom.xml b/camel/csv_example/pom.xml similarity index 100% rename from camel/CSVExample/pom.xml rename to camel/csv_example/pom.xml diff --git a/camel/CSVExample/src/main/java/ibmi/example/CSVExample.java b/camel/csv_example/src/main/java/ibmi/example/CSVExample.java similarity index 100% rename from camel/CSVExample/src/main/java/ibmi/example/CSVExample.java rename to camel/csv_example/src/main/java/ibmi/example/CSVExample.java diff --git a/camel/CSVExample/src/main/resources/log42j2.properties b/camel/csv_example/src/main/resources/log42j2.properties similarity index 100% rename from camel/CSVExample/src/main/resources/log42j2.properties rename to camel/csv_example/src/main/resources/log42j2.properties