I wrote some logic which represent near 200 websocet connection with exchange at the same time.I use third party api and it based on org.eclipse.jetty.websocket.api. I have this method which i had to override.
try {
URI uri = new URI(websocketBaseUrl + url);
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setTrustAll(true);
WebSocketClient client = new WebSocketClient(sslContextFactory);
client.setMaxIdleTimeout(0);
client.start();
return client.connect(adapter, uri).get();
} catch (URISyntaxException e) {
throw new BinanceApiException("URL Syntax error: " + e.getMessage());
} catch (Throwable e) {
throw new BinanceApiException("Websocket error: " + e.getMessage());
}
I added setIdleTimeout to it so that the connection is not lost when i don't recieve information for a long time.
Exchange close connection one time a day but for 2,3 sometimes 4 days it have been reconnected.But finally i get this:
java.nio.channels.ClosedChannelException: null at
org.eclipse.jetty.io.WriteFlusher.onClose(WriteFlusher.java:507) at
org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onIncompleteFlush(SslConnection.java:527)
at
org.eclipse.jetty.io.AbstractEndPoint$2.onIncompleteFlush(AbstractEndPoint.java:54)
at org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:331) at
org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:372)
at
org.eclipse.jetty.websocket.common.io.FrameFlusher$Flusher.flush(FrameFlusher.java:153)
at
org.eclipse.jetty.websocket.common.io.FrameFlusher$Flusher.process(FrameFlusher.java:217)
at
org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241)
at
org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:224)
at
org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:382)
at
org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:614)
at
org.eclipse.jetty.websocket.client.io.WebSocketClientConnection.outgoingFrame(WebSocketClientConnection.java:72)
at
org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onConnectionStateChange(AbstractWebSocketConnection.java:473)
at
org.eclipse.jetty.websocket.common.io.IOState.notifyStateListeners(IOState.java:184)
at
org.eclipse.jetty.websocket.common.io.IOState.onReadFailure(IOState.java:498)
at
org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.readParse(AbstractWebSocketConnection.java:666)
at
org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:511)
at
org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104)
at
org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:289)
at
org.eclipse.jetty.io.ssl.SslConnection$3.succeeded(SslConnection.java:149)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104)
at
org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at
org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
at
org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
at
org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
at
org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:243)
at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:679)
at
org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:597)
at java.lang.Thread.run(Thread.java:748)
I find this question on stackoverflow but i can't see clear answer. Pls help.Thanks in advance.
If you want the connection to stay open if idle, you should configure your client this way:
client.setMaxIdleTimeout(Long.MAX_VALUE);
Setting maxIdleTimeout to 0 would have the opposite effect: Closing the connection as soon as it becomes idle.
Related
I have been unable to catch time out exception that happens in my vertx HttpClientRequest. I have enclosed my connection and request creation code in try-catch block. Also I have added exceptionHandler and endHandler. But none of them gets fired when the time out happens. All I receive is the below error message which gets printed on the console. Please give me idea how to catch this exception, so that I can call the caller back with relevant info.
io.vertx.core.http.impl.HttpClientRequestImpl
SEVERE: io.netty.channel.ConnectTimeoutException: connection timed out:
The code below is what I use to make request to server. As you can see I have used try-catch and added exceptionHandler as well.
try{
HttpClient httpClient = Vert.x.createHttpClient(new HttpClientOptions().setSsl(true).setTrustAll(true).setVerifyHost(false));
HttpClientRequest request = httpClient.get(port, host, uri.getRawPath(), event-> {
event.exceptionHandler(e -> {
log.error(" Error:: " + e);
});
event.handler(handler -> {
//code
});
});
request.putHeader(HttpHeaders.Names.AUTHORIZATION, "Basic "+authEnc);
request.end();
} catch(Exception e){
log.error(" Exception :: " + e);
}
Due to the async programing model you won't be able to use try-catch since your method has long been terminated before you get the timeout event. In order to catch it you need to setup an exception handler like:
request.exceptionHandler(t -> {
// where t is a throwable
// do something with it...
}
If you're interested in catching response exceptions same concept applies.
In my application, I am downloading JSON data from a ReST web service. Most of the time, this works fine, however sometimes the connection will time out.
This is the code I use to set the timeout...
HttpConnectionParams.setConnectionTimeout( httpParameters, 20000 );
HttpConnectionParams.setSoTimeout( httpParameters, 42000 );
If the connection times out, the application crashes and closes, how do I handle a time out?
The HttpClient class throws a ConnectTimeoutException Exception, so you should listen for it:
try {
HttpResponse response = client.execute(post);
// do something with response
} catch (ConnectTimeoutException e) {
Log.e(TAG, "Timeout", e);
} catch (SocketTimeoutException e) {
Log.e(TAG, " Socket timeout", e);
}
Increase your time of waiting for response like :
HttpConnectionParams.setConnectionTimeout( httpParameters, 60000 ); //1 minute
HttpConnectionParams.setSoTimeout( httpParameters, 90000 ); // 1.5 minute
I have tried to catch a variety of exception types, I have found that catching an IOException worked as I wanted!
Im trying to set a timeout on an FTP Connect using com.enterprisedt.net.ftp.FTPClient but for some reason can't get it to work.
This is my code:
public boolean testFtpDetails(String path) {
boolean success = false;
try {
FTPClient ftp = new FTPClient();
ftp.setRemoteHost("210.160.1.100");
ftp.setTimeout(3000);
ftp.connect();
ftp.login("test", "test");
success = ftp.connected();
if (success && path != null) {
ftp.chdir(path);
}
ftp.quit();
} catch (UnknownHostException e) {
} catch (IOException e) {
} catch (FTPException e) {
}
So after 3 seconds of trying to execute ftp.connect() I would expect it to timeout. However this does not happen and it takes around 15 seconds to timeout. Is there a way to change this?
Thanks,
the setTimeout method is documented as:
Set the SO_TIMEOUT in milliseconds on the underlying socket.
In this way it only affects the low level socket operations. The questions would be - what kind of timeout are you expecting to happen?
On connect ? During transfer? On idle?
If you connect to e.g. an unknown host you will also have DNS lookup taking most of the time before the timeout is even considered.
Hello all I have a basic Storm application set up where it receives a stream of tweets and stores them in a MySQL database. The application works great for the first ~23 hours or so then it starts giving the following error:
SQL Exception
SQL State: 08003
After it does this a few times it dies. I'm using the standard JBDC connector to connect to the database from Java. The code for the functions for storing and setting up the DB connection are as follows:
private String _db="";
private Connection conn = null;
private PreparedStatement pst = null;
public ArchiveBolt(String db){
_db = db;
}
private void setupConnection() {
//Connect to the database
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:8889/twitter_recording", "root", "root");
} catch (Exception e){
e.printStackTrace();
}
}
public void execute(Tuple tuple, BasicOutputCollector collector) {
Status s = (Status) tuple.getValue(0);
//setup the connection on the first run through or if the connection got closed down
try {
setupConnection();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.toString());
}
try {
pst = conn.prepareStatement("INSERT INTO " + _db + " (tweet)" +
"VALUES (?);");
pst.setString(1, s.toString());
//execute the SQL
pst.executeUpdate();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
if(ex.getSQLState().equals("08003")){
setupConnection();
}
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
After it became apparent that it was crashing because of a 08003 error I decided that if it threw that error it should retry the set up of the connection, however that didn't help either. Could anyone point me in the right direction for solving this issue?
After it became apparent that it was crashing because of a 08003 error I decided that if it threw that error it should retry the set up of the connection, however that didn't help either. Could anyone point me in the right direction for solving this issue?
There are basically two problems here that need to be solved:
Why are the connections getting lost in the first place?
Why isn't your attempt to reconnect succeeding?
For the first problem, you should take a look at the MySQL logs to see if there are any indications there. And also, check for SQL exceptions immediately prior to the (repeated) "state 080003" exceptions. The latter are simply telling you that the connection has died previously.
My guess is that the problem is one of the following:
The MySQL server has timed out the connection due to inactivity. You can change the connection timeout in the MySQL configs if this is the problem.
Your application may be slowly leaking JDBC connections.
For the second problem, the general approach is correct, but your code doesn't match the description. In fact, it looks like it is always trying to establish a new database connection each time your execute method is called. This renders the reconnection call in your exception handler pointless. (OTOH, the code show signs that someone has been "beating on it" to try to get it to work ... and that could well be part of the problem.)
I would check that setupConnection is being called when it needs to be, and look for any exception that might be thrown. In addition, you should make sure that you explicitly close() the dead connection object ... and rethink / recode your connection management so that it doesn't leak.
For the record, there is a connection URL parameter called "autoReconnect" that in the distant past used to "deal" with lost connections. Unfortunately, the original implementation was unsafe, so they effectively disabled it; see this Question for details: Why does autoReconnect=true not seem to work?
I've been playing around with my Java client app. Behaviour is very similar to this post (and others):
java.net.SocketException: Software caused connection abort: recv failed
As yet I've not found the answer - hence this post. I have a idea what is wrong but I'm not sure what to do about it.
The protocol is simple - read some data in, send back an ACK and repeat (until terminted).
I've been looking at what is going on with WireShark, and is seems the TCP window is filling up. I'm performing a flush() on the DataOutputStream (For the ACKs) but doesn't change the fact at after a while I get this exception (I can see on WireShark that there is always a window problem right before the Java exception).
So how to I make sure in Java that my TCP windows/buffers are clear (Which I think is the root cause of the problem?) seems to be no flush on the DataInputStream. Make be wonder that whilst I might be reading it the TCP stack is filling up.
Many Thanks
Mark
I've attached the basic code calls below:
public void connectToServer()
{
//Create socket connection
try
{
if (lSocket == null)
{
lSocket = new Socket("localhost", 7651);
lSocket.setSoTimeout(0);
lInDataStream = new DataInputStream(lSocket.getInputStream());
lOutDataStream = new DataOutputStream(lSocket.getOutputStream());
}
}
catch (UnknownHostException e)
{
System.out.println("Unknown host: localhost");
}
catch (IOException e)
{
System.out.println("No I/O");
}
}
public void readSocket() throws IOException
{
//Receive data from ROS SerialtoNetwork server
if (lSocket != null)
{
lInDataStream.readInt();
lInDataStream.readFully(cbuf);
lOutDataStream.writeBytes("Ack");
lOutDataStream.flush();
//String lstr = new String(cbuf);
//System.out.print(lstr);
//System.out.println("");
}
}
public String getDataBuffer()
{
String lstr = new String(cbuf);
return lstr;
}
This indicates persistent network errors. There are several (repetitive) MSDN article on this error, e.g. this one.