I am using the matlabcontrol-4.0.0.jar library to call Matlab from Java. This on Ubuntu 11.10, Matlab r2011b and Java version "1.6.0_23".
When trying to run this simple program:
public static void main(String[] args) throws MatlabConnectionException,
MatlabInvocationException {
//Create a proxy, which we will use to control MATLAB
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
//Display 'hello world' just like when using the demo
proxy.eval("disp('hello world')");
//Disconnect the proxy from MATLAB
proxy.disconnect();
}
I get, after the Matlab launch screen appears (which is good), a time out:
Exception in thread "main" matlabcontrol.MatlabConnectionException:
MATLAB proxy could not be created in 180000 milliseconds at matlabcontrol.RemoteMatlabProxyFactory.getProxy(RemoteMatlabProxyFactory.java:158)
at
matlabcontrol.MatlabProxyFactory.getProxy(MatlabProxyFactory.java:81)
at Main.main(Main.java:15)
I've looked everywhere including all the tips from provided by stackoverflow, but nothing seems to fit the problem i am encountering
*UPDATE*
I forbore to mention that I already tried the scenario described by Joshua Kaplan (thanks!) .This seems be for my case of no help, meaning that it just keeps waiting. Could someone perhaps elaborate on the communication protocol between java and the matlab proxy?
-> It could be an incompatibility issue as well, I've posted on the website delivering the resource, have received no answer so far...
*END UPDATE*
So, any of you a tip where to start looking, that would be wonderful
thanks
The getProxy() method is a blocking operation with a default timeout of 3 minutes (or 180 seconds or 180000 milliseconds). For most people's machines that is long enough, if the connection was not established in that amount of time then something has gone wrong. However, this timeout can be changed by creating an instance of a MatlabProxyFactoryOptions which is done by using a MatlabProxyFactoryOptions.Builder. The MatlabProxyFactoryOptions instance you create is passed into MatlabProxyFactory's constructor. Here's an example with a 5 minute timeout:
MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setProxyTimeout(300000L)
.build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
Alternatively you can request a proxy which is a non-blocking operation that has no timeout. Once the proxy has been created it will be passed to the provided callback. Example:
MatlabProxyFactory factory = new MatlabProxyFactory();
factory.requestProxy(new MatlabProxyFactory.RequestCallback()
{
public void proxyCreated(MatlabProxy proxy)
{
//TODO: Make use of the proxy
}
});
I got similar problem. Main issue is that in your imported .jar file "matlabcontrol-4.0.0.jar" there is default, configuration in class Configuration.java. In my case there was problem, that libraries cannot properly call matlab with all arguments. Try to add to your project not .jar file, but package matalbcontrol with all source .java files. You can download it form the same page http://code.google.com/p/matlabcontrol/downloads/list, form where you got .jar libs. Then in Configuration.java edit getMatlabLocation() lines:
else if(isWindows() || isLinux())
{
matlabLoc = "matlab";
}
replace with:
else if(isLinux())
{
matlabLoc = "/usr/local/MATLAB/R2011b/bin/matlab"; //or place where you got installed your matlab, directory bin, in my case, like in example
}
else if(isWindows())
{
matlabLoc = "matlab";
}
Related
I'm writing a POC using vertx, looking for alternatives when we have to migrate Spring Web from 4.x to 5 to be java 9 compliant.
I've written a simple client, just a GET towards a publicly available server just to get something working but it silently fails me.
public List<String> pull() {
Vertx vertx = Vertx.vertx();
HttpClientOptions options = new HttpClientOptions().setLogActivity(true);
HttpClient hc = vertx.createHttpClient(options);
hc.getNow(80, "http://sunet.se", "/",r -> {
System.out.println("\n****** Handler called! ***\n");
});
return new ArrayList<>();
}
This will silently fail and I cannot understand why.
As far as I can tell, I do exactly as in the examples given in the docs.
In desperation I fired up wire shark and according to WS, there is no actual call (when I use the browser WS captures that). So, it seems my call is never actually done. I don't get any exceptions or anything. Setting the log level to debug gives nothing noteworthy other than
Failed to get SOMAXCONN from sysctl and file /proc/sys/net/core/somaxconn. Default: 128
And that should not fail the call.
I've also tried using vertx.io WebClient but that fails also, in the same manner.
UPDATE
I've managed to get it to work but with a caveat.
As #tsegismont states in his answer, the protocol part of the URI shouldn't be there, that was not in the examples, I just missed it myself.
I ran my example as a stand-alone and then it worked.
My original example was run as a junit test (it's an easy way to test code and I usually try to write the test code first) and when it's run as a junit test it still doesn't work. Why that is, I have no idea. I would greatly appreciate if someone could tell me how to get that to work.
The getNow variant you use expects the server host, not a URL. It should be:
hc.getNow(80, "sunet.se", "/",r -> {
System.out.println("\n****** Handler called! ***\n");
}
If you found a snippet like this in the Vert.x docs it's a bug. Would you mind to report it?
Now a few comments.
1/ The HttpClient is a low-level client.
Most users should prefer the Vert.x Web Client
Here's an example for your use case:
WebClient client = WebClient.create(vertx);
client
.get(80, "sunet.se", "/")
.send(ar -> {
if (ar.succeeded()) {
// Obtain response
HttpResponse<Buffer> response = ar.result();
System.out.println("Received response with status code" + response.statusCode());
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
}
});
2/ Create a single Vert.x and WebClient instance
Do not create a Vert.x and WebClient instance on every method call.
It wastes resources and is inefficient.
I am working on android(Java) using TooTallNate's java websockets from this tutorial to consume websockets on android to connect with ws:// but I am getting error draft org.java_websocket.drafts.Draft_10#4560b1d0 refuses handshake. I tried their other draft versions but none of them worked either.
First of all, you want to use the Draft_6455, it is the current spec, the rest may or may not work on different servers reliably. There are constructors for the draft object which take a List<IProtocol>. If no protocol specified matches one offered by the server, the handshake will be refused.
public Draft_6455( List<IExtension> inputExtensions , List<IProtocol> inputProtocols )
public Draft_6455( List<IExtension> inputExtensions , List<IProtocol> inputProtocols, int inputMaxFrameSize )
I ran into a similar issue to yours with the newest version of TooTallNate's Java Websockets, my code was like so:
knownExtensions = new java.util.ArrayList();
knownProtocols = new java.util.ArrayList();
if(this._protocol){
knownProtocols.add(new org.java_websocket.protocols.Protocol(this._protocol));
}
this._socket = new _WebSocket(uri, new org.java_websocket.drafts.Draft_6455(knownExtensions, knownProtocols), toHashMap(this._headers), this._timeout);
You MUST have at least one valid protocol (even if it is a empty string), or you get the above error you referenced. So I changed my code to be:
...
if(this._protocol){
knownProtocols.add(new org.java_websocket.protocols.Protocol(this._protocol));
}
/* -=-=-=- NEW ADDED CODE -=-=-=- */
else {
knownProtocols.add(new org.java_websocket.protocols.Protocol(""));
}
/* -=-=-=- END NEW ADDED CODE -=-=-=- */
...
This is what broke, no protocol specified caused the "refuses handshake" error message for me.
Please note there are a couple of reasons for the above "refuses handshake", but in my case it was the missing empty protocol...
Did you try this on broswer? You will get a err code on the broswer.
You can write a simple js file to start and test whether this problem is on the server or is on the app.
Here is a demo,it won't take you too much time.
<script type="text/javascript">
function send() {
var url = 'ws://192.168.1.101:8080/WebSocket/echo';
var vs = new WebSocket(url);
vs.onopen = function(evt){
vs.send(te.value)
};
vs.onmessage = function(evt){
alert(evt.data);
};
}
Basically if you have for example a protocol "my-protocol"
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
protocols.add(new Protocol("my-protocol"));
//Uncomment below if you want to have a fallback
//protocols.add(new Protocol(""));
Draft_6455 my_draft = new Draft_6455(Collections.<IExtension>emptyList(), protocols);
Taken from here
I've made a web service using Java 7 and Matlabcontrol-4.1.0. In this web service, i'm starting a Matlab r2015a session to execute a function. As far as I can see, isExistingSession and setUsePreviouslyControlledSession are functions to use a previously created session.
Q: In order to get the best performance, which method should I use?
isExistingSession (MatlabProxy) and/or setUsePreviouslyControlledSession (MatlabProxyFactoryOptions)?
I am using the following code at the moment:
// setting up connection to MatLab
MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true).setHidden(true)
.setMatlabLocation(null).build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
MatlabProxy proxy = factory.getProxy();
I have checked setUsePreviouslyControlledSession and isExistingSession, but I don't quite understand.
After digging in the documentation, I think i've looked at it the wrong way.
setUsePreviouslyControlledSession (MatlabProxyFactoryOptions): sets whether to use a previously started session.
isExistingSession (MatlabProxy): Just returns a boolean answering "is there already a session running?".
These methods have different functions, so the comparison was never valid.
This is my very first time to use jconsole.
The final goal of using this is measuring using memory redis uses. However, since this is my first time I want to just begin with small java project which just prints a phrase.
Here is my source code.
public class Main {
public static void main(String[] args) throws Exception {
int cnt = 0;
while(true){
if(cnt == 100)
break;
System.out.println("Save me from the nap!");
Thread.sleep(1000*30);
cnt++;
}
}
};
After I wrote this code, I tried to run jconsole. So I ran this program in eclipse, at the same time, I execute 'jconsole' command in terminal. like below.
>jconsole
A UI interface poped up and I can see process ID of my program:
So I choose 'local' and my process ID and press connect button.
But it shows an error message like this.
ConnectionFailedSSL1
ConnectionFailedSSL2
<Cancel> <Insecure>
Do I had to extra job to execute jconsole? Give me some specific instruction to use this. I really do not have any concept of 'SSL'.(All I know about this is SSL is abbreviation of Secure Socket Layer)
My OS is OSX Mavericks.
Apparently jconsole cannot authenticate the certificate it received on making a direct SSL connection to the JVM. Since it's on the local machine, it should be safe to select "insecure" and connect without certificate authentication.
You should also make sure that the JVM and jconsole are the same version - particularly 64 vs 32 bits.
we've seen the above behavior complaining with "ConnectionFailedSSL1" even for a local connection with auth/ssl turned off (all defaults) with 1.7.0_45-b18 and it goes away with 1.7.0_75-b13, back to how it used to be with java 1.6.
Has anybody had a luck trying to use vertx bihind a corporate proxy? I have tried whatever possible ways that comes to my head to provide the proxy information to vertx. Nothing works so far.
set environment variable http_proxy=http://mycorporate.proxy.com:8080 - no luck
set environment variable VERTX_OPTS='-Dhttp.proxyHost=mycorporate.proxy.com -Dhttp.proxyPort=8080' - no luck
set environment variables
http.proxyHost=mycorporate.proxy.com
http.proxyPort=8080
no luck
Injecting extra echo into vertx command I can see that proxy related parameters are being passed to JVM correctly but required module still can't be downloaded ("vertx run hello.js" just stuck obviously trying to download io.vertx~lang-rhino~2.0.0-final)
Proxy itself is ok - I'm using it with no problem for maven, sbt and other different stuff requiring proxy
Same laptop being used from home can successfully run "vertx run hello.js" with downloading io.vertx~lang-rhino~2.0.0-final (for the first run)
I have just started evaluating vertx for our company needs and this is my very first choking point hindering my further attempts to make a decision. So far I have to follow next steps as workaround: 1 Run from home and get whatever required modules in sys-mods. 2 Manually upload the module(s) to sys-mods on the test server when back to the office.
Obviously this is not a normal way to run anything.
I had similar problem. I figure out that HttpClient form does not read settings from JVM_OPTS.
So solution was following:
Edit your vertx.bat(sh)
set JVM_OPTS=-Dhttp.proxyHost=xxxx -Dhttp.proxyPort=xxxx
and then in code related to httpClient try sth like this
HttpClient client = vertx.createHttpClient();
String proxyHost = System.getProperty("http.proxyHost", "none");
Integer proxyPort = Integer.valueOf(System.getProperty("http.proxyPort", "80"));
if(!"none".equalsIgnoreCase(proxyHost)){
client.setHost(proxyHost);
client.setPort(proxyPort);
}
and later in code releted to HTTP request
MultiMap map = new CaseInsensitiveMultiMap();
map.add("Host", domainName); //get domain of REQUESTED_URL
client.getNow(REQUESTED_URL, map, new new Handler<HttpClientResponse>(){...});
Building on #kamyk-pl answer. If you are using latest version of Vertx this is what you can do :
WebClientOptions webClientOptions = new WebClientOptions();
String proxyHost = System.getProperty("http.proxyHost", "none");
Integer proxyPort = Integer.valueOf(System.getProperty("http.proxyPort", "80"));
if(!"none".equalsIgnoreCase(proxyHost)){
ProxyOptions proxyOptions = new ProxyOptions();
proxyOptions.setHost(proxyHost);
proxyOptions.setPort(proxyPort);
webClientOptions.setProxyOptions(proxyOptions);
}
WebClient.create(vertx, webClientOptions)
Optionally you also want to add this if you are testing SSL connections
webClientOptions.setVerifyHost(false);
webClientOptions.setTrustAll(true);
I use vertx 3.8.0
HttpClient c = vertx.createHttpClient();
RequestOptions requestOptions = new RequestOptions();
requestOptions.setHost(ip).setPort(port).setURI("http://httpbin.org/ip");
c.request(HttpMethod.GET, requestOptions).handler(new Handler<HttpClientResponse>() {
#Override
public void handle(HttpClientResponse event) {
event.handler(new Handler<Buffer>() {
#Override
public void handle(Buffer event) {
System.out.println(event);
}
});
}
}).end();
To set the proxy, edit vertx.bat (if you are on Windows) and add
set JVM_OPTS=-Dhttp.proxyHost=xxxx -Dhttp.proxyPort=xxxx