I’m trying to run the server of an application, but when i run it from maven they run for like 30 second or so, and then the server shout down and give me this error:
[WARNING] thread Thread[#39,PostgreSQL-JDBC-Cleaner,5,server.Server] was interrupted but is still alive after waiting at least 15000msecs [WARNING] thread Thread[#39,PostgreSQL-JDBC-Cleaner,5,server.Server] will linger despite being asked to die via interruption [WARNING] NOTE: 1 thread(s) did not finish despite being asked to via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
But if i run the jar the server it doesn’t stop until I turn it off.
The problem is that when i try to connect the client side, as long as the server is on I can use the methods to run queries on the postgres database, but when the server shuts down when I try to call the methods it returns error because the server has shut down, this only happens when I start the server from maven, but when I start it from cmd this does not happen and I can run the methods normally.
The main class is the server:
public class Server extends UnicastRemoteObject implements GenericRemoteInterface{
private final static long serialVersionUID = 10L;
/**
* The database object.
*/
private DBAdapterDatabase db;
/**
* The port number for the server.
*/
private final static int PORT=2020; //! DO NOT CHANGE
/**
* The constructor of the server.
* @param port
* @param db
* @throws IOException
*/
protected Server(int port, DBAdapterDatabase db) throws IOException {
super(port);
this.db=db;
}
/**
* The main method of the server.
* @param args not used in this application
* @throws RemoteException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws RemoteException, IllegalArgumentException {
try {
// Read the arguments
CliParser.parseCli(args);
//--- To set the hostname of the server (needed for RMI)
String ip = null;
if(!CliParser.isLocal()) { //If it's not local, use the external IP
// Code thanks to /questions/2939218/getting-the-external-ip-address-in-java
URL whatismyip = new URL("https://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(
whatismyip.openStream()));
ip = in.readLine();
if(ip==null || ip.isEmpty()){
logError("IP not found, exiting...");
System.exit(-1);
}
System.setProperty("java.rmi.server.hostname", ip);
logInitialMessage("Application starting on external IP: " + ip + "...");
}else{
logInitialMessage("Application starting on internal IP...");
}
//---
// Show environment variables on startup
logEnvironmentVariables();
DBAdapterDatabase db=new DBAdapterDatabase();
try {
db.init();
}catch (SQLException e){
logError("DATABASE ERROR: "+e.getMessage());
System.exit(-1);
}
log("---------------------------------------------------");
log(" SERVER (V."+serialVersionUID+") ");
log("---------------------------------------------------");
// To set up the database pool
ConnectionInstance.setUpDBPool();
// To create the server
Server server = new Server(PORT, null);
// To start the registry
Registry registry = LocateRegistry.createRegistry(PORT);
if(CliParser.isLocal())
log("RMI registry running on port: " + PORT + " on the local host");
else
log("RMI registry running on port: " + PORT + " with IP: " + ip);
registry.bind("server", server);
}
/**
* The method to get the connection.
* @param userID
* @param pass
* @return
* @throws RemoteException
*/
public ConnectionInstanceInterface getConnection(String userID, String pass) throws RemoteException {
User user1=new User();
//ricerca da db
return new ConnectionInstance(user1);
}
/**
* The method to get the connection.
* @return
* @throws RemoteException
*/
public ConnectionInstanceInterface getConnection() throws RemoteException {
return new ConnectionInstance();
}
/**
* Utility method to log environment variables on startup.
* @throws UnknownHostException
*/
private static void logEnvironmentVariables() throws UnknownHostException {
log("------------------ENVIRONMENT VARIABLES--------------------");
log("POSTGRES_ADMIN_USER: "+ CliParser.getAdminUser());
log("POSTGRES_ADMIN_PASSWORD: " + CliParser.getAdminPassword());
InetAddress sqlAddress= InetAddress.getByName(CliParser.getPostgresURL());
String address = sqlAddress.getHostAddress().replace("/", "");
log("POSTGRES_URL: "+ CliParser.getPostgresURL() + " WITH IP: " + address);
log("SERVER_VERSION: " + serialVersionUID);
log("POSTGRES_MAX_CONNECTIONS: " + CliParser.getMaxConnections() + " [1020]");
log("POSTGRES_SHARED_BUFFER: " + CliParser.getSharedBufferSize() + " [128MB]");
log("SERVER_MAX_DB_CONNECTIONS: " + CliParser.getMaxDBConnections() + " [1000]");
log("----------------------------------------------------------");
}
}
The complete output when running with maven is:
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< railwai.Server:Server >------------------------
[INFO] Building Server 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ Server ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource from srcmainresources to targetclasses
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ Server ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO]
[INFO] --- exec:3.1.0:java (default-cli) @ Server ---
[INFO] server.Server.logEnvironmentVariables: ------------------ENVIRONMENT VARIABLES--------------------
[INFO] server.Server.logEnvironmentVariables: POSTGRES_URL: localhost WITH IP: 127.0.0.1
[INFO] server.Server.logEnvironmentVariables: SERVER_VERSION: 10
[INFO] server.Server.logEnvironmentVariables: POSTGRES_MAX_CONNECTIONS: 115 [1020]
[INFO] server.Server.logEnvironmentVariables: POSTGRES_SHARED_BUFFER: 128MB [128MB]
[INFO] server.Server.logEnvironmentVariables: SERVER_MAX_DB_CONNECTIONS: 5 [1000]
[INFO] server.Server.logEnvironmentVariables: ----------------------------------------------------------
[INFO] database.DBAdapterDatabase.init: ---------------------------------------------------
[INFO] database.DBAdapterDatabase.init: DATABASE
[INFO] database.DBAdapterDatabase.init: ---------------------------------------------------
[INFO] database.DBAdapterDatabase.init: Integrity check of the database
[INFO] database.DBAdapterDatabase.init: The database has already been initialized, proceed to start up the server
[INFO] database.DBAdapterDatabase.init: random generato
[INFO] database.DBAdapterDatabase.init: Performing database cache (Expect poor performance for the first few minutes)
[INFO] server.Server.main: ---------------------------------------------------
[INFO] database.DBAdapterDatabase.lambda$init$0: Caching the database
[INFO] server.Server.main: SERVER (V.10)
[INFO] server.Server.main: ---------------------------------------------------
[INFO] database.DBAdapterDatabase.cacheDatabase: Database cached successfully
[INFO] server.DBPool$1.run: Creating DB connections in background.
[INFO] server.DBPool$1.run: DB connections created. The server is ready to accept connections.
[WARNING] thread Thread[#39,PostgreSQL-JDBC-Cleaner,5,server.Server] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[#39,PostgreSQL-JDBC-Cleaner,5,server.Server] will linger despite being asked to die via interruption
[WARNING] NOTE: 1 thread(s) did not finish despite being asked to via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.072 s
[INFO] Finished at: 2024-06-30T14:25:26+02:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
While the pom file is:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>railwai.Server</groupId>
<artifactId>Server</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<junit.version>5.9.2</junit.version>
<javaTarget.version>21</javaTarget.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Compiling plugins -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>server.Server</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>${javaTarget.version}</release>
<source>${javaTarget.version}</source>
<target>${javaTarget.version}</target>
</configuration>
</plugin>
<!-- javadoc plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<configuration>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
</project>
And the command used when I run it is:
compile exec:java -Dexec.mainClass=server.Server
Whit this environment variables:
POSTGRES_ADMIN_USER=postgres;POSTGRES_URL=localhost;POSTGRES_MAX_CONNECTIONS=115;SERVER_MAX_DB_CONNECTIONS=5;POSTGRES_SHARED_BUFFER=128MB
Does anyone know how I can solve this problem?
Thanks in advance to everyone
bonnyx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.