* The ETL scenario is APP_HOME/data/scenario/Examples/engine/db2db.xml. It is configured to extract data from 3 database tables * and then load to the different or the same database using different table names. *
* Data are streamed from the source database to the destination which means the memory consumption is minimal. *
* The destination tables are created automatically using metadata discovery feature of the ETL framework. *
* There are no input files or output files. *
* The logger is manually set to the INFO mode which increases verbosity of the etl engine. * */ public class GetConnectionsUsingJndi { /** * Configures ETL engine and executes ETL scenario. * * @param args the command line arguments */ public static void main(String[] args) { GetConnectionsUsingJndi engine = new GetConnectionsUsingJndi(); try { // instantiates ETL configuration EtlConfig etlConfig = new EtlConfig(); // creates embedded ETL process EtlProcess etlProcess = new EtlProcess(EtlProcess.EtlMode.EMBEDDED); // print out framework version System.out.println(SystemConfig.instance().getTitle( EtlConfig.DEFAULT_TITLE) + " " + SystemConfig.instance().getSystemProperty( SystemConfig.VERSION)); // creates configuration which contains source and destination // connections and ETL scenario name, loads and executes ETL // scenario. EtlResponse response = engine.createConfigAndExecute(etlConfig, "Examples/Engine/db2db.xml", etlProcess); // print out formatted output from the ETL response System.out.println(engine.getMessage(response, "Examples/Engine/db2db.xml")); } catch (Exception ex) { System.out.println(Utils.getStackTraceAsString(ex)); } System.exit(0); } /** * Manually creates connection aliases for the ETL process and maps to the JNDI resource, loads given ETL scenario * from the default folder. Executes ETl scenario. * * @param config the ETL configuration * @param scenariFileName the ETL scenario file name * @param etlProcess the ETL process * @return ETL response * @throws Exception in case of any error */ private EtlResponse createConfigAndExecute(EtlConfig config, String scenariFileName, EtlProcess etlProcess) throws Exception { // initializes ETl config config.init(); // creates source alias Alias source = new Alias(); source.setName("Source DB"); source.setUrl("jndi:java:comp/env/jdbc/source"); // creates destination alias Alias destination = new Alias(); destination.setName("Destination DB"); source.setUrl("jndi:java:comp/env/jdbc/destination"); // adds aliases. ETL process will create connections from these aliases config.addAliasToMap(EtlConfig.SOURCE_CONNECTION_NAME, source); config.addAliasToMap(EtlConfig.DEST_CONNECTION_NAME, destination); // instantiates ETL factory EtlFactory etlFactory = new EtlFactory(); // loads given ETl scenario, sets ETL action Scenario scenario = etlFactory.getScenario(config, scenariFileName); if (scenario == null) { System.exit(0); } scenario.setAction(EtlConfig.EXTRACT_LOAD); // creates ETl request using given config, scenario and log level EtlRequest request = new EtlRequest(config, scenario, Logger.INFO); // executes ETL process return etlProcess.execute(request); } /** * Creates the formatted message from the ETL response * * @param response the ETl response * @param scenarioName the ETL scenario name * * @return formatted message from the ETL response */ private String getMessage(EtlResponse response, String scenarioName) { String msg = ""; String start = response != null ? response.getStartTime().toString() : new Date().toString(); String end = response != null && response.getEndTime() != null ? response .getEndTime().toString() : new Date().toString(); String diff = response != null ? String .valueOf(Utils.getDateDiff(response.getEndTime(), response.getStartTime(), Calendar.SECOND)) : "0"; if (response.getRetCode() == EtlConfig.RETURN_OK) { msg = Utils .format("\n" + EtlResource.FINISH_SUCCESS_MSG.getValue() + "\n" + "Scenario name: %1" + "\n" + "Started at %2, finished at %3, total execution time %4 seconds.", scenarioName, start, end, diff); if (!Utils.isNothing(response.getWarnings())) { msg = msg + "\n" + EtlResource.WARING_NOTHING_WAS_EXECUTED.getValue(); } } else if (response.getRetCode() == EtlConfig.RETURN_NO_DRIVERS) { msg = "\n" + EtlResource.NO_DRIVERS_MSG.getValue(); } else { msg = Utils .format("\n" + EtlResource.FINISH_ERROR_MSG.getValue() + "\n" + "Scenario name: %1" + "\n" + "Started at %2, finished at %3, total execution time %4 seconds.", scenarioName, start, end, diff) + "\n" + Utils.getStackTraceAsString(response.getException()); } return msg; } }