Server-Side Implementation for Incoming Communication

The process of creating an incoming communication channel involves the writing of a channel class, implementing its methods, and configuring it using the Partner Administration tool.

This section provides you detailed information about the channel class and its methods.

IncomingChannel Class

You must create an incoming channel class to enable EC Communication Plug-In to receive incoming messages.

This class must extend the IncommingChannel class.

For example:

public class SampleIncommingChannel extends IncommingChannel { // Class fields goes here }

For a complete code listing of SampleIncommingChannel, see Sample Code - SampleIncommingChannel.package.

initiateChannel Method

The initiateChannel method contains the logic for configuring the channel. This abstract method must be implemented in the super class, using the initiateChannel(ChannelProperty[] props)method. If the initialization is successful, the return value is true. Otherwise, the return value is false.

ChannelProperty contains elements that hold the configuration data. The IncomingChannel class must be configured with the following properties:

  • Port number (Port) – the number of the port on which the channel will listen for incoming connections.

  • Maximum sub-threads (MaxSubThreads) – the maximum number of sub-threads that are allowed to run.

  • Read timeout (ReadTimeOut)

For every property, the associated ChannelProperty object must be retrieved. From this object we then retrieve the value which can be the default value if a value is not specified. If the default value is not specified and the property is mandatory, then we use the hardcoded value for the property.

The sample code below shows how the property for the port number is retrieved:


String propKey = "Port";
// Retrieve the correct ChannelProperty from the array
ChannelProperty prop = getProperty(props, propKey);
String value = getValue(prop);
// If the property did not exist we return false.
if (prop == null) return false;
if (value == null) {
// If value is null but mandatory we issue a warning and
// use the default value defined globally.
if (prop.isMandatory()) {
cat.warn("Using internal default value: " + myPort);
}
}
else {
try {
int tmp = Integer.parseInt(value);
if (tmp <= 0) {
throw new IllegalArgumentException("Negative or
zero value.");}
myPort = tmp;
}
catch (Exception e) {
cat.warn(propKey + " property for SampleIncommingChannel
is invalid. Using internal default value: " + myPort);
}
}

runChannel Method

The runChannel method contains the logic for receiving the message. This method contains logic for these:

  • CreatingtheServerSocket object if not already created

  • Acceptingincoming connections

  • Startingaworker that will receive the message

getDelayTime Method

The time in milliseconds between each successive call to runChannel is given by the getDelayTime method. If this method returns 0, then the runChannel will be called over and over again with no delay.

The implementation of this method looks like this:


public long getDelayTime() {
return 0;
}

ThreadWorker Class and runWork method

EC has a framework for managing threads in an easy way. For example, there is a worker class that implements the IThreadWorker. The worker implements the actual receiving of the message.

The IThreadWOrker.runWork method, which is called by the thread framework, implements logic for the these:

  • Creating a manifest

  • Reading the size of the message

  • Reading the message

  • Persisting the message

  • Queuing the manifest

Retrieve a ByteBuffer object then store the first four bytes from the incoming socket to the said object. A manifest, which acts as a delivery note for an incoming message, is then created. Once the first four bytes are retrieved, convert them into an integer that defines the length of the message.

The PersistMessage method is then called with the socket and message length as two arguments. To allow further processing, this message is stored in a file that is accessible to the EC Server.

stopPlugIn Method

The Select call in the selector is blocking the thread for the communication channel, thus, an error might occur when stopping the channel. To unblock the selector, override the stopPlugIn method.

The implementation of this method looks like this:


public void stopPlugIn() {
super.stopPlugIn();
connectSelector.wakeup();
}

The pausePlugIn method needs to be overridden in the same way. But in this case it calls the pausePlugIn method.

cleanUpChannel method

After the last call to the runChannel method, the cleanUpChannel method is called. As a result, the channel is given the chance to execute some logic before terminating. The socket-related objects eventually close.