• 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

putting time limit on print call

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


Sometimes the method gets stuck and doesnt return. Is there built in expiration mechanism on the call? If not what is the simplest way for it to always return after set amount of time?

Thanks
 
author & internet detective
Posts: 41905
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
Take a look at the Future interface. It lets you execute in a new Callable (like a thread) with a timeout.
 
John Landon
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt quite understand how this works. Is there any example? The examples in the page dont really explain anything.
How do I use it in my context?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Landon wrote:I didnt quite understand how this works.


Actually, I don't quite understand how your code works, because 'document' is null.

I can only assume that silentPrint() is a static method, and that the document variable is actually redundant, since then the same thing could be done with:
PDDocument.silentPrint(printJob);

but not knowing the API you're using, I'm not sure.

Is there any example? The examples in the page dont really explain anything.
How do I use it in my context?


Erm...what page? What examples? What context?

You've given us three lines of code to work with John. We're good, but we're not magicians.

Winston
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
Erm...what page? What examples?


The page Jeanne linked to.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:The page Jeanne linked to.


Ah. My bad. Apologies.

Winston
 
John Landon
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course the pdf is not null it has document in it.

The question is more general is how to timeout any call in java?
 
Marshal
Posts: 28258
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Landon wrote:The question is more general is how to timeout any call in java?



The answer which Jeanne gave as the very first response in this thread is the answer to that question as well.
 
Paul Clapham
Marshal
Posts: 28258
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh right, you asked for examples of how to use that.


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

Paul Clapham wrote:Oh right, you asked for examples of how to use that.




Thanks.

So I wrote this and it never hits the sleep breakpoint (never gets into the thread) and after 10 seconds just throws TimoutException.....

 
Paul Clapham
Marshal
Posts: 28258
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like I missed part of the solution. Try this:



You'll notice that the "Tick" appears repeatedly, showing that the long-running task is running correctly, and you'll notice that the TimeoutException happens after 10 seconds. In other words the "future.get()" call waits for up to 10 seconds and then gives up waiting. Change the 100 in the long-running loop to 2 and you'll see that the method terminates without a TimeoutException being thrown.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic