Multiple Thread invocation

Hi,

We wanted to have a service that is able to invoke another service in a different thread. we we’re able to make use of the Service.doThreadInvoke(), but upon implementation with our use case. the java service seems to mix combine the output of some of the threads.

Ideal:
output[0]: [0,1,2,3,4,5]
output[1]: [11,22,33,44,55]

Actual output:
output[0]:[]
output[1]: [0,1,2,3,4,5,11,22,33,44,55]

Use case:
We wanted to have a service that could spawn new threads to invoke a different service.
Input: a string array - each string of the array is the input to the service that would be invoked.
Service: a single service that accept a string. A concurrent invocation to this thread would improve the service performance as doing it synchronously. (an array of 100 string would mean to wait for the service will be invoked 100x synchronously, this would take much time.)
Output: a document array of the responses of the invoked service.

sample code snippet
for(int i = 0; i < inputStringList.length; i++){

		// Create Input IData to 'getSampleDetails'
		IData input = IDataFactory.create();
		IDataCursor inputCursor = input.getCursor();
		IDataUtil.put( inputCursor, "inputStringList", inputStringList[i] );
		inputCursor.destroy();

		//Invoke service
		svcThread[i] = Service.doThreadInvoke("wm.test.services", "getSampleDetails", input);
	}
	
	//Get 'getSampleDetails' service output
	IDataCursor pipeline_1 = pipeline.getCursor();
	IData[] results = new IData[svcThread.length];
	for(int i = 0; i < svcThread.length; i++){
		try {
			results[i] = svcThread[i].getIData();
		} catch (Exception e) {throw new ServiceException(e);}
	}

Try this…


import com.wm.data.*;
import com.wm.util.Values;
import com.wm.app.b2b.server.Service;
import com.wm.app.b2b.server.ServiceException;
// --- <<IS-START-IMPORTS>> ---
import com.wm.lang.ns.NSName;
import com.wm.app.b2b.server.ServiceThread;
// --- <<IS-END-IMPORTS>> ---

public final class support

{
	// ---( internal utility methods )---

	final static support _instance = new support();

	static support _newInstance() { return new support(); }

	static support _cast(Object o) { return (support)o; }

	// ---( server methods )---




	public static final void doAsyncInvoke (IData pipeline)
        throws ServiceException
	{
		// --- <<IS-START(doAsyncInvoke)>> ---
		// @sigtype java 3.5
		// [i] field:0:required service
		// [i] record:0:optional input
		// [o] object:0:required pointer
		IDataCursor pc = pipeline.getCursor();
		
		ServiceThread pointer = Service.doThreadInvoke(NSName.create(&quot;distributor.support:timedInvoke&quot;), pipeline);
		
		IDataUtil.put(pc, &quot;pointer&quot;, pointer);
		pc.destroy();
		// --- <<IS-END>> ---

                
	}



	public static final void timedInvoke (IData pipeline)
        throws ServiceException
	{
		// --- <<IS-START(timedInvoke)>> ---
		// @sigtype java 3.5
		// [i] field:0:required service
		// [i] record:0:optional input
		// [o] object:0:required pointer
		IDataCursor pc = pipeline.getCursor();
		
		String svc = IDataUtil.getString(pc, &quot;service&quot;);
		IData input = IDataUtil.getIData(pc, &quot;input&quot;);
		
		long timer = System.currentTimeMillis();
		IData output;
		try {
		
			output = Service.doInvoke(NSName.create(svc), input);
		} catch (Exception e) {
			
			throw new ServiceException(e);
		}
		timer = System.currentTimeMillis() - timer;
		
		IDataCursor oc = output.getCursor();
		IDataUtil.put(oc, &quot;@duration&quot;, String.valueOf(timer));
		oc.destroy();
		
		IDataUtil.put(pc, &quot;output&quot;, output);
		pc.destroy();
		// --- <<IS-END>> ---

                
	}



	public static final void timer (IData pipeline)
        throws ServiceException
	{
		// --- <<IS-START(timer)>> ---
		// @sigtype java 3.5
		// [i] field:0:optional offset
		// [o] field:0:required timer
		IDataCursor pc = pipeline.getCursor();
		long offset = IDataUtil.getLong(pc, &quot;offset&quot;, 0);
		
		IDataUtil.put(pc, &quot;timer&quot;, String.valueOf(System.currentTimeMillis() - offset));
		pc.destroy();
		// --- <<IS-END>> ---

                
	}



	public static final void waitForThread (IData pipeline)
        throws ServiceException
	{
		// --- <<IS-START(waitForThread)>> ---
		// @sigtype java 3.5
		// [i] object:0:required pointer
		// [o] record:0:optional output
		IDataCursor pc = pipeline.getCursor();
		
		ServiceThread pointer = (ServiceThread)IDataUtil.get(pc, &quot;pointer&quot;);
		
		IData output;
		try {
			
			output = pointer.getIData();
		} catch (Exception e) {
		
			throw new ServiceException(e);
		}
		
		IDataCursor oc = output.getCursor();
		IDataUtil.put(pc, &quot;output&quot;, IDataUtil.get(oc, &quot;output&quot;));
		oc.destroy();
		pc.destroy();
		// --- <<IS-END>> ---

                
	}
}

Hi Senthilkumar G,

Sorry, I’m not that good in Java codes. Would you be able to explain to me the usage of these methods? I’m not sure how to Integrate them to my code.

Thank you very much.