Make JRuby inherit Java proxy settings

若如初见. 提交于 2019-12-10 14:46:12

问题


I would like to make HTTP requests from Rails code running on top of JRuby.

How can I make it to re-use http.proxyHost, http.proxyPort and http.nonProxyHosts settings, given to JVM running it ?


回答1:


To pass JVM flags through JRuby, use -J.... In this case:

jruby -J-Dhttp.proxyHost=foo -J-Dhttp.proxyPort=1234 -J-Dhttp.nonProxyHosts="*.bar.com" ...

This is explained in JRuby's help text.

-J[java option] pass an option on to the JVM (e.g. -J-Xmx512m)
                use --properties to list JRuby properties
                run 'java -help' for a list of other Java options



回答2:


I have had the same issue. I found that java or net::http doesn't obey the nonProxyHosts option. The best way to get around this is to modify the ENV_JAVA settings to account for this.

The steps I took to ensure nonProxyHosts was used were the following:

1) JAVA_OPTS="-Dhttp.proxyHost=192*** -Dhttp.proxyPort=1234 -Dhttp.nonProxyHosts=local|127.0.0.1"
OR
1) JRUBY_OPTS="-J-Dhttp.proxyHost=192*** -J-Dhttp.proxyPort=1234 -J-Dhttp.nonProxyHosts=local|127.0.0.1"

Keep in mind that at least for java1.7 the nonProxyHosts should not have quotations see here.

Now I find that either net::http or java itself doesn't actually honour the nonProxyHosts option.

However you can get around this by doing the following in JRuby

a = URI("http://someurl")
Net::HTTP.new(a).proxy?.should == true
regex = /$#{ENV_JAVA["http.nonProxyHosts"]}/ #dollar needed to behave as expected
if a.hostname.match(regex)
   ENV_JAVA["http.proxyHost"]=nil
end
Net::HTTP.new(a).proxy?.should == false

Hope that helps.



来源:https://stackoverflow.com/questions/4844505/make-jruby-inherit-java-proxy-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!