Запущенные потоки выглядят как не закрывающиеся даже после возврата из run (). На левой панели NetBeans он показывает большое количество «pool-xx-thread-1-running» и увеличивается с каждым выполнением потока. Ниже мой код:
// прослушивание операций client.subscribe ("/ location / #", new IMqttMessageListener () {publi c void messageArrived (final String topi c, final MqttMessage message) Выдает исключительную ситуацию {final String payload = new String (message.getPayload ());
System.out.println("Received operation " + payload);
if (payload.startsWith("{\"location\":")) {
// execute the operation in another thread to allow the MQTT client to
// finish processing this message and acknowledge receipt to the server
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
public void run() {
if(payload.equals("")) return;
try {
JsonObject jsonObject = new Gson().fromJson(payload, JsonObject.class);
String fixTime="", vehicleid="", deviceTime="", lat="", lon="", speed="",alt="", accuracy="", bearing="";
try{
String locationStr=jsonObject.get("location").toString();
locationStr=locationStr.substring(1, locationStr.length()-1).trim();
locationStr=locationStr.replace("\\", "");
JsonObject locationJson = new Gson().fromJson(locationStr, JsonObject.class);
try{
long millis = Long.parseLong(locationJson.get("mTime").toString());
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
fixTime = df.format(cal.getTime());
}catch(Exception ex){
}
lat=locationJson.get("mLatitude").toString();
lon=locationJson.get("mLongitude").toString();
alt=locationJson.get("mAltitude").toString();
speed=locationJson.get("mSpeed").toString();
accuracy=locationJson.get("mAccuracy").toString();
bearing=locationJson.get("mBearing").toString();
}catch(Exception ex){
ex.printStackTrace();
}
try{
String entityStr = jsonObject.get("entity").toString();
JsonObject entityJson = new Gson().fromJson(entityStr, JsonObject.class);
vehicleid = entityJson.get("id").toString();
vehicleid = vehicleid.replace("\"", "");
}catch(Exception ex){
ex.printStackTrace();
}
try{
String headerStr=jsonObject.get("header").toString();
JsonObject headerJson = new Gson().fromJson(headerStr, JsonObject.class);
String deviceTimeMsStr = headerJson.get("timestamp").toString();
long msTime = Long.parseLong(deviceTimeMsStr);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(msTime);
deviceTime = df.format(cal.getTime());
}catch(Exception ex){
ex.printStackTrace();
}
Connection conn = null;
Statement stmt = null;
try{
String SQL = "INSERT INTO positions(protocol, deviceid, devicetime, fixtime, valid, latitude, longitude, altitude, speed, course, address, attributes, accuracy, network)";
SQL += " VALUES('osmand',(SELECT id FROM devices WHERE uniqueid = '" + vehicleid + "'),'" + deviceTime + "','" + fixTime + "',1," + lat + "," + lon + "," + alt + "," + speed + "," + bearing + ",'',''," + accuracy + ",'')";
Class.forName("com.mysql.jdbc.Driver");
conn =DriverManager.getConnection(MYSQL_SERVERURL, MYSQL_USERID, MYSQL_PASSWORD);
stmt = conn.createStatement();
stmt.executeUpdate(SQL);
System.out.println("Executed " + SQL);
return;
}catch(Exception ex){
ex.printStackTrace();
}finally {
try {
if(stmt != null)
conn.close();
} catch(SQLException se) {
}
try {
if(conn != null)
conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
}catch(Exception ex){
ex.printStackTrace();
}
finally{
return;
}
}
});
}
}
});
снимок экрана