Skip to content

Commit 970090f

Browse files
authored
Merge pull request #10 from vlkong/master
Use post process listener to write results
2 parents 54ec828 + 450eca1 commit 970090f

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

build.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
<property name="oplall.jar" value="${example.home}/lib/oplall.jar"/>
3131

32+
<property name="debug" value="off"/>
3233

3334
<path id="project.class.path">
3435
<pathelement location="build/"/>
@@ -43,7 +44,7 @@
4344
</target>
4445

4546
<target name="compile" depends="init">
46-
<javac srcdir="${src}" destdir="${build}">
47+
<javac srcdir="${src}" destdir="${build}" debug="${debug}">
4748
<classpath refid="project.class.path"/>
4849
</javac>
4950
</target>

lib/jdbc-custom-data-source.jar

-2.11 KB
Binary file not shown.

src/main/java/com/ibm/opl/customdatasource/JdbcCustomDataSource.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.ibm.opl.customdatasource;
22

33
import ilog.opl.IloCustomOplDataSource;
4+
import ilog.opl.IloCustomOplPostProcessListener;
45
import ilog.opl.IloOplDataHandler;
56
import ilog.opl.IloOplElement;
67
import ilog.opl.IloOplElementDefinition;
@@ -20,6 +21,8 @@
2021
import java.util.Enumeration;
2122
import java.util.Properties;
2223

24+
25+
2326
/**
2427
* An custom data source reading data using JDBC.
2528
*
@@ -28,6 +31,30 @@ public class JdbcCustomDataSource extends IloCustomOplDataSource {
2831
private JdbcConfiguration _configuration;
2932
private IloOplModelDefinition _def;
3033

34+
/**
35+
* A post process listener to write output
36+
*/
37+
private static class JdbcCustomDataSourcePublisher extends IloCustomOplPostProcessListener {
38+
IloOplModel _model;
39+
JdbcConfiguration _config;
40+
41+
42+
JdbcCustomDataSourcePublisher(IloOplFactory factory, IloOplModel model, JdbcConfiguration config) {
43+
super(factory);
44+
_model = model;
45+
_config = config;
46+
}
47+
48+
@Override
49+
public void customStartPostProcess() {
50+
}
51+
52+
@Override
53+
public void customEndPostProcess() {
54+
JdbcWriter.writeOutput(_config, _model);
55+
}
56+
}
57+
3158
/**
3259
* Adds a custom data source to a model.
3360
*
@@ -41,6 +68,7 @@ public static void addDataSourceXMLConfig(String xmlFile, IloOplModel model) thr
4168
IloOplModelDefinition definition = model.getModelDefinition();
4269
JdbcCustomDataSource source = new JdbcCustomDataSource(config, factory, definition);
4370
model.addDataSource(source);
71+
model.registerPostProcessListener(new JdbcCustomDataSourcePublisher(factory, model, config));
4472
}
4573

4674
/**
@@ -54,6 +82,7 @@ public static void addDataSource(JdbcConfiguration config, IloOplModel model) {
5482
IloOplModelDefinition definition = model.getModelDefinition();
5583
JdbcCustomDataSource source = new JdbcCustomDataSource(config, factory, definition);
5684
model.addDataSource(source);
85+
model.registerPostProcessListener(new JdbcCustomDataSourcePublisher(factory, model, config));
5786
}
5887

5988
/**

src/main/java/com/ibm/opl/customdatasource/JdbcWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public JdbcWriter(JdbcConfiguration configuration, IloOplModelDefinition def, Il
4141
}
4242

4343
public void customWrite() {
44+
long startTime = System.currentTimeMillis();
4445
System.out.println("Writing elements to database");
4546
Properties prop = _configuration.getWriteMapping();
4647
Enumeration<?> propertyNames = prop.propertyNames();
@@ -50,7 +51,8 @@ public void customWrite() {
5051
System.out.println("Writing " + name + " using table " + table + "");
5152
customWrite(name, table);
5253
}
53-
System.out.println("Done");
54+
long endTime = System.currentTimeMillis();
55+
System.out.println("Done (" + (endTime - startTime)/1000.0 + " s)");
5456
}
5557

5658
static final String CREATE_QUERY = "CREATE TABLE %(";
@@ -74,7 +76,7 @@ else if (type == Type.STRING)
7476
query += ", ";
7577
}
7678
query += ")";
77-
System.out.println("Create query = " + query);
79+
// System.out.println("Create query = " + query);
7880
return query;
7981
}
8082

0 commit comments

Comments
 (0)