i have created mqtt connection suscribeData() as follows to receive data 24/7 from multiple devices
#RequestMapping("/suscribe")
#ResponseBody
public String suscribeData(){
connOpt= new MqttConnectOptions();
connOptPublish= new MqttConnectOptions();
boolean result=false;
int retry=0;
while(!result){
try{
result=initializeMqTTParameters(retry);
retry++;
if(!result){
Thread.sleep(1000);
}
}catch(Exception ex){
}
if(retry>10){
return "mqtterror";
}
}
suscribeReceivedData("DATA/#",2);
Calendar cal=TimeZoneJu.getCalenderInstance();
ModemOnlineStatus status=new ModemOnlineStatus();
status.setActiondate(cal.getTime());
status.setActiontime(cal.getTime());
status.setModemid("mqttreceive");
status.setRemark("online");
status.setStatus(true);
try{
service.checkAndInsertModemStatus(true, status);
}catch(Exception ex1){
}
return "ok";
}
checkAndInsertModemStatus() function is used to save the online offline state of connection i check the connection from another url with cronjobs.
and following is my initializeMqTTParameters
public boolean initializeMqTTParameters(int retry){
String clientID = M2MIO_THING;
connOpt = new MqttConnectOptions();
connOpt.setCleanSession(false);
connOpt.setUserName(M2MIO_USERNAME);
connOpt.setPassword(M2MIO_PASSWORD_MD5.toCharArray());
connOpt.setWill("WILL/mqttreceive", "mqttreceive:disconnected".getBytes(), 2, true);
try {
if(retry%2==0){
MQTTReceive.myClient = new MqttClient(BROKER_URL, "mqtt_virtual_received_sus_1");
}else{
MQTTReceive.myClient = new MqttClient(BROKER_URL, "mqtt_virtual_received_sus_2");
}
MQTTReceive.myClient.setCallback(this);
MQTTReceive.myClient.connect(connOpt);
} catch (MqttException e) {
System.out.println(e.getMessage());
return false;
}
return true;
}
on connectionLost i am trying update status in database and try reconnect
#Override
public void connectionLost(Throwable ex) {
System.out.println("Mqtt Received Connection Lost"+ex.getMessage());
Calendar cal=TimeZoneJu.getCalenderInstance();
ModemOnlineStatus status=new ModemOnlineStatus();
status.setActiondate(cal.getTime());
status.setActiontime(cal.getTime());
status.setModemid("mqttreceive");
status.setRemark("offline");
status.setStatus(false);
try{
service.updateModemStatus(false, status);
}catch(Exception ex1){
}
suscribeData();
}
and from the above code i got my connections were online but i stopped receving data on subscribed topics so another method i tried that i am checking connection status from another function from database like i set the connection online when i connect and offline on connectionLost and i can call to connect(suscribeData()) but it leaves me with too many open files in tomcat within few days how can i make sure previous open files should close
like System.exit(1) on connectionLost() so that current process will close and i will reconnect after checking the status
or please suggest me if there is any other logic i can use to maintain my connections
Your catch block does nothing in method connectionLost.. it should do something like this:
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
You should also disconnect your MQTT client whenever not required..i.e processing over:
myClient.disconnect();
Related
In my current code i had a servlet from which if i create post to the servlet it will open a new websocket client , that mean 10 client connection each running for same purpose but with different api and secret , so i need to close particular session
I am using Jetty :: Websocket :: Client v9.4.48.v20220622
Please suggest , as i can get the session details but unable to use because it's not working with String data type . only in Session session it is working and i am unable to store session details anywhere else , as only in String data type i can save .
Whereas a is my API and b is my Secret Key ;
PS : Websocket connection is working fine to send expected data
class connector {
String a;
String b;
public void start() {
WebSocketClient client = new WebSocketClient();
MyWebSocket socket = new MyWebSocket();
try {
client.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URI destUri = null;
try {
destUri = new URI("wss://socket.delta.exchange");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ClientUpgradeRequest request = new ClientUpgradeRequest();
System.out.println("Connecting to: " + destUri);
try {
client.connect(socket, destUri, request);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
socket.awaitClose(3600, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#WebSocket
public class MyWebSocket {
private final CountDownLatch closeLatch = new CountDownLatch(1);
#OnWebSocketConnect
public void onConnect(Session session) throws IOException {
session.getRemoteAddress();
System.out.println("Connection opened");
PingPong newObj = new PingPong();
newObj.session = session;
Authorization authMe = new Authorization();
Identifier getSt = new Identifier();
newObj.enableHeartBeat();
System.out.println(session);
session.getRemote().sendString(authMe.data(a, b));
}
#OnWebSocketMessage
public void onMessage(String message) {
MessageHandler objmsg = new MessageHandler();
objmsg.check();
System.out.println(
"Current Thread ID: "
+ Thread.currentThread().getId());
System.out.println("Message from Server: -- " + message);
}
#OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("WebSocket Closed. Code:" + statusCode);
}
public boolean awaitClose(int duration, TimeUnit unit)
throws InterruptedException {
return this.closeLatch.await(duration, unit);
}
}
}
I want to do session.close() for a particular session details which i got from
session.getRemoteAddress().toString();
Session session ;
String sessionDetailSaved ;
i want to search for sessionDetailSaved and compare with all the on running sessions and close it
Or else any other way i can close particular session with different method may be interrupting session thread but sure it will not completely close connection .
Maven Dependency i am using
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.4.48.v20220622</version>
</dependency>
Calling Session.close() will initiate a close handshake where the remote endpoint should reply with a response close frame, and once the close response has been received the WebSocket connection will be closed. You can send custom close status code and reason with Session.close(int statusCode, String reason).
You also have the option to call Session.disconnect() which will do a hard close of the underlying connection without sending this close frame.
In regards to your code, it looks like you are never completing the closeLatch in the OnWebSocketClose method, so your awaitClose method will always timeout.
Also, if possible you should try to re-use the same WebSocketClient instance for multiple connections because it is a heavy weight object. It is expensive to create a new one for each request.
i'm creating android application with Socket.io and node.js server.
My js server:
var http = require('http');
var server = http.createServer(function(req, res) {
});
var mysql = require('mysql');
var db = mysql.createConnection({
host:,
port:,
user:,
password:,
database:
});
var resultconnection = "";
db.connect(function(err){
if (err) {
resultconnection = err;
}
});
var io = require('socket.io').listen(server);
io.on('connection',function(socket){
console.log('Connected!');
socket.on("createaccount", function(data){
console.log('Create account executed.');
if(resultconnection != ""){
db.query("INSERT INTO USER (USERNAME, PASSWORD, EMAIL, BIRTHDATE) VALUES('"+data["username"]+"', SHA1('"+data["password"]+"'), '"+data["email"]+"', "+data["birthdate"]+"')");
resultconnection = "OK";
}
socket.emit('response', {"response": resultconnection});
});
});
server.listen(8080);
Android client(java):
try {
socket = IO.socket("http://localhost:8080");
socket.on("response", new Emitter.Listener() {
#Override
public void call(Object... args) {
JSONObject obj = (JSONObject) args[0];
try {
result = obj.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
socket.connect();
JSONObject createUser = new JSONObject();
try {
createUser.put("username", username);
createUser.put("password", password);
createUser.put("email", email);
createUser.put("birthdate", datanasc);
} catch (JSONException e) {
e.printStackTrace();
}
socket.emit("createaccount", "");
} catch (URISyntaxException e) {
result = e.getMessage();
}
Console.log("Connected") is running but when I run the socket.emit("createaccount") the client does not run on the server.
I think the server IP you gave in the client code is not correct. It is trying to connect to the localhost of your Android device while your server is running on some other remote system.
Try changing the server IP in the following snippet(ideally this should be something other than localhost):
try {
socket = IO.socket("<server_IP:server_PORT");
socket.on("response", new Emitter.Listener() {
#Override
public void call(Object... args) {
JSONObject obj = (JSONObject) args[0];
try {
result = obj.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
socket.connect();
JSONObject createUser = new JSONObject();
try {
createUser.put("username", username);
createUser.put("password", password);
createUser.put("email", email);
createUser.put("birthdate", datanasc);
} catch (JSONException e) {
e.printStackTrace();
}
socket.emit("createaccount", "");
} catch (URISyntaxException e) {
result = e.getMessage();
}
There are significant structural problems with how your server is designed:
You're trying to save resultconnection in a module global. That means you can only ever have one socket.io client connection that will work properly. The second connection will see the resultconnection from the previous client connection. There's just no way this is a proper way to do things. It could possibly work if you never ever had more than one client connect to your server - not just one client at a time, but one client all time.
The way you handle the async database operation in the createaccount message handler is just wrong. You are trying to .emit() the resultconnection back to the client before it has been set.
Your error handling for the db.connect() call does not seem correct. When you can't connect to the database you need to take some action, not just set a variable as such and proceed on as if nothing is wrong. Even that variable you set is being used wrong later. You're checking to see if you had an error on the connect and if you did, then you're trying to do a database operation. Besides just a messed up design, this logic is even backwards.
To think about how to fix the first issue, you need to explain what you're trying to do with this statement:
if(resultconnection != ""){
because that will need to be done a different way. You can't just save global state in your server and expect that to represent the state of any incoming connection.
How to fix the second issue will depend upon what is done for the first issue.
How to fix the third issue involves some design decisions about what to do with your server when you can't connect to the database (shut-down, retry, log, etc...).
I'm developing a SIP client app using Android 4.3.1+ API. I can make a outgoing call and receive a incoming call successfully by using SipDemo sample codes.
Let me show you my makecallWithSipStack method. On the phone side i can see on the screen Call Established with bla bla. At the pc side i havent any problem as well. And i cant see any problem in the logs. But i cant hear voice each side. By the way phone is caller.
Thanks for all advices.
public void makecallWithSipStack(){
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onCallEstablished(SipAudioCall call) {
try {
call.startAudio();
call.setSpeakerMode(true);
updatestatus("Call Established with "+callusername+"#"+calluserdomain);
}catch (Exception e){
Log.e("Make Call","Error");
e.printStackTrace();
}
}
#Override
public void onCallEnded(SipAudioCall call) {
try {
call.endCall();
call.close();
updatestatus("Call Ended with "+callusername+"#"+calluserdomain);
}catch (Exception e){
Log.e("Make Call","Error");
e.printStackTrace();
}
}
};
try {
if (mSipManager != null && mSipProfile !=null){
call = mSipManager.makeAudioCall(mSipProfile.getUriString(), callusername+"#"+calluserdomain , listener, 30);
updatestatus("Calling "+callusername+"#"+calluserdomain);}
}catch (Exception e){
Log.e("Make Call","Error");
e.printStackTrace();
}
}
I'm trying to make a call that might throw an exception due to server being down.
this is what i want to accomplish:
Server server = serverQueue.poll();
try {
if (server==null){return null}
server.makeConnection();
} catch (Exception e) {
// try another server
server = serverQueue.poll();
// now return to try block?
}
So i have 5 servers and maybe in later stage i'll add some more. So i want to connect to
anyone of them in this manner. How can i return to the try block? is there anything such as a statement like this below in java?:
Server server = serverQueue.poll();
outerBlock:
try {
if (server==null){return null}
server.makeConnection();
} catch (Exception e) {
// try another server
server = serverQueue.poll();
continue outerBlock;
}
Basically a loop:
Server server = serverQueue.poll();
boolean connected = false;
while (!connected) {
if (server == null) {
return null;
}
try {
server.makeConnection();
connected = true;
} catch (Exception e) {
// Presumably log something here, for management info
// try another server
server = serverQueue.poll();
}
}
Or to avoid the duplication:
Server server;
boolean connected = false;
while (!connected) {
server = serverQueue.poll();
if (server == null) {
return null;
}
try {
server.makeConnection();
connected = true;
} catch (Exception e) {
// Presumably log something here, for management info
}
}
You might consider isolating the "find a server" part of that into its own function.
You need to put your try block inside a simple loop, either while or for.
ou can use a for loop
Server server;
for(int i=0;i<=numberOFServers-1;i++){
server = serverQueue.poll();
try {
if (server==null){return null}
server.makeConnection();
} catch (Exception e) {
System.out.println("Server : " +i+ " could not establish connection");
}
}
I am trying to make a monitoring application for a FTP server using FTP4J(referred to as client in the code example).
It connects to a FTP, logs in, creates a file locally, uploads file, downloads file, validates the files are the same, cleans up files on ftp and locally and disconnects.
All this is already made but my question is how do I best log what has happened and break when an error is detected?
The simple solution I could think of was to make a Boolean that shows if previous steps where successful and only do next step if they where.
StringBuilder sb = new StringBuilder();
boolean noError = true;
// Connect to FTP
try {
client.connect(hostname, port);
} catch (Exception e) {
noError = false;
sb.append("failed to connect<br>");
}
//Logging in to FTP
if(noError) {
try {
client.login(username, password);
} catch (Exception e) {
noError = false;
sb.append("failed to login<br>");
}
}
...
// Close connection
if(client.isConnected()) {
try {
client.disconnect(true);
} catch (Exception e) {
sb.append("failed to disconnect<br>");
}
}
another solution I could think of was nested try/catch but that looked even worse, is there a better way of doing this?
The solution is simple: don't catch the exception. As soon as an exception is thrown and is not caught, the whole process will stop. Or catch it but transform it into your own exception with the appropriate error message, and throw this exception.
Side note: you should use a boolean and not a Boolean to store a non-nullable boolean value.
StringBuilder sb = new StringBuilder();
Boolean noError = true;
// Connect to FTP
try {
client.connect(hostname, port);
client.login(username, password);
} catch (ConnectException ce) {
sb.append("Couldn't connect: ");
sb.append(ce.getMessage);
} catch (LoginException le) {
sb.append("Couldn't login: ");
sb.append(le.getMessage);
} finally {
if(client.isConnected()) {
try {
client.disconnect(true);
} catch (Exception e) {
sb.append("failed to disconnect<br>");
}
}