how to kill inner threads in spring boot application - java

On the Springboot-based application, I am creating a thread to finish a batch job and returning back immediately. Now, I need to forcefully kill the thread in some scenarios like a timeout.
Can anyone help me to fix this use case?

If you're using Java's Executors Framework, you can simply use shutDown() method.
Otherwise, you can send an interrupt signal using Thread.interrupt() after your task is completed.

You can use Javamelody monitoring tool integrated in your application which provides you a nice dashboard of all the current running thread and a kill button to terminate the thread. This tool is extremely easy to integrate in Spring Boot applications - you just need to include couple of dependencies in your build file. Also you would like to protect the dashboard endpoint with some authentication - javamelody provides you with basic auth feature as well.
Another DIY way is to create an endpoint in your application to return thread id for the batch process ( you would need to give some meaningful name to tth thread group to differentiate it from other threads) and another endpoint which takes in thread id as parameter and can stop/kill the thread.

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.

Can I trigger a Dropwizard task execution?

Recently I tried to create a task with Dropwizard that would be triggered within a resource but I can't find a way to do it.
I know that there are a integration with Quartz but that doesn't fit my needs (don't want to schedule tasks).
Is the only option to make a POST to the task endpoint? If so, how can I do a request to /tasks/myTask ?
I don't want to change the architecture to something like producer/consumer, where I create a task in the resource and enqueue it to have then something executing the tasks enqueued.
I posted a sample of how you can use a Managed service to execute tasks.
Running async jobs in dropwizard, and polling their status
Is there a specific reason why you need to invoke the code as a task? I would extract the logic from the Task and put it in it's own class. Then you can use it from multiple places irrespective of the implementation. If it needs to be performed asynchronously, I've had success running Akka workers triggered from inside my Dropwizard services.

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.

Starting long-running process from within Apache ServiceMix

I am looking for suggestions or ideas.
There is an external process (or even a browser) that needs to trigger a long-running process via simple web service call that ideally should run in the same container as that web service. We're using Apache ServiceMix. The web service itself shouldn't stay alive for the duration of the long-running process, besides it may just time-out anyway so we want it to return the response normally pretty much right away.
Originally, I was thinking of using ProcessBuilder() to launch the long-running process as just another app but doing this introduces certain OS dependencies and seems like a less then ideal practice anyway. One of the options we considered is starting another thread from the request and just letting the request complete immediately with a response while the long-running thread would keep on going as long as needed. I fear resource hijacking on the container as well as long-running thread's health when its launcher/parent exits losing any reference to that long-running child.
If anyone has any good ideas for how this can be solved in an elegant way, please let me know.
Thank you very much!
I'm guessing here as you didn't provide the version of your servicemix. Though with Camel which is included with servicemix I'd have two routes the first one providing the web service the second one doing the long running process. The second route should use the seda component. This will give you the async call.

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