Timeout on http call using HttpUtil

I think it is not related to a specific URI

Java shows connect timeouts, which would correspond to your analysis
Example 1 from Shelly Binding thread
Example 2 from Shelly Binding thread

Is is the dump from the first example?

Yes it is from the use of the dev-version of the binding and it is the same situation the dump was made. But it is not actually the one the dump was made.

update: Alterco confirmed that this could be related to the firmware. The OS running on the device does not have multi tasking capabilities. Therefore it can’t process multiple requests at the same time, which means while sending data (e.g. their cloud or sending http event updates to the App) a REST API request will not be processed, which then leads into a connect timeout. I’m in discussion with them how to fix this or find a work around. Their recommendation: Use http only to issue commands and Coap to receive status updates, but this requires some extension to their Coap status updates. They confirmed to work on that and have it on the roadmap for version 1.6 - let’s see.

In parallel I’ll include a work around: Retry on connect timeout or connection reset. Not sure if that could be achieved by using HttpUtil or if I need to keep the switch to java.net.HttpURLConnection.

1 Like

I suppose it should work as well with HttpUtil because the logs did not show anything that should be working completely different with direct JDK http connections. But if it’s needed it’s needed.

how to gwt the info on connect timeout or connection reset from HttpUtil, „Timeout“ as http response strong is very unspecific, I would also prefer the http response code rather than the response string

Then please use the HttpClientFactory it will give you full access to a managed Jetty client. And Jetty is default implementation for openHAB (HttpUtil uses it internally as well)

Hi @martinvw,

I try to switch to HttpClient. I changed the code, Eclipse doesn’t show an error, but when I try to result the dependencies ([Resolve]) I get the following error:

When I run mvn I get

[ERROR] Failed to execute goal org.apache.karaf.tooling:karaf-maven-plugin:4.2.7:verify (karaf-feature-verification) on project org.openhab.binding.shelly: Feature resolution failed for [openhab-binding-shelly/2.5.3.SNAPSHOT]
[ERROR] Message: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=openhab-binding-shelly; type=karaf.feature; version=2.5.3.SNAPSHOT; filter:="(&(osgi.identity=openhab-binding-shelly)(type=karaf.feature)(version>=2.5.3.SNAPSHOT))" [caused by: Unable to resolve openhab-binding-shelly/2.5.3.SNAPSHOT: missing requirement [openhab-binding-shelly/2.5.3.SNAPSHOT] osgi.identity; osgi.identity=org.openhab.binding.shelly; type=osgi.bundle; version="[2.5.3.202003122105,2.5.3.202003122105]"; resolution:=mandatory [caused by: Unable to resolve org.openhab.binding.shelly/2.5.3.202003122105: missing requirement [org.openhab.binding.shelly/2.5.3.202003122105] osgi.wiring.package; filter:="(&(osgi.wiring.package=io.netty.handler.codec.http)(version>=4.1.0)(!(version>=5.0.0)))"]]
[ERROR] Repositories: {
[ERROR] 	file:/Users/markus/Dev/openhab-2-5-x/git/openhab-addons/bundles/org.openhab.binding.shelly/target/feature/feature.xml
[ERROR] 	mvn:org.apache.karaf.features/framework/4.2.7/xml/features
[ERROR] 	mvn:org.apache.karaf.features/standard/4.2.7/xml/features
[ERROR] 	mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/2.5.0/xml/features
[ERROR] 	mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-tp/2.5.0/xml/features
[ERROR] 	mvn:org.ops4j.pax.web/pax-web-features/7.2.11/xml/features
[ERROR] }
...

I checked other bindings, but can’t find additional dependencies for feature.xml, pom.xml etc.

I added the following to the HandlerFactory and Discovery handler

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;

public class ShellyHandlerFactory extends BaseThingHandlerFactory {
    private @Nullable HttpClient httpClient;
...
}

   @Reference
    protected void setHttpClientFactory(HttpClientFactory httpClientFactory) {
        this.httpClient = httpClientFactory.getCommonHttpClient();
    }

    protected void unsetHttpClientFactory(HttpClientFactory httpClientFactory) {
        this.httpClient = null;
    }

also adding @Reference HttpClient httpClient to the injected constructor’s parameter list doesn’t work.

Any idea?

Do you have the branch pushes somewhere? Does it work in Maven?

Did you add them to feature.xml:

I would expect the transport-http like in: https://github.com/openhab/openhab-addons/blob/88c489091862784167ce0880c4fbde2d8a0b6c0c/bundles/org.openhab.binding.spotify/src/main/feature/feature.xml

Hi,

those are my dependencies

    <feature name="openhab-binding-shelly" description="Shelly Binding" version="${project.version}">
        <feature>openhab-runtime-base</feature>        
        <feature>openhab-transport-http</feature>
        <feature>openhab-transport-coap</feature>
        <feature>openhab-transport-mdns</feature>
        <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.shelly/${project.version}</bundle>
    </feature>

My repo ist here (branch shelly_snapshot)

@martinvw I checked my CarNet binding, which is under development. There I also use jetty httpClient and event don’t have openhab-transport-http in feature.xml

I must admit that I didn’t develop bindings from scratch so I did not have to work with such things that often. Maybe one of the other guys knows maybe @J-N-K, @wborn or @hilbrand? Thanks

just found out that it works without the org.openhab.transport-http line - he he

2 Likes

grr, after debuggung was finised (everything is working fine) I tried to generate a new jar and hit the same thing (at least loiks like that):

ERROR] Failed to execute goal org.apache.karaf.tooling:karaf-maven-plugin:4.2.7:verify (karaf-feature-verification) on project org.openhab.binding.shelly: Feature resolution failed for [openhab-binding-shelly/2.5.3.SNAPSHOT]
[ERROR] Message: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=openhab-binding-shelly; type=karaf.feature; version=2.5.3.SNAPSHOT; filter:="(&(osgi.identity=openhab-binding-shelly)(type=karaf.feature)(version>=2.5.3.SNAPSHOT))" [caused by: Unable to resolve openhab-binding-shelly/2.5.3.SNAPSHOT: missing requirement [openhab-binding-shelly/2.5.3.SNAPSHOT] osgi.identity; osgi.identity=org.openhab.binding.shelly; type=osgi.bundle; version="[2.5.3.202003151920,2.5.3.202003151920]"; resolution:=mandatory [caused by: Unable to resolve org.openhab.binding.shelly/2.5.3.202003151920: missing requirement [org.openhab.binding.shelly/2.5.3.202003151920] osgi.wiring.package; filter:="(&(osgi.wiring.package=io.netty.handler.codec.http)(version>=4.1.0)(!(version>=5.0.0)))"]]
[ERROR] Repositories: {
[ERROR] 	file:/Users/markus/Dev/openhab-2-5-x/git/openhab-addons/bundles/org.openhab.binding.shelly/target/feature/feature.xml
[ERROR] 	mvn:org.apache.karaf.features/framework/4.2.7/xml/features
[ERROR] 	mvn:org.apache.karaf.features/standard/4.2.7/xml/features
[ERROR] 	mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/2.5.0/xml/features
[ERROR] 	mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-tp/2.5.0/xml/features
[ERROR] 	mvn:org.ops4j.pax.web/pax-web-features/7.2.11/xml/features
[ERROR] }
[ERROR] Resources: {
[ERROR] 	mvn:com.eclipsesource.jaxrs/publisher/5.3.1
[ERROR] 	mvn:com.google.guava/failureaccess/1.0.1
[ERROR] 	mvn:com.google.guava/guava/18.0
[ERROR] 	mvn:com.google.guava/guava/27.1-jre
[ERROR] 	mvn:commons-codec/commons-codec/1.6
[ERROR] 	mvn:commons-collections/commons-collections/3.2.1
[ERROR] 	mvn:commons-io/commons-io/2.2
[ERROR] 	mvn:commons-lang/commons-lang/2.6
[ERROR] 	mvn:commons-net/commons-net/3.3
[ERROR] 	mvn:de.jollyday/jollyday/0.5.8
[ERROR] 	mvn:de.maggu2810.p2redist/com.google.inject/3.0.0.v201312141243
[ERROR] 	mvn:de.maggu2810.p2redist/org.antlr.runtime/3.2.0.v201101311130
[ERROR] 	mvn:javax.annotation/javax.annotation-api/1.2
[ERROR] 	mvn:javax.annotation/javax.annotation-api/1.3

@J-N-K @wborn @hilbrand Any idea?

The HttpClientFactory is provided by the org.openhab.core.io.net bundle for which you don’t need any additional feature dependencies.

It might work better if you also use constructor injection for the HttpClientFactory not the HttpClient. It’s impossible to inject the HttpClientFactory into the ShellyHandlerFactory using the setter before it has been created using the constructor.

ok, inserted HttpClientFactory in the HandlerFactory’s contructor, this works

Does the MDNSDiscoveryParticipant also supports constructor injetion?

What’s about a HttpServlet?
public class** ShellyEventServlet extends HttpServlet {
does this also also supports constructor injection?

Nevertheless mvn still reports the error:

[ERROR] Failed to execute goal org.apache.karaf.tooling:karaf-maven-plugin:4.2.7:verify (karaf-feature-verification) on project org.openhab.binding.shelly: Feature resolution failed for [openhab-binding-shelly/2.5.3.SNAPSHOT]
[ERROR] Message: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=openhab-binding-shelly; type=karaf.feature; version=2.5.3.SNAPSHOT; filter:="(&(osgi.identity=openhab-binding-shelly)(type=karaf.feature)(version>=2.5.3.SNAPSHOT))" [caused by: Unable to resolve openhab-binding-shelly/2.5.3.SNAPSHOT: missing requirement [openhab-binding-shelly/2.5.3.SNAPSHOT] osgi.identity; osgi.identity=org.openhab.binding.shelly; type=osgi.bundle; version="[2.5.3.202003152148,2.5.3.202003152148]"; resolution:=mandatory [caused by: Unable to resolve org.openhab.binding.shelly/2.5.3.202003152148: missing requirement [org.openhab.binding.shelly/2.5.3.202003152148] osgi.wiring.package; filter:="(&(osgi.wiring.package=io.netty.handler.codec.http)(version>=4.1.0)(!(version>=5.0.0)))"]]
[ERROR] Repositories: {
[ERROR] 	file:/Users/markus/Dev/openhab-2-5-x/git/openhab-addons/bundles/org.openhab.binding.shelly/target/feature/feature.xml
[ERROR] 	mvn:org.apache.karaf.features/framework/4.2.7/xml/features
[ERROR] 	mvn:org.apache.karaf.features/standard/4.2.7/xml/features
[ERROR] 	mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/2.5.0/xml/features
[ERROR] 	mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-tp/2.5.0/xml/features
[ERROR] 	mvn:org.ops4j.pax.web/pax-web-features/7.2.11/xml/features
[ERROR] }
[ERROR] Resources: {
[ERROR] 	mvn:com.eclipsesource.jaxrs/publisher/5.3.1
[ERROR] 	mvn:com.google.guava/failureaccess/1.0.1
[ERROR] 	mvn:com.google.guava/guava/18.0
[ERROR] 	mvn:com.google.guava/guava/27.1-jre
[ERROR] 	mvn:commons-codec/commons-codec/1.6
[ERROR] 	mvn:commons-collections/commons-collections/3.2.1
[ERROR] 	mvn:commons-io/commons-io/2.2
[ERROR] 	mvn:commons-lang/commons-lang/2.6
[ERROR] 	mvn:commons-net/commons-net/3.3
[ERROR] 	mvn:de.jollyday/jollyday/0.5.8
[ERROR] 	mvn:de.maggu2810.p2redist/com.google.inject/3.0.0.v201312141243
[ERROR] 	mvn:de.maggu2810.p2redist/org.antlr.runtime/3.2.0.v201101311130
[ERROR] 	mvn:javax.annotation/javax.annotation-api/1.2
[ERROR] 	mvn:javax.annotation/javax.annotation-api/1.3
[ERROR] 	mvn:javax.el/javax.el-api/3.0.0
[ERROR] 	mvn:javax.mail/mail/1.4.7
[ERROR] 	mvn:javax.measure/unit-api/1.0
[ERROR] 	mvn:javax.servlet/javax.servlet-api/3.1.0
[ERROR] 	mvn:javax.validation/validation-api/1.1.0.Final

I don’t get why this was no problem in my CarNet binding

It should work for anything that is instantiated using the @Component annotations.

That’s because you should use the jetty instead of the netty import in ShellyHttpApi.

The computer will be very happy if you switch it to:

import org.eclipse.jetty.http.HttpMethod;

top, this works. You made my day!
also switched DiscoveryParticipant and Servlet to instric constructors. Nice side effect:

  • makes the code more stable (pointer might not be null = no NPE)
  • allows to turn @Nullable instance variables to final, because they get initialized in the constructor and not asynchronously, which also means
  • reduced number of @Nullable and @SuspressWarnig(“null”) annotations
    = improve stability, reduced code, code better readable, less review efforts

This think the topic constructor injection should be explained and recommended in the coding guidelines. It should also become a formal criteria for acceptance of OH3 bindings.

PacMan: great job :slight_smile:

1 Like

Nice! Yes the constructor injection is preferred over setters for all those reasons. The only downside is readability (or potential cycles) when a lot of dependencies are injected. But when that’s the case the class usually has too many responsibilities.

for me looks good and readability is way better than before, because I could remove various @Nullable.