iSeries DataQueue access using Java com.ibm.as400.access.DataQueue

DataQueue is the standard in AS400 for any kind of queue processing. Basically it is used mostly for FIFO transaction that is asynchronous. So what if we need to process a HTTP request as we are creating new orders in the system to send data in XML format to another system. Well it would be nice if you can push the order number as we create them into the dataqueue and then a Java job just read the queue and take the necessary action based on the order number. Well here are examples on how to use AS400 dataqueues in Java programs.

How to read data from DataQueue using Java Program in iSeries

//Properties file with AS400 connection info and create DataQueue object
Properties props = new Properties();
DataQueue input_dataQ = null; 

//Load properties file and create AS400 system object 
props.load(new FileInputStream(prop_filename));
AS400 system = new AS400(props.getProperty("local_system").trim(),props.getProperty("dataQuserId").trim(),props.getProperty("dataQpassword").trim());

//Create DataQueue and connect to DataQueue service on the AS400
input_dataQ = new DataQueue(system, props.getProperty("dataQ").trim());
system.connectService(AS400.DATAQUEUE);

//Take a peek inisde the DataQueue without actually removing the entry
DataQueueEntry dataQEntry = input_dataQ.peek(-1);
String input_data = dataQEntry.getString().trim();

//Read the DataQueue entry                   
dataQEntry = input_dataQ.read(-1);
input_data = dataQEntry.getString().trim();

//Disconnect DataQueue service 
system.disconnectService(AS400.DATAQUEUE);

How to write data to DataQueue using Java Program in iSeries

//Properties file with AS400 connection info 
Properties props = new Properties();

//Load properties file and create AS400 system object 
props.load(new FileInputStream(prop_filename));
AS400 system = new AS400(props.getProperty("local_system").trim(),props.getProperty("dataQuserId").trim(),props.getProperty("dataQpassword").trim());

//Connect to DataQueue service on the AS400
system.connectService(AS400.DATAQUEUE);

//Create DataQueue object and write data
DataQueue output_dataQ = new DataQueue(system, input_data.substring(0,40).trim());
output_dataQ.write("Writing data into the DataQueue !");

//Disconnect DataQueue service
system.disconnectService(AS400.DATAQUEUE);

Sample properties file

#I-series ip or host name 
local_system=localhost

#I-series UserId, used for dataQ
dataQuserId=XXXXXX

#I-series Password
dataQpassword=XXXXXX

#Data Queue name for Transaction Request
dataQ=/QSYS.LIB/DTAQLIB.LIB/@DTAQNAME.DTAQ

Tip: You must download jt400.jar or jt400.zip for the above code to work

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.