Author Message
kirthikachandru
Joined: May 8, 2014
Messages: 72
Offline
Hi,

How to do auto reconnect to prociders in JTAPI application

Thanks,
Kirthika.C
ScottHaley
Joined: Nov 22, 2006
Messages: 7
Offline
I have written code that will reconnect to another AES server if the application gets a Provider Shutdown message. Make sure you call the rebootProviderService method from your EventHandler. Here is a snippet.

private ProviderService() throws ProviderException{
bootStrap();
//add ExtentionCollector contents back into queue...
Collection<Extension> set = ExtensionCollector.getInstance().getExtentions();
for(Extension e: set){
ExtensionQueue.getInstance().push(e);
}
}

public synchronized static ProviderService getInstance() throws ProviderException{
if (instance == null){
instance = new ProviderService();
}
return instance;
}

public Provider getProvider(){
return provider;
}

public List<String> getProviderHosts(){
return this.providerHosts;
}
/**
* This method parses through the TSAPI.PRO file and establishes a connection to an AES
* @throws ProviderException
* @throws AddressException
* @throws MessagingException
* @throws IOException
*/
private void bootStrap() throws ProviderException{

providerHosts = new ArrayList<String>();
// get list of AES Servers
String preferredHost = "";
String preferredService = "";

try {
providerHosts = ProviderUtils.getServers();
peer = JtapiPeerFactory.getJtapiPeer(null);
String[] services = getServices();


String p = services[0] +";login=" + ConfigMgr.getInstance().getLogin() + ";passwd=" + ConfigMgr.getInstance().getPw();

String providerString = p + ";servers=";
for(int i = 0; i < providerHosts.size(); i++){
String address = "";
address = Utils.lookup(providerHosts.get(i));
logger.info("AES IP Address:" + address);

providerString = providerString + address + ":450,";
logger.info("Provider String:" + providerString);
}
providerString = providerString.substring(0,providerString.length()-1);
logger.info("Provider String:" + providerString);
Thread.sleep(200);
try {
provider = peer.getProvider(providerString);
} catch (ProviderUnavailableException e) {
logger.error(e.getStackTrace());
System.exit(-1);
}
Thread.sleep(200);
provider.addProviderListener(new ProviderEventHandler());
} catch (JtapiPeerUnavailableException | ResourceUnavailableException | MethodNotSupportedException | InterruptedException | IOException | MessagingException e) {
logger.error(e);
throw new ProviderException(e.getMessage(),e);
}
}


/**
* This method Reboots, or restarts, the Provider and re-monitors extensions.
* @throws ProviderException
* @throws AddressException
* @throws MessagingException
* @throws IOException
*/
public void reBootProviderService() throws ProviderException, AddressException, MessagingException, IOException{
logger.info("Rebooting Provider Service");

// Stop any Provider Service established (if any) prior to trying to reconnect.
// stopProviderService();

int retryCount = 1;
int timer = ConfigMgr.getInstance().getRetryTimer();
String hostName = null;
try {
hostName = InetAddress.getLocalHost().getHostName() ;
}
catch (UnknownHostException e2) {
logger.error(e2);
}

// Try to reconnect to an AES server
instance.bootStrap();

// Wait for 1/2 second before checking status
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
logger.error(e1);
}

// Keep trying to restart the connection to AES for a specified amount of tries. If it succeeds, send out an UP page.
while( retryCount <= timer ){

// Check to see if the provider is back up.
if(provider.getState() == Provider.IN_SERVICE) {

String tname = provider.getName();
String[] t = tname.split(";");
String[] n = t[0].split("#");
String host = n[3];
System.out.println("Host name is: " + host);

logger.info("\nThe provider is back up and connected to " + host + ". Sending notification.\n");
// Send a page to notify that the connection to AES is back up.
Utils.sendPage("The CTI application is back up and connected to AES " + host + " for " + hostName);

// Re-monitor all of the extensions in the queue
extensionList = ExtensionCollector.getInstance().getExtentions();
for(Extension e: extensionList){
ExtensionQueue.getInstance().push(e);
}

for(int i1=0;i1 < ConfigMgr.getInstance().getExtensionThreadCount();i1++){
ExtensionWorker ew = new ExtensionWorker(i1);
ew.start();
}
// exit out of condition and continue on.
break;
}
// Wait for 1 minute (60 seconds) before retrying to connect again
try {
Thread.sleep(60L * 1000L);
} catch (InterruptedException e1) {
logger.error(e1);
}

if(retryCount == timer) {
// Unable to make a connection to AES. Shut down the application.
logger.error("Unable to establish a connection to AES on " + hostName + " CTI service shuting down.");
Utils.sendPage("Unable to establish a connection to AES on " + hostName + " CTI service shuting down.");
CtiServer cti = new CtiServer();
cti.stop();
cti.destroy();
System.exit(-1);
}
// increment the retryCount
retryCount = retryCount + 1;
// Retry the connection to AES
instance.bootStrap();
}

}
lokeshtaxali
Joined: Jan 23, 2014
Messages: 23
Offline
I have a question regarding the connect String that's been used in the code above (I've read the JTAPI programmers manual, but cant figure it out)

The manual says:

The format of the String is “<tlink>;login=<loginID>;passwd=<pw>;servers=<server entries>”
Where server entries follows the format
<AES_IP_ADDRESS1>:<TSAPI_SERVICE_PORT1>,<AES_IP_ADDRESS2>:<TSAPI
_SERVICE_PORT2>


The <tlink> contains the (unique) server hostname of the AES server, which will map to a unique IP.

If that is so, what is the point of being able to specify multiple server IPs/hostnames in the 'servers' parameter?
(Isn't a given tlink only going to exist on 1 server?)

Thanks
lokeshtaxali
Joined: Jan 23, 2014
Messages: 23
Offline
I'd really appreciate if someone could reply to my question above, thanks!
Go to:   
Mobile view