• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can i call my asynchronous task via synchronous method ?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I wrote an application which is sending Sip Invite Message to a destination. After some period (10 ms,100 ms,whatever) response message comes from destination. I'm doing some businnes in processResponse.
So this issues are asynchronous!!! By the way i'm using nist library for sip messaging.

Another project team requesting a library from me. They want to use my application by calling synchronously.

What is the simplest way to do this ?
How can i call my asynchronous task via synchronous method ?

Could you share code samples,if you have any ?

Best Regards.





 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)send request
2) poll for response in a loop, sleep for sometime in the loop
3) return response to caller.
 
alp carsikarsi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jayesh. is this solution works on heavy simultaneus request load ?

Best regards.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what exactly is asynchronous? Just use a blocking stream/channel and when all data has arrived you finish up.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

alp carsikarsi wrote:Thanks jayesh. is this solution works on heavy simultaneus request load ?

Best regards.



Any solution that uses a synchronous call that waits on a background job to complete will have an inherent cost:- The thread that called your synchronous method will wait till the background process is complete. This means that if your app server spawns a thread for each request, there is one more thread for the OS to keep track off, or if the app serves uses a thread pool (as most app servers do), there is one less thread to service other requests. Either way, you will be bottlenecking. This may or may not be a big deal depending on the frequency of requests, and time taken to process each request.

Besides that, if you use the mechanism that I described, there will be an overhead while you continuously poll for completion. It depends on what the cost of each call is. I am not familiar with the library that you are using. If the mechanism that Stephen described is available, it should be less costly, since you will be spending more time in wait
 
alp carsikarsi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the attachment i sent the flow.
a.jpeg
[Thumbnail for a.jpeg]
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After you send the request, how would you then retrieve the response? Do you need to provide a "callback" to the library?

If so, then you should write an await() signal() notification system between the initial thread and the callback thread.

Note that I wrote this code from the top of my head and I did not compile or test it. Use it only to give you an idea of how to solve the problem.
 
alp carsikarsi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Stephan van Hulst...

I solved my problem. I'm grateful.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic