Skip to main content
Published: June 29 2006, 5:37:00 PMUpdated: July 24 2022, 3:53:09 PM

 

Many eBay API applications in most times need to execute unrelated small tasks like, GetItem, GeteBayOfficalTime, GetUser. The simplest way to carry this kind of tasks is to create new thread object whenever a task arrive. One of the disadvantages of the thread-per-request approach is the significant overhead on your underlying JVM because your JVM spends more time and resource to create and destroy threads than actually execute tasks.

A thread pool or worker thread pattern offers a solution to the problem of thread life-cycle overhead. Worker thread executes many requests, each worker thread continually accepts new runnable commands from hosts, holding them in a pool until they can be run. This implementation minimize the start-up overhead by reusing existing threads to execute multiple runnable tasks.

With 1.5 a supported eBay JAVA SDK version, we do not need to write our own Thread Pool code and we can take advantage of the jdk1.5 concurrency utility library. The library provides powerful, high-level thread constructs, including executors and other synchronization primitives.

What you need to do is to have a client who creating arriving request instances and send to the thread queue, a few request classes contain the API actual runnable request task and then have all the tasks executed by ThreadPoolExecutor.

Implementation sample:

1. Instantiate the ThreadPoolExecutor class and create a thread pool with following parameters
import java.util.concurrent.*;

//Creating a thread pool with maxSize = 10 and minSize =2
//Using an unbounded queue: LinkedBlockingQueue
//keepAliveTime = 50000L;
int minSize = 2;
int maxSize = 10;
long keepAlive = 50000L;
ThreadPoolExecutor tpe =
new ThreadPoolExecutor(
minSize,
maxSize,
keepAlive,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()
);

2. Execute api request runnable tasks
//Instantiate the API request runnable tasks
VerifyAddItemReq verifyTask = null;
GetItemReq getItemTask = null;
GetSearchResultsReq getSearchResultsTask = null;
//execute api runnable tasks
for (int i = 0; i < nTasks; i++) {
try {
switch (i){
case 0:
(new ApiRequestThreadPool()).setApiReqParam();
verifyTask = new VerifyAddItemReq(n, "VerifyAddItemReqTask " +i, _apiContext,_apiCall);
tpe.execute(verifyTask);
break;
case 1:
//Create ApiContext, register ApiLogging and set up CallRetry logic
(new ApiRequestThreadPool()).setApiReqParam();
getItemTask = new GetItemReq(n, "GetItemReqTask " +i, _apiContext, _apiCall);
tpe.execute(getItemTask);
break;
case 2:
(new ApiRequestThreadPool()).setApiReqParam();
getSearchResultsTask = new GetSearchResultsReq(n, "GetSearchResultsReqTask " +i,
_apiContext, _apiCall);
tpe.execute(getSearchResultsTask);
break;
}
}catch(RuntimeException re){
tpe.shutdownNow();
}catch (Exception e){
// shutdown running tasks immediately
tpe.shutdownNow();
}
}
tpe.shutdown();



 

 

Attachments
How well did this answer your question?
Answers others found helpful