Skip to content

Instantly share code, notes, and snippets.

@greghelton
Last active July 31, 2016 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greghelton/f38efb1b9019560bc19ead4b94501efd to your computer and use it in GitHub Desktop.
Save greghelton/f38efb1b9019560bc19ead4b94501efd to your computer and use it in GitHub Desktop.
using AS400 DTAQs (Data Queues), CL and a little bit of RPG to communicate with Java
package com.frontier.dpiplant.db;
import java.io.IOException;
import java.io.InputStream;
import com.ibm.as400.access.AS400;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class DpiDB {
String driver;
String url;
public String user;
public String pwd;
public String as400Name;
public AS400 as400;
public Connection conn;
public String inqInteractive;
public String outqInteractive;
public String inqBatch;
public String outqBatch;
static Logger LOGGER = LogManager.getLogger(DpiDB.class.getName());
public DpiDB() {
LOGGER.fatal("LOADING PROPERTIES FILE dpidb.properties");
System.out.println("LOADING PROPERTIES FILE dpidb.properties");
Properties props = new Properties();
InputStream file = DpiDB.class.getClassLoader().getResourceAsStream("dpidb.properties");
try {
props.load(file);
} catch(IOException e) {
LOGGER.fatal("ERROR LOADING PROPERTIES FILE");
System.out.println("ERROR LOADING PROPERTIES FILE");
System.exit(1);
}
this.driver = props.getProperty("driver");
this.as400Name = props.getProperty("as400");
this.url = props.getProperty("dburl");
this.user = props.getProperty("userName");
this.pwd = props.getProperty("password");
this.inqInteractive = props.getProperty("indtaq");
this.outqInteractive = props.getProperty("outdtaq");
this.as400 = new AS400(this.as400Name, this.user, this.pwd);
LOGGER.info("inqName=" + inqInteractive + ", outqName=" + outqInteractive + ", url=" + url);
System.out.println("inqName=" + inqInteractive + ", outqName=" + outqInteractive + ", url=" + url);
}
public Connection getConnection() throws ClassNotFoundException, SQLException {
if (conn == null || conn.isClosed()) {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
}
return conn;
}
Statement createStatement() throws SQLException {
return conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
}
public static void main(String[] args) throws Exception {
DpiDB db = new DpiDB();
System.out.println(db.toString());
}
}
pgm &Parm
/* +
to compile: +
- crtpgm LATLNGFIPC module(LATLNGFIPC UTGETUUID) +
+
to execute, pass an address to the program on the call: +
- call LATLNGFIPC ('6520 Plantaion Ln Frisco, TX') +
+
expected result if Java is running is parameter returns to caller +
containing latitude and longitude and Fipcode for the address passed in +
*/
Dcl &Parm *Char (200)
Dcl &Data *Char (216)
Dcl &FifoQ *Char (10) Value('DQFIFO')
Dcl &KeyedQ *Char (10) Value('DQKEYED')
Dcl &QueueLib *Char (10) Value('GAH285')
DCL &DqLength *Dec (5 0) Value(216)
DCL &KeyLength *Dec (3 0) Value(16)
DCL &Key *Char (16)
DCL &Wait *Dec (5 0) Value(10)
DCL &SndLength *Dec (3 0) Value(0)
DCL &Sender *Char (1) Value(' ')
DCL &KeyDqLen *Dec (5 0) Value(40)
DCL &LatLngFip *CHAR (40)
CRTDTAQ DTAQ(GAH285/DQFIFO) MAXLEN(216)
MONMSG CPF0000
CRTDTAQ DTAQ(GAH285/DQKEYED) MAXLEN(40) +
SEQ(*KEYED) KEYLEN(16)
MONMSG CPF0000
CRTDTAARA GAH285/TESTING *CHAR LEN(40)
MONMSG CPF0000
chgdtaara testing %sst(&parm 1 40)
dspdtaara testing
CALLPRC PRC(GETUUID) RTNVAL(&Key)
CHGVAR &Data Value(&Parm *cat &key)
Call QSndDtaQ (&FifoQ +
&QueueLib +
&DqLength +
&Data)
Call QRcvDtaQ (&KeyedQ +
&QueueLib +
&KeyDqLen +
&LatLngFip +
&Wait +
'EQ' +
&KeyLength +
&Key +
&SndLength +
&Sender )
/* get the char(40) value placed on the queue by Java */
CHGVAR &Parm &LatLngFip
endpgm
package org.yip.as400.RpgOaLatLngFip;
import java.io.IOException;
import java.text.DecimalFormat;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.frontier.dpiplant.db.DpiDB;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.DataQueue;
import com.ibm.as400.access.KeyedDataQueue;
public class PollDtaqs {
static final Logger LOGGER = LogManager.getLogger(PollDtaqs.class.getName());
DpiDB db;
DataQueue readQueue;
KeyedDataQueue writeQueue;
PollDtaqs() throws IOException, AS400SecurityException {
this.db = new DpiDB();
this.readQueue = new DataQueue(db.as400, db.inqInteractive);
this.writeQueue = new KeyedDataQueue(db.as400, db.outqInteractive);
}
public static void main(String[] arg) throws Exception {
PollDtaqs pdq = new PollDtaqs();
Queues queues = new Queues(pdq.db);
queues.connect();
String key = null;
String data = null;
do {
data = queues.readQueue.read(-1).getString().trim();
if (data != null && data.length() > 16) {
key = data.substring(data.length() - 16);
System.out.println(data);
queues.writeQueue.write(key, " .. + .. 1 .. + .. 2 .. + .. 3 .. + .. 4");
}
} while(true);
}
}
package org.yip.as400.RpgOaLatLngFip;
import java.io.IOException;
import java.text.DecimalFormat;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.frontier.dpiplant.db.DpiDB;
import com.ibm.as400.access.*;
public class Queues {
static final Logger LOGGER = LogManager.getLogger(Queues.class.getName());
String as400Name;
String user;
String pwd;
AS400 as400;
DataQueue readQueue;
String readQueueLocation;
KeyedDataQueue writeQueue;
String writeQueueLocation;
Queues(DpiDB db) throws IOException, AS400SecurityException {
this.user = db.user;
this.pwd = db.pwd;
this.readQueueLocation = db.inqInteractive;
this.writeQueueLocation = db.outqInteractive;
this.as400Name = db.as400Name;
}
public void connect() throws Exception {
as400 = new AS400(as400Name, user, pwd);
LOGGER.warn("connecting write queue=" + writeQueueLocation + " & read queue=" + readQueueLocation);
readQueue = new DataQueue(as400, readQueueLocation);
writeQueue = new KeyedDataQueue(as400, writeQueueLocation);
as400.connectService(AS400.DATAQUEUE);
}
}
ctl-opt Option(*Srcstmt:*Nodebugio:*NoUnRef)
Main(RPGOA01) Debug( *Yes );
dcl-f LatLngFip disk usropn usage(*update:*output)
handler('LATLNGFIPC');
dcl-proc RPGOA01;
dcl-pi *N;
end-pi;
dcl-ds result;
fld1 CHAR(20);
fld2 CHAR(20);
end-ds;
if not %open(LatLngFip);
open LatLngFip;
endIf;
parm = *BLANKS;
parm = '6520 Plantation Ln Frisco, TX';
read LatLngFip;
// write rLatLngFip;
// read LatLngFip;
result = parm;
dsply fld1;
close LatLngFip;
*INLR = *ON;
end-proc;
create table latlngfip (PARM CHAR(200) NOT NULL) RCDFMT RLATLNGFIP
CRTRPGMOD RPGOA01
CRTPGM RPGOA01
CRTRPGMOD UTGETUUID
CRTCLMOD LATLNGFIPC DBGVIEW(*SOURCE)
CRTPGM LATLNGFIPC module(LATLNGFIPC UTGETUUID)
CALL RPGOA01
ctl-opt NoMain Option( *Srcstmt:*Nodebugio:*NoUnRef )
Debug( *Yes );
// compile: crtrpgmod UTGETUUID
dcl-proc getUuid EXPORT;
dcl-pi *N CHAR(16);
end-pi;
dcl-ds uuidDS QUALIFIED;
dcl-subf bytes_provided INT(10) INZ(%size(uuidDS));
dcl-subf bytes_available INT(10) INZ(0);
dcl-subf reserved CHAR(8) INZ(*ALLx'00');
dcl-subf uuid CHAR(16);
end-ds;
dcl-pr genId extproc('_GENUUID');
dcl-parm pUuid LIKE(uuidDS);
end-pr;
genId(uuidDS);
return uuidDS.uuid;
end-proc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment