Java service multi threadInvoke

Hi All,

Need help here. I’m just new to WM java service. I might made some mistakes along the code.
I wanted to create a java service that would spawn a number of threads depending on the number of input parameters, each input will do a server.doThreadInvoke to a certain flow service that will have some processing. During the implementation it keeps on throwing an error execution.

Input:
inputParam ----> string array

Output:
documents -----> documentArray

IDataCursor pipelineCursor = pipeline.getCursor();
String inputParam= IDataUtil.getStringArray( pipelineCursor, “inputParam” );
pipelineCursor.destroy();

ServiceThread svcThread = null;
for(int i=0;i<inputParam.length;i++){
IData input = IDataFactory.create();
IDataCursor inputCursor = input.getCursor();
IDataUtil.put(inputCursor, “inputToService”, inputParam[i]);
inputCursor.destroy();
Logging(“DEBUG”,inputParam[i]);
svcThread[i] = Service.doThreadInvoke(“helloFolder.anotherFold.services”, “ServiceMade”, input);
}
Logging(“DEBUG”,“reached this point”);
IDataCursor pipelineCursor_1 = pipeline.getCursor();
for(int i=0;i<=inputParam.length;i++){
try {
documents[i] = IDataFactory.create();
IDataCursor docCursor = documents[i].getCursor();
IDataUtil.put( docCursor , “documents”, svcThread[i] );
docCursor .destroy();
} catch (Exception e) {
throw new ServiceException(e);
}
}
pipelineCursor_1.destroy();

I get to see the logging of the inputParam[0]. (invoked service contains logging). however, I can’t see if it was able to process the inputParam[1] and so on. because I can’t see their process in the logs. My guess is that during the first invocation, inputParam[0]. it was able to successfully invoke the service but after the invocation, there’s some error.

I get a null pointer error at the first loop (it doesn’t reach the second loop) because I’m not seeing “reached this point” in the logs. Is there any step that I’m missing.

Error
java.lang.NullPointerException
at helloFolder.anotherFold.services.ServiceMade(services.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

May I know the reason for this service and also your wM version?

There is a service already available “pub.flow:invokeService”, check the service guide.

You’re trying to access svcThread before it’s initialized.

Try to change the code below:

ServiceThread[] svcThread = null;

to

ServiceThread[] svcThread = new ServiceThread[inputParam.length];

@Mahesh, I wanted to create a service that would accept an array input. These array input will looped inside the service and each input will spawn a new thread and invoke a service. This way I would be able to asynchronously invoke services, then take the output of each spawned thread and output it as the output of the service.

@Xiaowei Wang, Thank you for this. This solved my problem. :slight_smile:

@ Mark,

Thanks for the details, just thinking what would be the ideal use case to spawn the thread based on number of inputs?

We have used a code which invokes the service purely in async fashion and continues with the next execution, I am curious to know your requirement.

Hi @Mahesh,

Actually, I have a service that outputs an array of string. Then each string is an input of the invoked service, since the backend of the invoked service might respond for more than a second, invoking the service synchronously for each input string might take me forever(e.g. string might have a length of 50). Then taking the response of each of the invoked service( so that could be a document array of 50 ) for further processing.

E.g. Service Response = inputParam[0] = input_1, inputParam[1]=input_2, inputParam[2]=input_3.
So I need to invoke “helloFolder.anotherFold.services:ServiceMade” 3x asynchronously.

And read some posts from the forum, their talking about service.doThreadInvoke() and getIData(). So I was thinking of using them with some for Loop logic.

hmmm… Do you mean pub.flow:invokeService can invoke other service Asynchronously? The documentation seems to be confusing.

Thank you. I would like to get some suggestions If you could provide. I really wanted to avoid java service if possible.

pub.flow:invokeService is not Async, I can crosscheck again.

Okay, Thank you @Mahesh. :slight_smile: