Bind action to the tomcat pool event - java

I'm working on a web application that's running using Tomcat (it's very Tomcat-oriented, with inclusion of Tomcat jars on the development stage and so on), and i need to bind an action to an event of returning Tomcat thread to it's pool. Is there way to do it?
Alternatively, is there any Tomcat-related thread local logic (like in java ThreadLocal class), that will consider same thread that have been taken from tomcat pool, returned back and taken again as two different "threads"?

Related

How to use single "application wide" thread pool in Spring MVC app running on Tomcat Server

For simpler control over application performance I want to use one application wide thread pool. Our app uses Tomcat as web server, Spring and, on the lower level, makes a lot of calls to external APIs. I need some of this external calls run asynchronously, and want to use for this purpose same thread pool as Tomcat uses for client request service.
Firstly, I thought that I can somehow obtain Tomcat thread pool and then use it, but after some googling I haven't found a way to do it.
My second thought was to set custom thread pool for Tomcat and also use it in other places, where I need async calls. But I haven't found how to implement it also.
Any help and advice will be appreciated.
Choose task executor abstractions from Spring.

How to configure stuck thread handling for only one new restful web service?

I'm developing a restful web service which is supposed to return huge files (2GB or larger). The execution of this web service will of course take time. When testing it with several parallel downloads, the WLS throws the BEA-000337 error (the download takes more time than "Stuck Thread Max Time" is set to in the server configuration).
My problem is that I can't find a way to adjust it only for the new web service. The "Stuck Thread Max Time" setting is set at the server level, meaning it will affect all other services. This is not acceptable. I need a way to set this only for the new service.
It seems that I should be able to use own defined WorkManagers, but the only configuration options I can find in the documentation for WorkManagers deals with the handling of stuck threads and not how to recognize if a thread is stuck.
My question is:
Is there a way to configure in wls, only for this new web service, how it should recognize stuck threads and how it should handle these threads?
The WLS version I am using is 10.3.6
Your processing takes time and default setting of StuckThreadMaxTime is set to 600 seconds, you will see such errors in logs.
You can set StuckThreadMaxTime with bigger value (its located on weblogic console in: domain > Environment > Servers > Admin Server > Configuration/Tuning)
Weblogic is an application server for javaEE applications, and one of the features that make application servers attractive is that they act as containers handling a lot of stuff that otherwise should be done programmatically. One of them il thread management. So among EJB restrictions you can find:
-create or manage threads
-use thread synchronization primitives to synchronize access with other enterprise bean instances
This is because thread handling is up to the container.
In weblogic console you can configure the "Stuck Thread Max Time" parameter per single managed server (Configuration > Tuning tab) changing the default value of 600 seconds.
Anyway remember that when thread processing exceeds 600 seconds you get an error on the logs because Weblogic simply classifies the thread as stuck, but processing goes on, and the thread might finally get unstuck when processing ends.
This might be useful:
https://docs.oracle.com/cd/E84527_01/wls/WLACH/taskhelp/tuning/TuningExecuteThreads.html

Can a Vaadin+Tomcat webapp be used to run a thread continuously?

I am trying to build a webapp with a Vaadin frontend which lets a user upload and process data on our server. The process is quite complicated and is a multi-threaded app (let's call this the 'core'). Whilst designing this app, I thought I could stick everything onto the tomcat server but a colleague of mine told me that natively, Vaadin is RESTful and will thus not run the business process continuously because the application is stateless. He claims that the tomcat JVM will simply go to sleep after running the request and not complete the thread process. Therefore, he suggests that I use RMI to send the data to another process on the same server and process it there instead.
I have a few questions about this:
Is all that he's claimed true? There are some intricacies of implementing Vaadin on Tomcat that I'm not aware of?
More likely I think I'm misunderstanding him and he's actually explaining on why it's better to seperate presentation and business components (which I completely agree with). But on a purely theoretical point of view, would it be possible to stick the multi-threaded core onto the same tomcat server instance as the one running Vaadin?
As far as i know, Vaadin does not use REST services for client-server communication. It is stateful and uses some kind of backing beans.
Regarding your thread issue, if you call your long running task directly from a Vaadin component, it will block the thread processing your request until the task is done. From the browser point of view, you'll have to wait and see the spinning indicator until the process is done (or an exception due to request timeout is thrown).
What you can do is to run your long running task in a separate thread. If you want the new thread to run on the same JVM, you do not need something like RMI.
You can do it by either:
Use an ExecutorService (e.g.: Executors.newSingleThreadExecutor()) and submit a task into
Create a new thread and start it
Do something like: https://vaadin.com/forum/#!/thread/2008536/2010911
Note that you'll probably have to implement some kind of notification mechanism to know when the thread has completed the task.
You can start separate threads from tomcat as needed.
It does not matter what frontend you have for this.
But what's important is to access the vaadin UI components the correct way when you wish to update them from another thread.
For vaadin 7 this has been greatly enhanced, to allow server push out of the box.
In vaadin 6 you had to use some work arrounds for this.
https://vaadin.com/book/-/page/advanced.push.html#advanced.push.running
We use this concept a lot for export and report generation.
- Use click on Export/Report
- On the server we start a (low priority) thread which builds the report/export
- During this, we update a progressbar on the client via server push
- Once the thread has generated the export/report we send it to the webbrowser
If you wish to have a core running always and accepting "jobs" then perhaps you are better served with a job sheduler like quartz or similar.

Removing threadLocal objects on tomcat threads while app shutdown

I am working on a web service and creating thread local instances and only want to remove them during app shutdown (once a threadlocal object is created for thread I want use that object during different service calls on that thread). As threads are created and owned by tomcat , is there any way to remove those threadlocals during application shutdown ??
Tomcat 6 has memory leak detection in place, and Tomcat 7 has actual removal logic - it will automatically remove all thread local objects for you: http://wiki.apache.org/tomcat/MemoryLeakProtection
Ideally you should remove all objects from thread local after request is completed, since the same thread is going to be put back to thread pool and used to serve other requests - in this case thread local values may interfere with subsequent request logic, and cause all kind of security issues.
But if you're specifically looking to keep values in thread local for the whole duration of Tomcat webapp lifetime - Tomcat 7 will take care of cleaning it up for you on webapp shutdown, think of it as garbage collection.

Does Websphere respect Daemon threads?

I've got an app that creates a load of Daemon threads, I'd like each one to shut down when the app is shut down.
I'm a little worried thought that Websphere 7 might not be shutting them all down.
Does anyone know if Websphere 7 treats Daemons threads differently? (I know it should do)
Note:
I know what shouldn't create threads manually, and that I should probably use WebSphere WorkManager or something, but this app has to run in Tomcat and WebSphere.
I know that I should tie in all threads to some context/shutdown mechanism, this is in progress.
Each WAS server runs a single JVM, and daemon threads are tied to the JVM's lifecycle, not the app's lifecycle. Therefore, you should not expect any daemon threads to be shut down when your app stops.
As you've already indicated, you should not create threads manually; the Java EE specs forbid this and the behavior in a Java EE container is different than a standalone Java application as you've already found. Unfortunately, there is currently no Java EE standard for a WorkManager equivalent; however, JSR-236 (Concurrency Utilities for Java EE) may be back as a candidate for inclusion in Java EE 7.
In the meantime, on WAS, you can use the asynchronous beans (WorkManager). We have successfully used this method to tie threads to the application lifecycle.
However, since you need to run in another container as well (Tomcat), there may be some other options to consider handling concurrency in your applications:
CommonJ WorkManager
Servlet 3.0 Asynchronous Servlets
ServletContextListener to hook into the web app lifecycle
Some other potential options for handling concurrency include the following, but these require EJBs, which may not be available in Tomcat:
EJB 3.0 Timer Service
EJB 3.1 Asynchronous Beans
Here are a few related threads on the topic of concurrency in Java EE:
Replacing Websphere's WorkManager in JBoss?
Getting thread from Container?
As has been mentioned you're not supposed to do this, but there isn't a good way to do it. This hasn't caused any problems for me.
This approach requires centralized thread-creation and the use of a listener to terminate threads when the app is stopping.
You'll have to do a few things:
Centralize all thread creation in a single class (call it ThreadService). When a thread is created here put it in a list so you can later loop through the list to stop them all.
Make an interface that your threads implement that allows you to stop each thread via the same interface. Each thread you have has to implement it's own mechanism for handling this. For example if your Thread uses a loop and Thread.sleep() then set stopped=true and interrupt the thread. The loop should check this and break from the loop when stopped=true.
Make a listener and implement ServletContextListener. When contextDestroyed() is called call ThreadService.stopThreads(). Register this listener in web.xml.
Websphere is just a java application. It cannot respect or do not respect deamon threads that are the feature of JVM or java runtime environment. So, if you create deamon thread inside Java EE application it will be deamon in every application server.
Moreover as far as I know even if you create regular thread it will not prevent application server from shutting down: the shutdown mechanism of every application server tries to close all its components and in the end runs System.exit() to win the criminals :) that open threads manually.

Categories