
io.baltoro.remote.InstancePoller Maven / Gradle / Ivy
package io.baltoro.remote;
import java.util.concurrent.ConcurrentLinkedQueue;
import io.baltoro.exception.NoRunningInstanceException;
import io.baltoro.to.WSTO;
public class InstancePoller extends Thread
{
private String appUuid;
boolean run = true;
public InstancePoller(String appUuid)
{
this.appUuid = appUuid;
}
@Override
public void run()
{
while(run)
{
if(!WSSessions.get().isRunning(appUuid))
{
System.out.println("1 No running instance for "+appUuid+", closing the polling thread ");
break;
}
ConcurrentLinkedQueue queue = WSSessions.get().getQueue(appUuid);
if(queue == null || queue.size() == 0)
{
sleep(appUuid+" queue is empty !");
continue;
}
WSTO to = queue.peek();
if(to == null)
{
sleep(appUuid+" no items in queue !");
continue;
}
SessionInstance session;
try
{
session = WSSessions.get().getSessionForWorker(appUuid, to.instanceUuid);
}
catch (NoRunningInstanceException e)
{
System.out.println("2 No running instance for "+appUuid+", closing the polling thread ");
break;
}
if(session == null)
{
sleep(appUuid+" no free instances ! try again in 5 secs ");
continue;
}
to = queue.poll();
RequestWorker worker = new RequestWorker(to, session);
worker.start();
}
}
private void sleep(String text)
{
try
{
long t0 = System.currentTimeMillis();
String sync = appUuid+"-queue";
synchronized (sync.intern())
{
System.out.println(text);
sync.intern().wait(50000);
System.out.println(text);
System.out.println("Server waited : "+(System.currentTimeMillis() - t0));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy