Skip to main content

Java - working with Threads



In one of my last posts, i created a Spring based rest api project and hosted it on Heroku. If you would like to read about it, please see below posts -



I was working on a similar project at work where I was using Siebel java databean to create a rest api interface into our Siebel application. I wanted to test the performance of this application. This was more of a quick unit test and I decided to do this by running a Java application with multiple threads calling the API at the same time.

I am posting below the sample program to show how I did it. It is a very basic tutorial giving an example of using threads in Java. It also shows how to make HTTP calls from a java application.

First I will setup a class that will make my HTTP call to the Heroku api I setup -


As you can see in the above class, I am implementing the Runnable interface with it. This is the interface that will help me run it as a thread later. All the code that I want to execute in the thread is within the Run method that I am overriding.

Here I am using the HttpsURLConnection class to make a get call to my heroku web app url in the Run method. Another thing I am doing is capturing the time before the call and after it and finding the difference. This will give me an idea of how much time each call took.

Now let me setup the Main class which will run my threads. I want to run around 10 threads making the call to this api end point.  So lets do that below in a very dumb looking main class -

I called it dumb as it really doesnt do much. All it does was create 10 threads with instances of my other class. Then i started all of them one after the other.

Below you can see the output of each of the calls. Looks like my api on heroku was responding with 500ms each of the 10 times. Not bad performance at all. Of course it is a very simple API with very little data. However I hope you still found this post useful to see how threads can be created and used in Java. Enjoy!

Comments

Popular posts from this blog

Workaround to bypass Salesforce SSO

One of the best practices for implementing Single Sign On for your Salesforce org is always ensure there is a way your System administrator can login via the standard login page side-stepping the SSO configuration.  Reason for this is if ever something goes wrong with your Idp provider's service and the SSO authentication responses are not coming as expected, all your users are unable to login. That is, if you have setup your My domain to prevent logins via standard Salesforce login urls (login.salesforce.com). This includes the System administrator as well. Only if your system administrator can somehow login, then he or she can disable the SSO settings for your domain and allow login via the normal login page as a temporary measure. What do you do in such a situation? Well Salesforce has built a workaround for this which is not well documented anywhere (probably for a good reason :) ). I found out about it from a colleague at work. If your my domain url is - https://Com

Salesforce Big Objects - Key learnings

I remember reading about Salesforce Big Objects before they became available and thought maybe it is an option to backup data present in regular objects. That is, instead of taking a backup via an ETL tool or data loader, maybe this will be an option to backup the data in the Force.com platform itself. Now that it is GA and I am reading more about it, i think the use cases for this are more varied. Some use cases I can think of are –  Archival of key data objects within the platform: You may want to use this option if you dont use any other means to backup key data. Also this may be an attractive option for non-large enterprise customers who dont themselves invest on large data warehouses or data lakes in their enterprise architecture. Ex: customer history (if present in tasks and activities) will become huge over years but this is useful information for reporting & customer analysis. Store key information which is large volume in quantity and also high volume in transa

DBAmp for Salesforce - salesforce integration for SQL Server DBAs

Recently i got the opportunity to explore a tool called DBAmp for integration with Salesforce. I found it to be a very useful tool which will help with any data manipulation requirements in Salesforce. Following are my learnings from the exercise. Hope it helps any of you who may need to work with this tool -  DBAmp is a SQL Server package that can be used to integrate with Salesforce. The site where this software is available is - http://www.forceamp.com/ Overview: It essentially installs on top of an existing SQL Server database and provides an OLE DB connector that can be used to connect to Salesforce. Behind the scenes, it executes API calls against Salesforce for any SQL server command it receives. Thus we can create a connection in SQL server to Salesforce org and pull data into tables in the database just as if we are querying against Salesforce tables directly. Use cases for DBAmap + Salesforce: Many use cases exist for using this tool against Salesforce. Pr