Chapter 14. Building roll your own clients 291
Closing the socket connection
To close the socket connection, use the close() function using the socket descriptor as a
parameter.
In the sample program, we use a
transaction socket. That means that the IMS Connect host
will close the connection as soon as the transaction has completed. Therefore, if we try to
send a second transaction to IMS, we receive an input output error, because we are trying to
write to a socket that has the remote end closed. If we want to send more than one
transaction without reopening the socket, we use a
permanent socket. See Chapter 7, “IMS
Connect programming model” on page 91 to learn more about transaction and permanent
sockets.
If you have used IMS Connector for Java, you are familiar with
shared and dedicated
connections. This concept has nothing to do with non-IMS Connector for Java clients, and it is
specific to the JCA implementation.
14.5.2 Java example
The complete source code listing is in Example A-2 on page 484. In this section, we review
some code excerpts that are relevant to this chapter.
Program structure
The Java sample consists on a single class (Sample), which contains methods to prepare,
send, and receive information to and from IMS Connect, and a static main method to parse
the command line to get parameters and data to build the transaction. Even though this is not
the
correct way of doing things in Java, it will suffice to show you the IMS Connect
programming specifics so that you can build on it.
Obtain a stream socket connection to the IMS Connect server
This is straightforward in Java. Example 14-7 shows the way to do it. The Socket constructor
takes care of the host name translation and returns an opened, ready-to-use socket object
reference.
Example 14-7 Opening a socket connection in Java
/**
* Establish a socket connection with the IMS Connect host
*/
public void connect() {
try {
// open a socket for the transaction
socket = new Socket(hostName, portNumber);
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
Preparing and sending the message to IMS Connect
Example 14-8 shows the code we use to send a message to IMS Connect in Java. This code
is very similar to the code we reviewed in Example 14-5 on page 284. The only relevant
differences are:
We do not need to do any translation between the network and host byte orders. We use a
DataOutputStream object to prepare the IRM, and the write() method of that Class already
converts the binary integers to big endian order.
292 IMS Connectivity in an On Demand Environment: A Practical Guide to IMS Connectivity
We use ByteArrayOutputStream as a buffer to build and hold the message until it is
complete so that we can send it to the IMS Connect server in a single write().
Of course, we do not have to worry about memory management: The garbage collector
takes care of that.
Example 14-8 Preparing and sending a message to IMS Connect in Java
/**
* Build and send a message to IMS Connect
* This method sends the message using a single write() call
* to improve efficiency and performance.
* The z/OS TCP/IP stack can be configured in such way that
* there is a 200ms delay for every ack message (TCP ack, not
* IMS Connect ACK). If such is the case, then you should expect
* a severe performance penalty if you use multiple write() calls.
* The old IMS Connect sample used a write for each field. DO
* NOT DO THAT.
* @param msgType Value for the IRM_F4 field (' ','R','A','N')
*/
public void send(char msgType) {
int totalLength;
String segment = null;
response = null;
byte irm_f3 = 0;
if (msgType == ' ')
segment = this.tranText;
// Compute the total length of the message. 1.
// In java we don't care about byte order, since the write() methods
// of DataOutputStream always work in network order.
// +4 for first LL, ZZ and final LL, ZZ
totalLength = 4 + prefixLength + 4;
// add in segment length, if segment is defined
if ((segment != null) && (segment.length() > 0)) {
totalLength += segment.length() + 12; // +12 for LL, ZZ, tranCode
}
try {
// Allocate a conveniently sized byte stream. 2.
ByteArrayOutputStream messageBuffer =
new ByteArrayOutputStream(totalLength);
// Associate a DataOutputStream to this newly created byte stream.
DataOutputStream out = new DataOutputStream(messageBuffer);
// Prepare another DataOutputStream to send the real message
// to our IP socket.
DataOutputStream outsocket =
new DataOutputStream(socket.getOutputStream());
// Compute the IRM_F3 byte 3.
irm_f3 = syncLevel;
if (purgeAsync) {
irm_f3 |= SL_PURGE;
}
if (reRoute) {
irm_f3 |= SL_REROUTE;
}

Get IMS Connectivity in an On Demand Environment: A Practical Guide to IMS Connectivity now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.