Pages

Monday, April 15, 2013

Using AS/400 DataQueues in Java

Effective SOA solutions take advantage of all the services available across the systems, which include the legacy systems in your organization. There are several ways you can integrate the existing services running on your legacy systems. This series of posts is about using the AS/400 services directly in your Java applications. I will be covering how to call RPG programs, AS/400 Commands and AS/400 DataQueues in your Java applications.

Fortunately IBM has provided a very nice easy to use library for communicating with the AS/400 server from Java. The IBM Toolbox for Java is a library of Java classes that give Java programs easy access to IBM iSeries data and resources. JT Open is the open source version of Toolbox for Java. You can go to JT Open link to download the full set of java libraries and some more details of how that can be used to easily communicate with the AS/400 server. There are several ways you can access the services on AS/400 server, most common are as follows.

All of these different methods of accessing the AS/400 services and have their own pros and cons. JT Open is a very powerful library and provides very easy to use APIs to communicate with the AS/400 services. These posts are about exploiting the JT Open libraries to use the AS/400 services. I am splitting these different approaches to separate posts. Click on the topic above to see the relevant post for that topic.

  • Using AS/400 DataQueue.

DataQueues are an integral part of the AS/400 System. For any external communication they are a very good tool to implement event based applications to take appropriate actions on either sides on receipt of a message. Best thing about the DataQueues is that they are bidirectional, means not only you can send a message to the AS/400 but also you can receive a message from AS/400.

Data queues provide considerable flexibility to the Programmer. The DataQueues interfaces require no communications programming and can be used either for connected or disconnected communication. Java programs can communicate with AS/400 programs via a common AS/400 DataQueue. The data queue messages are merely described at the record-level, allowing the application programmer to define the field-level structure as required.

JT Open provides a simple set of Classes that hide most of the communication complexity and presents a very simple easy to use set of APIs to the Java Programmer. I am going to demonstrate the use of these classes with the help of tow very simple programs that reads and writes messages to a DataQueue. First the program that reads data from the DataQueue.

Reading from DataQueue

package as400;

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.DataQueue;
import com.ibm.as400.access.DataQueueEntry;

public class DataQueueTest implements Runnable{

 String server="yourserver.company.com";
 String user = "AS400USER";
 String pass = "AS400PWRD";

 String queueName = "MYDTQ";
 String libraryName = "MYLIB";
 
 private AS400 system = null;
 private DataQueueEntry dqData = null;
 
 @Override
 public void run() {

  String queue = "/QSYS.LIB/" + libraryName +".LIB/" + queueName +".DTAQ";
  
  try{
   int cntr=1;
   system = new AS400(server, user, pass);
   DataQueue dq = new DataQueue(system, queue);
   
   while(true){
    
    String data = null;
    try{
     
     System.out.println("Listening to DataQueue ......");
     
     // Start wailting for a message to arrive
     // Disconnect after 5 seconds if nothing received
     dqData = dq.read(5);
     if (dqData != null) {
      // get the data out of the DataQueueEntry object.
         byte[] bytes = dqData.getData();
      data = new String(bytes, "IBM285").trim();
        }
     
     if (data == null || data.trim().length() <= 0) {
      
      // Break after 5 tries
      if(cntr < 5){
       System.out.println("DataQueue Re-Started: " + cntr);
       cntr ++;
       continue;
      }else{
       System.out.println("Giving up on DataQueue after " + cntr + " tries");
       break;
      }
     }
     System.out.println("--|" + data + "|--");
    }catch(Exception e){
     e.printStackTrace();
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   // Make sure to disconnect
   if(system != null){
    try{
     system.disconnectAllServices();  
    }catch(Exception e){}
   }
  }
 }
 
 // Main method to start the Thread
 public static void main(String a[]){
  new Thread(new DataQueueTest()).start();
 }
}

This is a very simple program listening to a DataQueue and waiting for a message to arrive. We pass the timeout to dq.read(5); method in seconds. This is very low just to show the output as this tries again and again to re-connect to the queue. Here is the output when I run this program on my system.

Listening to DataQueue ......
DataQueue Re-Started: 1
Listening to DataQueue ......
--|00000001ADS#D000000000000000000000100000000000000#JK9000000000000001000|--
Listening to DataQueue ......
DataQueue Re-Started: 2
Listening to DataQueue ......
--|00000001SDFJKLO0000000000000000000100000000000000#OD8000000000000001000|--
Listening to DataQueue ......
DataQueue Re-Started: 3
Listening to DataQueue ......
DataQueue Re-Started: 4

As you can see from the output, the data received is a long String, this is actually a list of parameters received from AS/400 with fixed lengths. We can extract data from this String and use it to Kickstart something in our Java program.

Writing to a DataQueue

Now let's see how we can write data to a DataQueue.


package as400;

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.DataQueue;

public class DataQueueTest {

 String server="yourserver.company.com";
 String user = "AS400USER";
 String pass = "AS400PWRD";

 String queueName = "MYDTQ";
 String libraryName = "MYLIB";
 
 private AS400 system = null;
 
 public static void main(String a[]){

  String queue = "/QSYS.LIB/" + libraryName +".LIB/" + queueName +".DTAQ";
  
  String dataStr = "Message from Java";
  
  try{
   system = new AS400(server, user, pass);
   DataQueue dq = new DataQueue(system, queue);
   
   // Convert the Data Strings to IBM format
   byte[] byteData = dataStr.getBytes("IBM285");
   
   dq.write(byteData);
   
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   // Make sure to disconnect
   if(system != null){
    try{
     system.disconnectAllServices();  
    }catch(Exception e){}
   }
  }
 }
}

These are the very basics of how to use the IBM Toolbox for Java to communicate with programs in AS/400. For more details and advanced topics you can consult the IBM programmers guide. To view or download the PDF version of this document, select IBM® Toolbox for Java™ (about 3100 KB).

No comments:

Post a Comment