Sending data by using the Java API

The Z Common Data Provider Java™ API is a set of Java classes that Z Common Data Provider uses to exchange data internally. You can use these classes to write Java applications that send data to the Data Streamer.

About this task

To send data, the API must have the port number on which the Data Streamer listens for data.

Procedure

To use the Java API to send data to the Data Streamer, complete the following steps:

  1. Extract the /DS/LIB/CDPzLibraries.tar file, and add the CdpCommon.jar and CdpProtocol.jar files to the Java build path. Java API documentation is included in the .tar file.
  2. As shown in the following example, define a Java class for the sender, where localhost is the host where the Data Streamer is running. Specify the IP address of the Data Streamer host, if the Data Streamer is configured to bind to a specific IP address. Because the Data Streamer and Java application must be running on the same LPAR, the IP address must be a valid IP address on the LPAR where the Java application runs. The variable port is the port number on which the Data Streamer listens for data:
    CDPSender sender = new CDPSender("localhost",port);
  3. As shown in the following example, define a variable for identifying the origin of the data in traces and dumps.
    The value of senderName, which must have a maximum length of 8 characters, is included in the header to identify the origin of the data.
    String senderName = "SAMPSNDR";
    
  4. As shown in the following example, define a Java class for containing the metadata for the data.

    This table must contain the metadata keywords and values that are described in Metadata keywords and values.

    You can find the values on the Configure data stream dialog in the Configuration Tool. Also, the values must be the same as the data when you create the application data stream definition. For more information, see Creating an application data stream definition.

    HashMap<String, String> metadata = new HashMap<String, String>(5);
    metadata.put(DictionaryKey.encoding.name(), "IBM1047");
    metadata.put(DictionaryKey.path.name(), "APP/MyDataStream");
    metadata.put(DictionaryKey.sourcename.name(), "MyDataStream");
    metadata.put(DictionaryKey.sourcetype.name(), "zOS-MyDataStream");
    metadata.put(DictionaryKey.timezone.name(), "+0000");
  5. To send the data to the Data Streamer, complete the following steps that apply, depending on whether you are sending split or unsplit data:
    Table 1. Split or split data
    Split data
    1. Define a Java class for containing the records to be sent, and for adding records as they are collected, as shown in the following example:
      
      List<String> records = new ArrayList<String>();
      records.add(someRecord);
      
    2. Send the data to the Data Streamer, as shown in the following example:
      
      sender.sendType2(senderName, metadata, records);
    Unsplit data
    1. Send the data to the Data Streamer, as shown in the following example:
      
      String data = someData;
      sender.sendType1(senderName, metadata, data);
      
    Important: The following Java exceptions are thrown by the sendType2 and sendType1 methods and must be caught:
    IOException
    Thrown if an I/O error occurs when connecting to or sending data to the Data Streamer.
    IllegalArgumentException
    Thrown when the metadata does not contain a value for encoding, or when the length of the sender name is greater than 8 characters.
    UnsupportedEncodingException
    Thrown when the encoding that is provided in the metadata is not supported by Java.
    InterruptedException
    Thrown when the thread is interrupted.