(Code samples for this document can be downloaded here.)
You are calling the eBay SOAP API through the eBay SDK for Java or eBay SDK for .NET from a multithreaded application and want to test for thread safety before you run a high volume of calls.
Run a simple thread safety test, like the sample included with this topic, in the Sandbox. Always run a thread safety test before you attempt a large volume of calls from a multithreaded application.
You can test thread safety effectively with a small number of threads and calls. The sample runs six threads that each call AddItem and then display the thread name, item ID, and token used. You can increase the number of threads or number of calls, but the test method is the same.
The MultiThreadTest sample included with this topic is fairly easy to run. Essentially, it takes these steps:
Each thread uses a different Sandbox test user, and each user has an individual token. You can easily change the sample to use more threads or make more calls. The key points to look for in the output are:
For example, this part of the output shows the tokens set for each user:
C:\samples\java\ThreadTest\classes>java com.ebay.migration.MultiThreadTest Start multithreading tests... For tsuser3 token is ABC...123 For tsuser6 token is DEF...456 . .
This part of the output shows that threads from users tsuser5, tsuser1, tsuser3, and tsuser2 are running concurrently, rather than consecutively:
[Fri Dec 09 10:38:39 PST 2005] tsuser2 -begin [Fri Dec 09 10:38:39 PST 2005] tsuser3 -begin [Fri Dec 09 10:38:39 PST 2005] tsuser5 -begin [Fri Dec 09 10:38:39 PST 2005] tsuser1 -begin [Fri Dec 09 10:38:39 PST 2005] tsuser4 -begin [Fri Dec 09 10:38:39 PST 2005] tsuser6 -begin [Fri Dec 09 10:38:39 PST 2005] tsuser2 -start GeteBayOfficialTime [Fri Dec 09 10:38:39 PST 2005] tsuser3 -start GeteBayOfficialTime [Fri Dec 09 10:38:39 PST 2005] tsuser5 -start GeteBayOfficialTime [Fri Dec 09 10:38:39 PST 2005] tsuser1 -start GeteBayOfficialTime [Fri Dec 09 10:38:39 PST 2005] tsuser4 -start GeteBayOfficialTime [Fri Dec 09 10:38:39 PST 2005] tsuser6 -start GeteBayOfficialTime [Fri Dec 09 10:38:49 PST 2005] tsuser5 -end GeteBayOfficialTime [Fri Dec 09 10:38:49 PST 2005] tsuser5 -buildItem() [Fri Dec 09 10:38:49 PST 2005] tsuser5 -start AddItem [Fri Dec 09 10:38:49 PST 2005] tsuser1 -end GeteBayOfficialTime [Fri Dec 09 10:38:49 PST 2005] tsuser1 -buildItem() [Fri Dec 09 10:38:49 PST 2005] tsuser1 -start AddItem [Fri Dec 09 10:38:51 PST 2005] tsuser3 -end GeteBayOfficialTime [Fri Dec 09 10:38:51 PST 2005] tsuser3 -buildItem() [Fri Dec 09 10:38:51 PST 2005] tsuser3 -start AddItem [Fri Dec 09 10:38:51 PST 2005] tsuser2 -end GeteBayOfficialTime [Fri Dec 09 10:38:51 PST 2005] tsuser2 -buildItem() [Fri Dec 09 10:38:51 PST 2005] tsuser2 -start AddItem
The third section of the output shows that the correct token was used for the current user:
The item id is: 4504127997 The user name is: tsuser5 The token is: XYZ...789
If you want to change the number of threads, do it at the beginning of the runMultipleThreads method:
public static void runMultipleThreads() {
System.out.println("Start multithreading tests...");
final int threadCnt = 6; // number of threads
If you want to add more calls or change the calls made, do it in the makeCalls method. makeCalls is written to pass a SOAP API request through ApiCall.
public static void makeCalls()
{
// on this thread, do AddItem
System.out.println("[" + (new java.util.Date()).toString() + "] " + name + " -buildItem()");
ItemType item = new ItemMonopolyGame().buildItem();
System.out.println("[" + (new java.util.Date()).toString() + "] " + name + " -start AddItem");
AddItemRequestType request = new AddItemRequestType();
request.setItem(item);
AddItemResponseType response = (AddItemResponseType) call.executeByApiName("AddItem", request);
System.out.println("[" + (new java.util.Date()).toString() + "] " + name + " -end AddItem");
.
.
All thread-safe coding practices that you use for Java applications still apply. We also have some specific suggestions to add for coding thread-safe applications using the eBay SDK for Java.
The call object is the object from which the call to the eBay API is made. The call object is either a new ApiCall object (if you are passing a SOAP API request through the one of the SDKs) or a new instance of a wrapper class, for example, a new AddItemCall object.
This line in the sample (MultiThreadTest.java) creates a new ApiCall object:
ApiCall call = new CallManager().setUpCall();
Each thread in MultiThreadTest.java creates a new ApiCall object before it executes. If you are using a wrapper class, the line that creates a call object would look something like this (here we use AddItem as an example, but you can use any wrapper class):
AddItemCall additemcall = new AddItemCall(context);
Be careful when using a shared SimpleDateFormat to format date strings. Refer to the Javadoc for SimpleDateFormat for recommendations.
If you need something like a private static field in a class that has threads, use the java.lang.ThreadLocal class instead (see the Javadoc for recommendations).