Tomcat stopping issue + Spring boot + service - java

I have 2 Spring boot projects running on same tomcat:
REST-API
Background Service (BS)
Purpose
REST-API : UI application uses this to get things done.
BS : Based on UI activity this service gets executed and gets things done on Real-time .
Details
BS is written as while(1) considering the purpose of the same.
Tomcat Error
1st Attempt:
Using CATALINA_PID: /home/user/bin/pid/tomcat_pid
Tomcat did not stop in time.
PID file was not removed.
To aid diagnostics a thread dump has been written to standard out
2nd Attempt:
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:492)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:406)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
The stop command failed. Attempting to signal the process to stop through OS signal.
Tomcat stopped.
Problems:
1. When I restart tomcat, BS project will only be running and REST-API does not get started (I guess this happens because of while(1) it starts executing and not giving chance to start REST-API project )
2. While stopping tomcat it does not get stopped in a single attempt, when we try to stop one more time then it gets stopped.

This exception mostly indicates that there is no service listening on the IP/port you are trying to connect to, so ,you are trying to connect to the wrong IP/port, or the server is not started.

Related

Can't connect to postgres Scala slick org.postgresql.util.PSQLException: The connection attempt failed

Can't connect to postgres from scala slick app, jdbc connection on VPS.
But I can connect
from local computer to VPS using dbeaver,
and I can connect using psql localhost from inside VPS,
and I can connect to VPS db launching app on my home computer.
So looks like everything working except app on VPS. Port is open, configs are same, Java version 1.8 on home computer and vps, connection string not empty and looks like
jdbc:postgresql://localhost:{custom port}/{db_name}
Error log:
DEBUG c.zaxxer.hikari.pool.HikariPool - database.whale - Cannot acquire connection from data source
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
at org.postgresql.Driver.makeConnection(Driver.java:458)
at org.postgresql.Driver.connect(Driver.java:260)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
at com.zaxxer.hikari.pool.HikariPool.access$100(HikariPool.java:71)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:706)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:692)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:607)
at org.postgresql.core.PGStream.<init>(PGStream.java:75)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
Actually restart the VPS make all working.
I tried to restart firewalls(ufw, iptables etc), postgres, apps, another components, nothing happend. But restart full VPS save the day.
I think it was like a bug in OS, but don't know what kind of...

Request timeout with Java but working on cURL and postman

I'm setting up google recaptcha on my java application and I'm getting connect timeout:
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to www.google.com:443 [www.google.com/172.217.168.164] failed: connect timed out
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:134)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138)
HttpClientHelper.java:138
... 2 more
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:244)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:1
To test if there was any problem with the network, I made the same request using Postman and cURL, both returned the expected response. Then I used postman to generate java code, ran it and got the same error.
Notice that in order for the request to work with Postman I had to turn off "Use System Proxy". This lead me to think that maybe java was using system proxy by default and tried to disable it with System.setProperty("java.net.useSystemProxies", "false"); (also tried with true). Still got same error.
Here is a example of the code used:
HttpResponse<String> response = Unirest.post("https://www.google.com/recaptcha/api/siteverify")
.header("response", "abc")
.header("secret", "abc")
.asString();
Thanks in advance.
Like so often, the reason was dead simple:
The target server only has an IPv4 address. Java for some reason tries to access the target using IPv6 and fails to do so. I assume that this kind of "halts" the ongoing process and this can only be remedied by aborting the hanging thread (what the timeout effectively does)
As soon as I addded -Djava.net.preferIPv4Stack=true to the call of my program, I was able to run it successfully.
source: HttpClient hits timeout but server is available and working flawlessly
maybe?

What's the difference between Windows and Linux/OS X when using the Java SSLSocket?

I wrote a multi-server chat system based on Java under Windows. At the security part, I created one keystore to create the SSLSocket. When I launch 3 servers, it works on Windows(Win10 14393.321) but fails on OS X(Version 10.12 (16A323)) and Linux(Ubuntu 14.04.4 LTS). It really confused me. Here is the keystore part:
System.setProperty("javax.net.ssl.keyStore",keyFilepath);
System.setProperty("javax.net.ssl.trustStore",keyFilepath);
System.setProperty("javax.net.ssl.keyStorePassword","password");
System.setProperty("javax.net.ssl.trustStorePassword", "password");
And when I run the third server on OS X or Linux, it shows:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method) at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at
java.net.Socket.connect(Socket.java:589) at
sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668) at
sun.security.ssl.SSLSocketImpl.(SSLSocketImpl.java:427) at
sun.security.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:88)
at server.AuthorizeServer.MessageReceive(AuthorizeServer.java:99) at
server.AuthorizeServer.main(AuthorizeServer.java:64) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497) at
org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
This is my first time asking on StackOverflow and I really looking forward to your kind help.
Thanks!
java.net.ConnectException: Connection refused
Connection refused is an error message from the TCP stack and means that it could not connect with TCP to the other side. Since SSL/TLS is a layer on top of TCP and is only started once the TCP connect succeeded it means that the problem is not caused by different behavior at the SSL/TLS layer.
That this is not cause by the SSL layer but the TCP layer can also be seen by the stacktrace: Connection refused at java.net.PlainSocketImpl.socketConnect
More likely is that there is something blocking the TCP connection (firewall) or that you've tried to listen/connect to the wrong IP address (e.g. trying to reach a server listening on 127.0.0.1 on Windows from the Linux system). But is impossible to say from the currently provided information what exactly is the case.

how to start Tomcat shutdown listener at the beginning

If i stop Tomcat server as soon as it is started, it gives an error about:
Aug 08, 2016 11:54:26 AM org.apache.catalina.startup.Catalina stopServer
SEVERE: Could not contact localhost:8080. Tomcat may not be running.
Aug 08, 2016 11:54:26 AM org.apache.catalina.startup.Catalina stopServer
SEVERE: Catalina.stop:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:450)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:400)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:487)
The stop command failed. Attempting to signal the process to stop through OS signal.
Tomcat stopped.
Normally, when tomcat is started, the very last message is something like "Server startup in XXX ms". Shutdown listener is started at the end of starting Tomcat.
How can i start "Shutdown Listener" at the beginning when tomcat start.
You do not want to do that(*). To cleanly ask Tomcat to stop, the stop scripts tries to send it a special request. Tomcat developpers have found that there was a possible problem here if Tomcat could not answer requests because any reason: no still active, partially or totally crashed, etc. To solve it, they just log that Tomcat is not in its normal stated and proceed with an OS signal to force stop it. BTW, the scripts proceeds normally with printing Tomcat stopped at the real end if it was able to stop it.
Even if you could build a special version of Tomcat that would try to process request as soon as possible, there would still be a moment for a race condition, not speaking of possible crashes.
If you simply do not want the error message to be displayed, just wrap the real script with a thin wrapper that takes the output and error output, optionaly logs everything for further research in case of a real problem arising, and just search for the string Tomcat stopped. If it is found drop everything else and just display that, if it is not found after the end of the script signal the the error and give the full output for reference.
(*) anyway, I cannot imagine a reliable system for it...

HTTP Status 500 - java.net.ConnectException: Connection refused: connect

I tried to start application on remote host and received the below exception
message java.net.ConnectException: Connection refused: connect
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.net.ConnectException: Connection refused: connect
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.handleServiceException(AbstractApplicationServlet.java:1010)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:548)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
java.net.ConnectException: Connection refused: connect
java.net.DualStackPlainSocketImpl.connect0(Native Method)
java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
java.net.Socket.connect(Socket.java:579)
java.net.Socket.connect(Socket.java:528)
sun.net.NetworkClient.doConnect(NetworkClient.java:180)
sun.net.www.http.HttpClient.openServer(HttpClient.java:378)
sun.net.www.http.HttpClient.openServer(HttpClient.java:473)
sun.net.www.http.HttpClient.<init>(HttpClient.java:203)
sun.net.www.http.HttpClient.New(HttpClient.java:290)
sun.net.www.http.HttpClient.New(HttpClient.java:306)
sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:995)
sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931)
sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:849)
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1299)
com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:633)
com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:799)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
scala.xml.factory.XMLLoader$class.loadXML(XMLLoader.scala:40)
scala.xml.XML$.loadXML(XML.scala:40)
scala.xml.factory.XMLLoader$class.load(XMLLoader.scala:54)
scala.xml.XML$.load(XML.scala:40)
com.exo.AppUser.<init>(AppUser.scala:17)
com.exo.OdaWebApplication.<init>(OdaWebApplication.scala:24)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:526)
java.lang.Class.newInstance(Class.java:374)
com.vaadin.terminal.gwt.server.ApplicationServlet.getNewApplication(ApplicationServlet.java:82)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.createApplication(AbstractApplicationServlet.java:984)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.findApplicationInstance(AbstractApplicationServlet.java:807)
com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:456)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
i tried figuring out the reason and checked some issues but it's still not working
i found some information about it, but it was helpless
i checked some issues like:
service not listening - catalina logged about listening
firewall bocked request - no firewall, no antivirus
user can't connect to server - it's running only in localhost
application don't work - correctly work from IDE
Application run correctly from IDE, and also before host restart.
I can't understand why this is happening.
You've missed the point. You have connected to your Servlet. That's how you got the 500 status back from it. Something in the servlet has failed to connect to something else, while it was doing some XML process.
To diagnose the problem follow the list:
telnet the hostname and port from command line telnet {hostname} {port}
If the connection is established it is not an application error, but network problem
look you are not using proxy - if so you need to setup proxy for your application
If connection refused either there is something wrong with your application and possibly with network either.
Its definitely one of the 4 points you listed
OR
you are hitting the wrong port number?
if that is not in those 4 points then Can You check whether the server is started successfully. may be there is a chance of Java Core(.txt file) generation(tomcat/bin folder). so server will not be started perfectly and also there might be chance of corruption in deployment directory. you can redeploy and check.

Categories

Resources