Thread is not getting stopped while polling using camel file poller - java

I am trying to implement simple file polling from one folder to another using camel 2.14 version. I have used pollEnrich with basic timer to poll every 30 seconds. But whenever I tried to stop the tomcat 7.0 server I am getting the logs as:
Catalina.logs
SEVERE: The web application [/CamelPoller] appears to have started a thread named [Camel (camel-1) thread #0 - timer://myTimer] but has failed to stop it. This is very likely to create a memory leak.
Aug 14, 2015 2:50:06 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/CamelPoller] appears to have started a thread named [Camel (camel-1) thread #1 - file://D:/Input] but has failed to stop it. This is very likely to create a memory leak.
FilePollerDemo.java
public class FilePollerDemo {
public FilePollerDemo() {
CamelContext context = new DefaultCamelContext();
try {
context.addRoutes(new RouteBuilder() {
public void configure() {
from("timer://myTimer?period=30000")
.pollEnrich("file://D:/Input?fileName=test.txt")
.to("file://D:/Output");
}
});
context.start();
// context.stop();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
I have commented out the context.stop(), because if I am using it, the file polling is not happening or if I used like this :
context.start();
thread.sleep(30000);
context.stop();
then the poller run only for once.
Please help me I am new to camel.

Related

The web application [VehicleRouting] appears to have started a thread named [drools-worker-4] but has failed to stop it

I build a vehicle routing web application using optaplanner. When I tried to deploy my web application to a tomcat 8 server, and tried to run it from my web browser, it produces a warning in my tomcat log file. The log said something about my web application started a thread and failed to stop it, and probably will create a memory leak.
I have write a destroy method where my ExecutorService object will call shutdown method to make sure every thread it started was terminated. Here is my code :
public class OptimizerService implements IOptimizerService {
private ExecutorService executor;
#Override
public synchronized Boolean startSolving() throws Throwable {
executor = Executors.newFixedThreadPool(2);
...
}
...
// other methods
...
#PreDestroy
public synchronized void destroy() {
executor.shutdown();
}
}
But why I still got those warning in tomcat log?
Here is the tomcat log :
09-Jun-2017 08:25:56.377 WARNING [http-nio-18081-exec-295] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [VehicleRouting] appears to have started a thread named [drools-worker-4] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
Any comment will be appreciated. Thanks and regards.
executor.shutdownNow(); interrupts the threads, but executor.shutdown(); does not. That latter just waits until the tasks are finished, good luck if you have a 2h solver running...
If the Solver detects that its thread is interrupted, it terminates (pretty much the same as a normal Termination), which in turns calls KieSession.dispose(). I presume dispose() takes care of any drools spawned threads.
That's the theory at least :)

How to run a thread after JVM exits in java?

I am writing an application where it has two threads:
[1] main
[2] threadX
main thread is command line program and starts an application(mongodb) in background and then exits.
threadX is the Server part of my application which requires to be active as long as the application(mongodb) is running.
main takes two arguments: start and stop.
On start: it starts -> start mongod and threadX.
On stop: it starts -> kills mongod and (should)stops threadX.
But even after setting threadX as daemon it stops after main thread exits.
Proof:
public class Index {
public static void main(String [] args) {
Logger log = Logger.getLogger("Index logging");
try {
FileHandler logfile = new FileHandler("test.log");
logfile.setFormatter(new SimpleFormatter());
log.addHandler(logfile);
} catch (SecurityException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread daemonThread = new Thread() {
public void run() {
while (true) {
try {
log.log(Level.INFO,"Try block executed");
Thread.sleep(1000l);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
#Override
public void finalize() {
log.log(Level.INFO,"Finalize method called");
}
};
daemonThread.setDaemon(true);
daemonThread.start();
try {
Thread.sleep(3500l);
} catch (Throwable t) {
//NO-OP
}
}
}
Output:
Apr 08, 2017 2:30:54 PM num.Index$1 run
INFO: Try block executed
Apr 08, 2017 2:30:55 PM num.Index$1 run
INFO: Try block executed
Apr 08, 2017 2:30:56 PM num.Index$1 run
INFO: Try block executed
Apr 08, 2017 2:30:57 PM num.Index$1 run
INFO: Try block executed
As you can see it does not continues to write logs after main exits
What i want is a thread that can be started in background and continue to run even when main thread exits and that thread can be stopped after i restart my app. Just like console applications which starts processes in background and quit leaving console available to user. The user can then use command to stop that background process.
Daemon threads do exit after the main method exits - if you do not want your thread to exit you should not make it a daemon thread.
Remove the line
daemonThread.setDaemon(true);
or change it to
daemonThread.setDaemon(false);
From the Javadoc of the setDaemon method:
The Java Virtual Machine exits when the only threads running are all
daemon threads.
Create a User Thread and not a Daemon Thread. Why do you need to set the Thread as Daemon?
JVM will not terminate a User Thread and it will wait for it to finish before exiting unlike Daemon Thread.
Hope this Helps
If I get right at what you're trying to accomplish: you want a program to control mongodb. Separate invocations of the program may have diffrent commands as their command line arguments.
In the case of "start": your app should start mongodb as a separate process so it can exit right after mongodb started. You wont need thread to keep mongodb running.
In the case of "stop": You'll have to connect to the mongodb process and tell it to shut down gracefully, wait for it to end and end your own program.

Java exception does not propagate to Scala

the following scala code seems to throw a java exception, but keeps on executing other lines of code :
object FirstMain {
def main(args: Array[String]): Unit = {
var mongoClient : MongoClient = MongoClients.create() // this is a java method
println("hello")
Thread.sleep(500)
println("hello2")
}
console output :
Feb 17, 2017 7:57:49 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Feb 17, 2017 7:57:50 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:253)
[...] // stacktrace
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
[...] // stacktrace
hello
hello2
I tried using a try/catch block to deal with the exception but I get the same output as inthe fist code snippet. The following code never prints "do something !" :
object FirstMain {
def main(args: Array[String]): Unit = {
try{
var mongoClient : MongoClient = MongoClients.create()
}
catch {
case e : MongoSocketOpenException => println("do Something")
}
println("hello")
Thread.sleep(500)
println("hello2")
}
}
Anyone knows how to catch exception thrown by async java code in scala ?
Thanks in advance for your help.
This has little to do with async or scala. The method you are calling is synchronous. It doesn't return a Future or other async type. The client you are creating is async but the method is not. The reason that you cannot catch the exception is because Mongo is likely already catching the exception and not letting it bubble up.

Memory leak error in tomcat server even after removed quartz related code [duplicate]

I am using Quartz to run a job every hour. The servlet is running on Tomcat and I am using the ServletConextListener to listen for when the context is destroyed.
When I shut down tomcat, I get the message:
"appears to have started a thread named [MyScheduler_Worker-1] but has failed to stop it".
But later I see this message:
"[DEBUG] 28 Sep 11:45:26.671 AM MyScheduler_Worker-1 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down."
So is it safe to assume that there is no memory leak because of this thread?
Here is how my log looks:
{SEVERE: The web application [/*************] appears to have started a thread
named [MyScheduler_Worker-1] but has failed to stop it. This is very likely to c
reate a memory leak.
Sep 28, 2011 11:45:26 AM org.apache.catalina.loader.WebappClassLoader clearRefer
encesThreads
SEVERE: The web application [/*************] appears to have started a thread
named [MyScheduler_Worker-2] but has failed to stop it. This is very likely to c
reate a memory leak.
Sep 28, 2011 11:45:26 AM org.apache.catalina.loader.WebappClassLoader clearRefer
encesThreads
SEVERE: The web application [/*************] appears to have started a thread
named [MyScheduler_Worker-3] but has failed to stop it. This is very likely to c
reate a memory leak.
[DEBUG] 28 Sep 11:45:26.671 AM MyScheduler_Worker-2 [org.quartz.simpl.SimpleThre
adPool]
WorkerThread is shut down.
[DEBUG] 28 Sep 11:45:26.671 AM MyScheduler_Worker-1 [org.quartz.simpl.SimpleThre
adPool]
WorkerThread is shut down.
[DEBUG] 28 Sep 11:45:26.671 AM MyScheduler_Worker-3 [org.quartz.simpl.SimpleThre
adPool]
WorkerThread is shut down.
I know this is an old thread but in case others are looking for it.
We use to get the warnings of threads all the time until we added code to shutdown the Quartz Scheduler in our ServletContextListener.shutDown() method.
To shutdown the Scheduler:
quartzScheduler.shutdown();
int ct = 0;
// Try waiting for the scheduler to shutdown. Only wait 30 seconds.
while(ct < 30) {
ct++;
// Sleep for a second so the quartz worker threads die. This
// suppresses a warning from Tomcat during shutdown.
Thread.sleep(1000);
if (quartzScheduler.isShutdown()) {
break;
}
}
You can assume there is no memory leak because you see thread shutdown message. However, its possible to over come warning by clearing up threads before shut-down.
The shutdown-hook plugin catches the event of the JVM terminating, and calls shutdown on the scheduler.
Details:- http://quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigPlugins

Jetty: Stopping programatically causes "1 threads could not be stopped"

I have an embedded Jetty 6.1.26 instance.
I want to shut it down by HTTP GET sent to /shutdown.
So I created a JettyShutdownServlet:
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(202, "Shutting down.");
resp.setContentType("text/plain");
ServletOutputStream os = resp.getOutputStream();
os.println("Shutting down.");
os.close();
resp.flushBuffer();
// Stop the server.
try {
log.info("Shutting down the server...");
server.stop();
} catch (Exception ex) {
log.error("Error when stopping Jetty server: "+ex.getMessage(), ex);
}
However, when I send the request, Jetty does not stop - a thread keeps hanging in org.mortbay.thread.QueuedThreadPool on the line with this.wait():
// We are idle
// wait for a dispatched job
synchronized (this)
{
if (_job==null)
this.wait(getMaxIdleTimeMs());
job=_job;
_job=null;
}
...
2011-01-10 20:14:20,375 INFO org.mortbay.log jetty-6.1.26
2011-01-10 20:14:34,756 INFO org.mortbay.log Started SocketConnector#0.0.0.0:17283
2011-01-10 20:25:40,006 INFO org.jboss.qa.mavenhoe.MavenHoeApp Shutting down the server...
2011-01-10 20:25:40,006 INFO org.mortbay.log Graceful shutdown SocketConnector#0.0.0.0:17283
2011-01-10 20:25:40,006 INFO org.mortbay.log Graceful shutdown org.mortbay.jetty.servlet.Context#1672bbb{/,null}
2011-01-10 20:25:40,006 INFO org.mortbay.log Graceful shutdown org.mortbay.jetty.webapp.WebAppContext#18d30fb{/jsp,file:/home/ondra/work/Mavenhoe/trunk/target/classes/org/jboss/qa/mavenhoe/web/jsp}
2011-01-10 20:25:43,007 INFO org.mortbay.log Stopped SocketConnector#0.0.0.0:17283
2011-01-10 20:25:43,009 WARN org.mortbay.log 1 threads could not be stopped
2011-01-10 20:26:43,010 INFO org.mortbay.log Shutdown hook executing
2011-01-10 20:26:43,011 INFO org.mortbay.log Shutdown hook complete
It blocks for exactly one minute, then shuts down.
I've added the Graceful shutdown, which should allow me to shut the server down from a servlet; However, it does not work as you can see from the log.
I've solved it this way:
Server server = new Server( PORT );
server.setGracefulShutdown( 3000 );
server.setStopAtShutdown(true);
...
server.start();
if( server.getThreadPool() instanceof QueuedThreadPool ){
((QueuedThreadPool) server.getThreadPool()).setMaxIdleTimeMs( 2000 );
}
setMaxIdleTimeMs() needs to be called after the start(), becase the threadPool is created in start(). However, the threads are already created and waiting, so it only applies after all threads are used at least once.
I don't know what else to do except some awfulness like interrupting all threads or System.exit().
Any ideas? Is there a good way?
Graceful doesn't do what you think it does - it allows the server to shutdown gracefully, but it does not allow you to shutdown from inside a servlet.
The problem is as described in the mailing-list post you linked to - you're trying to stop the server, while you're still processing a connection inside the server.
You should try changing your servlet's implementation to:
// Stop the server.
new Thread()
{
public void run() {
try {
log.info("Shutting down the server...");
server.stop();
log.info("Server has stopped.");
} catch (Exception ex) {
log.error("Error when stopping Jetty server: "+ex.getMessage(), ex);
}
}
}.start();
That way the servlet can finished processing while the server is shutting down, and will not hold up the shutdown process.

Categories

Resources