• Post Reply Bookmark Topic Watch Topic
  • New Topic

putting time limit on print call  RSS feed

 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 239
  • Mark post as helpful
  • send pies
  • 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?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Erm...what page? What examples?

The page Jeanne linked to.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:The page Jeanne linked to.

Ah. My bad. Apologies.

Winston
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh right, you asked for examples of how to use that.


 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!