Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Cat 3240 java sdk fix connection concurrency#25

Merged
lfavaro-daitan merged 5 commits intomasterfrom
CAT-3240_Java_SDK_Fix_connection_concurrency
Jun 10, 2015
Merged

Cat 3240 java sdk fix connection concurrency#25
lfavaro-daitan merged 5 commits intomasterfrom
CAT-3240_Java_SDK_Fix_connection_concurrency

Conversation

@lfavaro-daitan
Copy link
Contributor

This fix implements a PoolingHttpClientConnectionManager into BandwidthClient, so client could support singleton pattern so allowing concurrency.

In order to implement PoolingHttpClientConnectionManager, these recommendations were followed:
https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

Other fix was to use Joda time, since SimpleDateFormat caused concurrency issues as well.

Most of the libraries were updated to its latest available release.

I have used this algorithm to validate the changes:

public static void main(String[] args) throws Exception {
BandwidthClient client = BandwidthClient.getInstance();
client.setCredentials("",
"",
"");
client.setEndpointandVersion("", "v1");
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 100; i++) {
executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    // The resource access do not support multi-threads accessing the same connection.
                    // With this code it is expected to see the following error:
                    //
                    // java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
                    // Make sure to release the connection before allocating another one.
                    // at org.apache.http.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:162)
                    // at org.apache.http.impl.conn.BasicClientConnectionManager$1.getConnection(BasicClientConnectionManager.java:139)
                    // at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:456)
                    // at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
                    // at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
                    // at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
                    // at com.bandwidth.sdk.BandwidthClient.performRequest(BandwidthClient.java:350)
                    // at com.bandwidth.sdk.BandwidthClient.request(BandwidthClient.java:338)
                    // at com.bandwidth.sdk.BandwidthClient.get(BandwidthClient.java:228)
                    // at com.bandwidth.sdk.model.Call.get(Call.java:48)
                    // at com.bandwidth.sdk.model.Call.get(Call.java:34)

                    // Call tests, create, hang and get call
                    Call call = Call.create("<to>", "<from>");
                    Call call2 = Call.get(call.getId());
                    System.out.println(call2);

                    Thread.sleep(3000);
                    call.hangUp();
                    Thread.sleep(1000);

                    call2 = Call.get(call.getId());
                    Assert.assertEquals("completed", call2.getState());
                    System.out.println("Call completed: " + call2.getId() + "-" + call2.getState());

                    // Message tests
                    Message message = Message.create("<to>", "<from>", "Hi", ReceiptRequest.ALL);
                    Message message2 = Message.get(message.getId());
                    System.out.println(message);
                    Thread.sleep(2000);
                    message2 = Message.get(message2.getId());
                    Assert.assertEquals("sent", message2.getState());
                    System.out.println("Message sent: " + message2.getId() + "-" + message2.getText() + "-" + message2.getState() + "-" + message2.getReceiptRequested());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // Wait to ensure that one thread will return the expected result
    executor.awaitTermination(3, TimeUnit.SECONDS);
    executor.shutdown();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert the string values to Integer in this step and throw exception specifying what is wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

lfavaro-daitan pushed a commit that referenced this pull request Jun 10, 2015
…tion_concurrency

Cat 3240 java sdk fix connection concurrency
@lfavaro-daitan lfavaro-daitan merged commit 9426c63 into master Jun 10, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants