# sendHttpGetRequest - TimeoutException

**URL:** <https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753>\
**Category:** Scripts & Rules\
**Tags:** rules, httpbinding1, sendhttpgetrequest\
**Created:** [June 10, 2017, 9:37am UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753 "2017-06-10T09:37:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dries](https://community.openhab.org/user_avatar/community.openhab.org/dries/32/8797_2.png) [@Dries](https://community.openhab.org/u/Dries)\
**Post date:** [June 10, 2017, 9:37am UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/1 "2017-06-10T09:37:49Z")

</div>

Hi,

I’m using the “sendHttpGetRequest” to send text messages (SMS) to my phone in case certain events occur.  
Yesterday I “missed” an important event. The cause was found in openhab.log:

```auto
2017-06-09 15:53:48.136 [INFO] [g.eclipse.smarthome.model.script.SMS] - SMS API Call initiated
2017-06-09 15:53:53.139 [ERROR] [.smarthome.model.script.actions.HTTP] - Fatal transport error: java.util.concurrent.TimeoutException
2017-06-09 15:53:53.140 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Send SMS when Alarm is triggered': cannot invoke method public boolean java.lang.String.startsWith(java.lang.String) on null

```

The time-out exception is triggered when no response has been received for 5000ms.  
I have now increased this to 10000ms seconds (in http.cfg).

But I was wondering, is there a way to “catch” that exception in my rule? This allows me to execute a plan B (try to send the text again, or send a Pushover message).

Thanks,  
Dries

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 11, 2017, 1:40pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/2 "2017-06-11T13:40:38Z")

</div>

```php
try{
    // Code goes here
}
catch(java.util.concurrent.TimeoutException e){
    logWarn("error", "received timeout exception blah blah blah")
}

```

---

<div class="post-metadata">

**Author:** ![Dries](https://community.openhab.org/user_avatar/community.openhab.org/dries/32/8797_2.png) [@Dries](https://community.openhab.org/u/Dries)\
**Post date:** [June 11, 2017, 3:25pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/3 "2017-06-11T15:25:26Z")

</div>

Thanks!  
I tried to implement your suggestion.  
Since I couldn’t replicate the time-out issue, I changed the domainname I wanted to call. This leads to a “java.nio.channels.UnresolvedAddressException”. I wanted to see if I could catch that.

Here is an extract from my code:

```auto
		var urlmessage = "https://klanten.bizzsms.pnl/api/send?username=" + sms_username + "&code=" + sms_code + "&text=" + sms_text_encoded + "&phonenumbers=" + sms_recipient + "&sendertitle=" + sms_sendertitle
		
		logInfo("SMS","SMS API Call: " + urlmessage)

		try{
    		var sms_returnvalue = sendHttpGetRequest(urlmessage)
		}
		catch(java.nio.channels.UnresolvedAddressException e){
			logWarn("error", "received timeout exception")
			 sms_returnvalue = "Sending SMS failed"
		}
			
		if (sms_returnvalue.startsWith("1|0|")) {
			logInfo ("SMS", "SMS has been delivered")
			true
		} 
		else {
			logInfo ("SMS", "SMS was not delivered! " + sms_returnvalue)
			false
		}

```

When running this code, I still get:

```auto
2017-06-11 17:20:45.986 [INFO] [g.eclipse.smarthome.model.script.SMS] - SMS API Call: https://klanten.bizzsms.pnl/api/send?username=blablabla
2017-06-11 17:20:46.016 [ERROR] [.smarthome.model.script.actions.HTTP] - Fatal transport error: java.util.concurrent.ExecutionException: java.nio.channels.UnresolvedAddressException
2017-06-11 17:20:46.017 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Test SMS functionality': The name 'sms_returnvalue' cannot be resolved to an item or type.

```

So somehow, I didn’t catch it correctly?

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [June 11, 2017, 5:02pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/4 "2017-06-11T17:02:56Z")

</div>

> [@Dries](#):
>
> try{  
> var sms\_returnvalue = sendHttpGetRequest(urlmessage)  
> }

I think that means the var only exists within the { } ?  
Try using var outside of the block, so you are assigning or testing values only inside the blocks.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [June 11, 2017, 7:50pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/5 "2017-06-11T19:50:30Z")

</div>

In addition to what @rossko57 identified, for whatever reason the code is throwing a different exception this time rather than the UnresolvedException.

You can catch Throwable which will catch any and all exceptions that could occur (Throwable is the root class of all Exceptions).

Usually I will put the code fur my entire rule inside the try because of an exception is thrown I don’t want the rest of the code to execute. A quick view of of your code does you don’t want the coffee that is after the catch to run either if the exception is thrown.

---

<div class="post-metadata">

**Author:** ![Dries](https://community.openhab.org/user_avatar/community.openhab.org/dries/32/8797_2.png) [@Dries](https://community.openhab.org/u/Dries)\
**Post date:** [June 12, 2017, 4:13pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/6 "2017-06-12T16:13:54Z")

</div>

Hi both,

Thank you very much. I was able to catch my first exception. 🙂

However, eventually I decided for a different approach.  
I noticed that when the HTTP GET fails, the exception is not really on the first line of the rule-extract mentioned below. Technically it is processed fine, but the value of “sms\_returnvalue” is just ‘null’. So I updated the rule so it does stuff when sms\_returnvalue == null.

```auto
var sms_returnvalue = sendHttpGetRequest(urlmessage)
if (sms_returnvalue == null) {
			// Do stuff here in case the HTTP GET throws an error/is unreachable/...
}

```

---

<div class="post-metadata">

**Author:** ![AlexKay](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@AlexKay](https://community.openhab.org/u/AlexKay)\
**Post date:** [August 23, 2017, 9:42am UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/7 "2017-08-23T09:42:09Z")

</div>

Hi @rlkoshak,  
Unfortunately I am not able to catch a TimeoutException when using sendHttpGetRequest in openHAB 2.1 (stable) along with your code example ☹

Do I miss an import statement or anything else?  
Instead of “// Code goes here” from your example, I replaced it with “sendHttpGetRequest([http://LongRunningURL](http://LongRunningURL), 100)”  
LongRunningURL takes 20s to complete, so 100ms is definitely to short to let it complete.

It still throws an TimeoutException in openhab.log and I am not able to catch it successfully ☹

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 23, 2017, 2:00pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/8 "2017-08-23T14:00:20Z")

</div>

Change the catch to catch all exceptions

```
catch(Exception e){

```

---

<div class="post-metadata">

**Author:** ![AlexKay](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@AlexKay](https://community.openhab.org/u/AlexKay)\
**Post date:** [August 25, 2017, 8:55am UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/9 "2017-08-25T08:55:57Z")

</div>

Hello Community,  
it drives me crazy… I am still not able to catch a TimeoutException, Exception or any Throwable ☹  
As you can see, it is a simple rule which does not need a thing, an item or anything else.  
openHAB 2.1 stable is installed on Debian Linux 9.0 (was the same with 8.x) using openjdk-8-jre-headless

```javascript
rule "Test1"
	when
		Time cron "1 * * * * ?"
	then
		logInfo("RULE","@R Test1: start try-catch-block")
		try{
			// TimeoutException expected, because URL needs 20000 ms to complete
			sendHttpGetRequest(http://LongRunningURL, 100)
		}
		//catch(java.util.concurrent.TimeoutException e){
		//catch(Exception e){
		catch(Throwable t){
			logInfo("RULE","@R Test1: catched exception/throwable")
		}
		logInfo("RULE","@R Test1: end try-catch-block")
end

```

/var/log/openhab2/openhab.log :

```javascript
2017-08-25 10:35:01.000 [INFO] [.eclipse.smarthome.model.script.RULE] - @R Test1: start try-catch-block
2017-08-25 10:35:01.101 [ERROR] [.smarthome.model.script.actions.HTTP] - Fatal transport error: java.util.concurrent.TimeoutException
2017-08-25 10:35:01.102 [INFO] [.eclipse.smarthome.model.script.RULE] - @R Test1: end try-catch-block

```

Do I miss an import statetment? Anything else?  
Your ideas are welcome!  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 25, 2017, 12:47pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/10 "2017-08-25T12:47:55Z")

</div>

Based on the behavior I’m guessing the http action code catches the timeout itself, logs it and doesn’t rethrow it so the exception never gets to your rule code.

You will have to detect the timeout some other way.

Does the action return anything when it times out? Even if it returns nothing but you are expecting something that can be enough to detect the timeout.

If not, you may have to implement the call using the native Java classes. Look for the iCloud Integration tutorial for an example.

---

<div class="post-metadata">

**Author:** ![AlexKay](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@AlexKay](https://community.openhab.org/u/AlexKay)\
**Post date:** [August 25, 2017, 7:56pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/11 "2017-08-25T19:56:12Z")

</div>

Hi @rlkoshak,  
thanks again for spending your time and your hints.  
Your explanation that the exception is catched in sendHttpGetRequest and not rethrown sounds reasonable, especially when I look again at the log entry which looks formatted by another class.

My primary goal is to trigger this HTTP request and continue processing in openhab without waiting until HTTP has finished (it triggers recording from 2 cameras handled by another system).  
I need openHAB avoid waiting 20 seconds.

Is it possible to send sendHttpGetRequest(MyURL, 20000) to a background thread? In background it may take its time to complete.  
Otherwise I consider calling a shell script which returns immediately while e.g. curl MyURL finishes in background , so that openhab can continue.  
…or simply live with the “fatal transport error” in openhab.log

Good hint implementing it in Java, but my Java experience has been 15 years ago without deeper practice since…it would take me ages 🙂

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 25, 2017, 8:53pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/12 "2017-08-25T20:53:31Z")

</div>

> [@AlexKay](#):
>
> My primary goal is to trigger this HTTP request and continue processing in openhab without waiting until HTTP has finished (it triggers recording from 2 cameras handled by another system).
> 
> I need openHAB avoid waiting 20 seconds.

All of openHAB doesn’t wait for 20 seconds. Just that one rule. All your other rules and everything else will continue to run.

However, if you need to process the rest of that rule without waiting then you can apply the [Separation of Behaviors Design Pattern](https://community.openhab.org/t/design-pattern-separation-of-behaviors/15886). the tl;dr is to send a command to an unbound Item which triggers a rule to make the http request.

That being said, given your stated goal, what did you hope to accomplish by catching the TimeoutException? Even if the Action threw the exception back into the rule you would still have had to wait the 20 seconds in order to catch it.

> [@AlexKay](#):
>
> Good hint implementing it in Java, but my Java experience has been 15 years ago without deeper practice since…it would take me ages 🙂

That is why I sent you to a posting with an actual working example. Which, now that I’m not on my phone I can more easily provide the actual link:

> [@iCloud device data integration in openHAB](https://community.openhab.org/t/icloud-device-data-integration-in-openhab/32329):
>
> Hi all. go get rid of my last dependency to MiOS/Vera I´ve started to create a rule to get the location of an iPhone directly from the iCloud. With the feedback from @ThomDietrich. @hmerk & the community the example evolved and is now able to retrieve quite some device specific information for multiple devices: Name: John’s iPhone Owner: John Doe Unique ID: VIRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx BatteryStatus: NotCharging BatteryLevel: 0.8100000023841858 Latitude: 51.xxxxxxxxx L…

---

<div class="post-metadata">

**Author:** ![AlexKay](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@AlexKay](https://community.openhab.org/u/AlexKay)\
**Post date:** [August 30, 2017, 7:57am UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/13 "2017-08-30T07:57:08Z")

</div>

Hi @rlkoshak,  
I was not precise enough. I meant the rule waiting, not all of openHAB.  
My try was to

```auto
sendHttpGetRequest(http://LongRunningURL, 100)

```

with a Timeout of 100 ms, in order to catch the TimeoutException very early and ignoring it instead of an error message in log like “Fatal transport error: java.util.concurrent.TimeoutException”.

But now I found a less complex way to accomplish the rule not waiting 20,000 ms. I simply create a Timer which fires immediately and executes sendHttpGetRequest in a parallel thread:

```
createTimer(now) [|
     sendHttpGetRequest(http://LongRunningURL, 20000)
     logInfo("RULE","LongRunningURL finished")
]

```

“Separation of Behaviors Design Pattern” is great! I will use it to evolve my rule file and centralize repeating logic in one place which can be maintained better!

I appreciate your help very much! Thanks!

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [August 30, 2017, 12:22pm UTC](https://community.openhab.org/t/sendhttpgetrequest-timeoutexception/29753/14 "2017-08-30T12:22:12Z")

</div>

> [@AlexKay](#):
>
> I was not precise enough. I meant the rule waiting, not all of openHAB.

But rules are already executed in separate threads. You could put a timeout of 20000000000 and only that one rule will be waiting for a response. It won’t cause all of OH to stop and wait and it won’t stop other rules or even other instances of that rule from executing.
