New SDKs Released!
We have released new versions of our SDKs, now supporting the latest version of our APIs. Explore them here.
Threading in the Java SDK
Threads in a Java program help you achieve parallelism. By using multiple threads, you can make a Java program run faster and do multiple things at the same time.
The Java SDK supports both single-threading and multi-threading irrespective of a single user or a multi user app.
Refer to the below code snippets that use multi-threading for a single-user and multi-user app.
Multi-threading in a Multi-user App
- The program execution starts from main().
- The details of "user1" are given in the variables user1, token1, environment1.
- Similarly, the details of another user "user2" are given in the variables user2, token2, environment2.
- For each user, an instance of MultiThread class is created.
- When start() is called which in-turn invokes run(), the details of user1 are passed to the switchUser function through the MultiThread object. Therefore, this creates a thread for user1.
- Similarly, When start() is invoked again, the details of user2 are passed to the switchUser function through the MultiThread object. Therefore, this creates a thread for user2.
package com.zoho.crm.sample.threading.multiuser;
import com.zoho.api.authenticator.OAuthToken;
import com.zoho.api.authenticator.Token;
import com.zoho.api.authenticator.OAuthToken.TokenType;
import com.zoho.api.authenticator.store.DBStore;
import com.zoho.api.authenticator.store.TokenStore;
import com.zoho.api.logger.Logger;
import com.zoho.crm.api.Initializer;
import com.zoho.crm.api.RequestProxy;
import com.zoho.crm.api.SDKConfig;
import com.zoho.crm.api.UserSignature;
import com.zoho.crm.api.dc.USDataCenter;
import com.zoho.crm.api.dc.DataCenter.Environment;
import com.zoho.crm.api.exception.SDKException;
import com.zoho.crm.api.record.RecordOperations;
import com.zoho.crm.api.util.APIResponse;
public class MultiThread extends Thread {
Environment environment;
UserSignature user;
Token token;
String moduleAPIName;
RequestProxy userProxy;
SDKConfig sdkConfig;
public MultiThread(UserSignature user, Environment environment, Token token, String moduleAPIName, SDKConfig config, RequestProxy proxy) {
this.environment = environment;
this.user = user;
this.token = token;
this.moduleAPIName = moduleAPIName;
this.sdkConfig = config;
this.userProxy = proxy;
}
public void run() {
try {
Initializer.switchUser(user, environment, token, sdkConfig, userProxy);
System.out.println(Initializer.getInitializer().getUser().getEmail());
RecordOperations cro = new RecordOperations();
@SuppressWarnings("rawtypes")
APIResponse getResponse = cro.getRecords(this.moduleAPIName, null, null);
System.out.println(getResponse.getObject());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws SDKException {
Logger loggerInstance = Logger.getInstance(Logger.Levels.ALL, "/Users/abc-1234/Documents/AutomateSDK/java/GitLab/sdk.log");
Environment env = USDataCenter.PRODUCTION;
UserSignature user1 = new UserSignature("abc@xyz.com");
TokenStore tokenstore = new DBStore(null, null, null, "abc@1234", null);
Token token1 = new OAuthToken("1000xxx0e16", "1000xxxe83", TokenType.REFRESH, "https://www.zoho.com");
String resourcePath = "/Users/username/Documents/zohocrm-javasdk-sample-application";
SDKConfig user1Config = new SDKConfig.Builder().setAutoRefreshFields(false).setPickListValidation(true).build();
Initializer.initialize(user1, env, token1, tokenstore, user1Config, resourcePath, loggerInstance);
MultiThread multiThread = new MultiThread(user1, env, token1, "Students", user1Config, null);
multiThread.start();
Environment environment = USDataCenter.PRODUCTION;
UserSignature user2 = new UserSignature("user2@xyz.com");
Token token2 = new OAuthToken("1000xxxda7f", "1000xxxxa42", TokenType.REFRESH);
RequestProxy user2Proxy = new RequestProxy("proxyHost", 80, "proxyUser", "password", "userDomain");
SDKConfig user2Config = new SDKConfig.Builder().setAutoRefreshFields(true).setPickListValidation(false).build();
multiThread = new MultiThread(user2, environment, token2, "Leads", user2Config, user2Proxy);
multiThread.start();
}
}
Multi-threading in a Single-user App
package threading.singleuser;
import com.zoho.api.authenticator.OAuthToken;
import com.zoho.api.authenticator.Token;
import com.zoho.api.authenticator.OAuthToken.TokenType;
import com.zoho.api.authenticator.store.DBStore;
import com.zoho.api.authenticator.store.TokenStore;
import com.zoho.api.exception.SDKException;
import com.zoho.crm.api.Initializer;
import com.zoho.crm.api.UserSignature;
import com.zoho.crm.api.dc.USDataCenter;
import com.zoho.crm.api.dc.DataCenter.Environment;
import com.zoho.crm.api.logger.Logger;
import com.zoho.crm.api.record.RecordOperations;
import com.zoho.crm.api.util.APIResponse;
public class MultiThread extends Thread
{
String moduleAPIName;
public MultiThread(String moduleAPIName)
{
this.moduleAPIName = moduleAPIName;
}
public void run()
{
try
{
RecordOperations record = new RecordOperations();
@SuppressWarnings("rawtypes")
APIResponse getResponse = record.getRecords(null, null, this.moduleAPIName);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws SDKException
{
Logger logger = Logger.getInstance(Levels.INFO, "/Users/user_name/Documents/java-sdk-logs.log");
Environment environment = USDataCenter.PRODUCTION;
TokenStore tokenStore = new FileStore("/Users/user_name/Documents/java-sdk-tokens.txt");
UserSignature user = new UserSignature("user1@zoho.com");
Token token = new OAuthToken("clientId1", "clientSecret1", "REFRESH/GRANT token", TokenType.REFRESH/GRANT);
Boolean autoRefreshFields = true;
String resourcePath = "/Users/user_name/Documents/javasdk-application";
Initializer.initialize(user1, environment, token1, tokenStore, autoRefreshFields, resourcePath, logger);
MultiThread mtsu = new MultiThread("Deals");
mtsu.start();
mtsu = new MultiThread("Leads");
mtsu.start();
}
}
Single-threading in a Multi-user App
package com.zoho.crm.sample.threading.multiuser;
import com.google.gson.Gson;
import com.zoho.api.authenticator.OAuthToken;
import com.zoho.api.authenticator.Token;
import com.zoho.api.authenticator.OAuthToken.TokenType;
import com.zoho.api.authenticator.store.DBStore;
import com.zoho.api.authenticator.store.TokenStore;
import com.zoho.crm.api.Initializer;
import com.zoho.crm.api.UserSignature;
import com.zoho.crm.api.dc.USDataCenter;
import com.zoho.crm.api.dc.DataCenter.Environment;
import com.zoho.crm.api.logger.Logger;
import com.zoho.crm.api.record.RecordOperations;
import com.zoho.crm.api.util.APIResponse;
public class SingleThread
{
Environment environment;
UserSignature user;
Token token;
String moduleAPIName;
public SingleThread( String moduleAPIName)
{
this.moduleAPIName = moduleAPIName;
}
public SingleThread(UserSignature user, Environment environment, Token token, String moduleAPIName)
{
this.environment= environment;
this.user = user;
this.token = token;
this.moduleAPIName = moduleAPIName;
}
public void run()
{
try
{
Initializer.switchUser(user, environment, token, false);
System.out.println(Initializer.getInitializer().getUser().getEmail());
RecordOperations cro = new RecordOperations();
Gson gson = new Gson();
@SuppressWarnings("rawtypes")
APIResponse getResponse = cro.getRecords(null, null, this.moduleAPIName);
System.out.println(gson.toJson(getResponse.getObject()));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Logger loggerInstance = Logger.getInstance(Logger.Levels.ALL, "/Users/username/Documents");
Environment env = USDataCenter.PRODUCTION;
UserSignature user1 = new UserSignature("p.boyle@abc.com");
TokenStore tokenstore = new DBStore(null, null, null, "password", null);
Token token1 = new OAuthToken("1000xxxxxKI3DH", "5500xxxxxx94cb2a", "https://www.zoho.com", "1000xxxxxxfc2401", TokenType.REFRESH);
String resourcePath = "/Users/username/Documents";
Initializer.initialize(user1, env, token1, tokenstore, sdkConfig, resourcePath, loggerInstance);
SingleThread singleThread = new SingleThread(user1, env, token1, "Students");
singleThread.run();
Environment environment = USDataCenter.PRODUCTION;
UserSignature user2 = new UserSignature("boyle1@abc.com");
Token token2 = new OAuthToken("1000xxxxxYZX3", "efd63xxxxxxe786c", "https://www.zoho.com", "1000xxxxxx4286ad", TokenType.REFRESH);
singleThread = new SingleThread(user2, environment, token2, "Leads");
singleThread.run();
}
}
Single-threading in a Single-user App
package com.zoho.crm.sample.threading.singleuser;
import com.google.gson.Gson;
import com.zoho.api.authenticator.OAuthToken;
import com.zoho.api.authenticator.Token;
import com.zoho.api.authenticator.OAuthToken.TokenType;
import com.zoho.api.authenticator.store.DBStore;
import com.zoho.api.authenticator.store.TokenStore;
import com.zoho.api.exception.SDKException;
import com.zoho.crm.api.Initializer;
import com.zoho.crm.api.UserSignature;
import com.zoho.crm.api.dc.USDataCenter;
import com.zoho.crm.api.dc.DataCenter.Environment;
import com.zoho.crm.api.logger.Logger;
import com.zoho.crm.api.record.RecordOperations;
import com.zoho.crm.api.util.APIResponse;
public class SingleThread extends Thread
{
String moduleAPIName;
public SingleThread(String moduleAPIName)
{
this.moduleAPIName = moduleAPIName;
}
public void run()
{
try
{
System.out.println(Initializer.getInitializer().getUser().getEmail());
RecordOperations cro = new RecordOperations();
Gson gson = new Gson();
@SuppressWarnings("rawtypes")
APIResponse getResponse = cro.getRecords(null, null, this.moduleAPIName);
System.out.println(gson.toJson(getResponse.getObject()));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws SDKException
{
Logger loggerInstance = Logger.getInstance(Logger.Levels.ALL, "/Users/username/Documents");
Environment env = USDataCenter.PRODUCTION;
UserSignature user = new UserSignature("p.boyle@abc.com");
TokenStore tokenstore = new DBStore(null, null, null, "password", null);
Token token1 = new OAuthToken("1000xxxxx3DH", "5500xxxxxxcb2a", "https://www.zoho.com", "1000xxxxxa7fc2401", TokenType.REFRESH);
String resourcePath = "/Users/username/Documents";
Initializer.initialize(user, env, token1, tokenstore, sdkConfig, resourcePath, loggerInstance, userProxy);
SingleThread stsu = new SingleThread("Leads");
stsu.start();
}
}