Hi Guys,
I think I found a solution for killing a service even if it got stuck in an infinite loop etc. Please find the following solution for this. Basically the main program will have 2 inputs service name and timeout value after which you want to kill the service for further execution. Sub program will have service name as input.
Main Program:
package threadsPOC.latestImplementation;
import com.wm.data.*;
import com.wm.util.Values;
import com.wm.app.b2b.server.Service;
import com.wm.app.b2b.server.ServiceException;
import com.wm.lang.ns.NSName;
import com.wm.app.b2b.server.ServiceThread;
import com.wm.app.b2b.server.InvokeState;
import com.wm.app.b2b.server.ThreadManager;
public final class invokeFilePullingImpService_SVC
{
/**
* The primary method for the Java service
*
* @param pipeline
* The IData pipeline
* @throws ServiceException
*/
public static final void invokeFilePullingImpService(IData pipeline)
throws ServiceException {
IDataCursor pipelineCursor = pipeline.getCursor();
try
{
String serviceName = "threadsPOC.services:xyz";
long timeout = 20000;
int startTimer=0;
IData status = null;
IData threadDetails;
IDataCursor threadCursor;
IData interruptThreadDet;
IDataCursor interruptThreadCursor;
FilePullImpService m1=new FilePullImpService(serviceName);
Thread t1 =new Thread(m1);
t1.start();
long threadID = t1.getId();
System.out.println("ThreadID ---> " +threadID);
while(true)
{
if(startTimer < timeout)
{
status = m1.returnval();
if(status != null)
{
com.wm.util.JournalLogger.log( com.wm.util.JournalLogger.INFO, com.wm.util.JournalLogger.FAC_FLOW_SVC, com.wm.util.JournalLogger.DEBUG, "Recieved Result From Thread Class");
IDataUtil.put( pipelineCursor, "status", status );
break;
}
else
{
startTimer +=1000;
t1.join(1000);
System.out.println("Timer " +startTimer);
}
}
else
{
com.wm.util.JournalLogger.log( com.wm.util.JournalLogger.INFO, com.wm.util.JournalLogger.FAC_FLOW_SVC, com.wm.util.JournalLogger.DEBUG, "Thread Faied To Complete Before Timeout");
System.out.println("Status Of Thread Before Killing " + t1.getState());
m1.killService();
Thread.sleep(1000);
System.out.println("Status Of Thread After Killing " + t1.getState());
if(t1.getState().toString().equals("TERMINATED"))
{
System.out.println("Thread Got Killed ");
break;
}
threadDetails = IDataFactory.create();
threadCursor = threadDetails.getCursor();
IDataUtil.put(threadCursor, "threadID", threadID);
threadCursor.destroy();
System.out.println(threadDetails);
NSName nsName = NSName.create("wm.server.query:getThreadDumpForThread");
IData threadDumpIData = Service.doInvoke(nsName, threadDetails);
IDataCursor threadDumpCursor = threadDumpIData.getCursor();
String threadDump = IDataUtil.getString(threadDumpCursor,"threadDump");
threadDump = threadDump.replace("\n", " ");
int iniIndex = threadDump.indexOf("Id=");
int actIndex = threadDump.indexOf("Id=", iniIndex+3);
int spaceIndex = threadDump.indexOf(" ", actIndex+3);
String threadIDNew = threadDump.substring(actIndex+3,spaceIndex).trim();
System.out.println("threadDump " +threadDump);
System.out.println("threadID " +threadIDNew);
interruptThreadDet = IDataFactory.create();
interruptThreadCursor = interruptThreadDet.getCursor();
IDataUtil.put(interruptThreadCursor, "threadID", threadIDNew);
interruptThreadCursor.destroy();
System.out.println(interruptThreadDet);
NSName nsNameInt = NSName.create("wm.server.query:interruptThread");
Service.doInvoke(nsNameInt, interruptThreadDet);
Thread.sleep(1000);
System.out.println("Status Of Thread After Killing " + t1.getState());
break;
}
}
}
catch(Exception e)
{
throw new ServiceException(e);
}
pipelineCursor.destroy();
}
// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---
static class FilePullImpService extends Thread
{
IData serviceDetails;
IDataCursor outputCursor;
IData outputData = null;
String thrId = null;
FilePullImpService(String serviceName)
{
serviceDetails = IDataFactory.create();
outputCursor = serviceDetails.getCursor();
IDataUtil.put(outputCursor, "serviceName", serviceName);
outputCursor.destroy();
}
public void run()
{
try
{
com.wm.util.JournalLogger.log( com.wm.util.JournalLogger.INFO, com.wm.util.JournalLogger.FAC_FLOW_SVC, com.wm.util.JournalLogger.DEBUG, "Thread Class Started");
NSName nsName = NSName.create("threadsPOC.latestImplementation:doInvokeService");
ServiceThread thread = new ServiceThread(nsName, InvokeState.getCurrentSession(), serviceDetails);
thrId = ThreadManager.runTarget(thread);
outputData = thread.getIData();
com.wm.util.JournalLogger.log( com.wm.util.JournalLogger.INFO, com.wm.util.JournalLogger.FAC_FLOW_SVC, com.wm.util.JournalLogger.DEBUG, "Thread Class Completed");
}
catch(Exception e)
{
System.out.println(e);
com.wm.util.JournalLogger.log( com.wm.util.JournalLogger.INFO, com.wm.util.JournalLogger.FAC_FLOW_SVC, com.wm.util.JournalLogger.DEBUG, "Exception Caught In Thread Class");
}
}
public IData returnval()
{
return outputData;
}
public void killService()
{
System.out.println("Killing Service In Thread Class");
ThreadManager.interrupt(thrId);
}
}
// --- <<IS-END-SHARED-SOURCE-AREA>> ---
}
Sub Program:
public static final void doInvokeService(IData pipeline)
throws ServiceException {
try
{
IDataCursor pipelineCursor = pipeline.getCursor();
String serviceName = IDataUtil.getString(pipelineCursor,"serviceName");
NSName nsName = NSName.create(serviceName);
IData outputData = Service.doInvoke(nsName, null);
IDataUtil.put( pipelineCursor,"result",outputData);
pipelineCursor.destroy();
}
catch(Exception e)
{
throw new ServiceException(e);
}
}
Please analyse the program and let me know if you guys have any queries. Thanks to Castro for the help.
Thanks,
vinodkumar.