Java concurrent safe worker pool queue - spring

I am writing Spring (Java 8) web application and per each request(separate thread) my application make a few tasks, which should be completed as quickly as possible to return result to client in browser, so I'd like to find worker pool library, which can be accessed from different threads in safe way.
I have read about Execution pool, Rabbit MQ, but I couldn't find information about the feature of accessing the tasks queue from different threads.
I will really appreciate if somebody can give me advice how to do it in Java.

Here you can use asynchronous method call supported by Spring via #Async annotation.
The #Async annotated method will be executed in new thread and the result will be available in Future object (void return is also supported). Do note that the method call is non-blocking which will help return the response quickly without waiting for every task to complete. However if required the main thread can be made to wait for all task i.e. Future to complete via Future.get() which is blocking.
To configure the asynchronous support, annotate the configuration class with #EnableAsync and provide below method definition as described here.
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(500);
executor.initialize();
return executor;
}

Related

How to pause/resume individual Spring JMS message listeners

My Spring Boot JMS application is consuming messages from several SQS queues. Each queue needs to connect to a different external resource in order to process its messages. If there's an external resource failure I expect the consumer requiring that resource will rapidly drain the queue to a DLQ and that's no fun.
I need to be able to suspend (pause) the message listener for each consumer independently when my code detects that its resource is offline (e.g. comms exception calling REST endpoint).
How can I do this?
After searching for platform support for this, I found the AbstractJmsListeningContainer which (via the Lifecycle interface) supports stop() and start() methodsm though it doesn't document whether start can be invoked following a stop call.
My concern is that there appears to be one shared instance among my multiple #JmsListener annotated consumers; so stopping one queue stops them all and I don't want that.
How can I achieve my end goal of pausing individual consumers?
I have seen references to using multiple bean definitions like so:
#Bean
SimpleMessageListenerContainer container1(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
// snip
}
#Bean
SimpleMessageListenerContainer container2(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
// snip
}
...but never seen any explanation stating how and when one will be used versus the other.
See my answer to this question.
Yes, you can call start() after stop().
Note that stop() only stops the threads; the connection remains open.
If you want to shut everything down, call shutDown() after stop() and then initialize() before start().
You should not call stop() on a listener thread, though - hand it off to another thread and wait until isRunning() is false.

Where should i store thread dependent data within a receiving rabbitListener component in a multithreaded environment?

I'm using the annotation based approach of spring amqp in a multithreaded environment (i have multiple consumers => multiple rabbit listener containers).
#RabbitListener(queues = "tasks")
public void receiveMessage(#Payload Task task) {
// usage of httpClient here with its own httpContext (would be fine)
// this method gets called from different listenerContainers / threads
}
My component which contains the annotated receiveMessage() method needs to do some http calls with the apache http client. Since i'm working with multiple consumers at the same time, this method gets called from different threads and the apache http client documentation says that i should create a httpContext for each thread to be thread safe. Since all threads are calling the same component method i can't put the httpContext into the component.
Is there something like a listener container context for each listener container where i can put the httpClientContext? Or does somebody have an idea how to solve this easy? I thought about ThreadLocal or a central registry for httpContexts but it would be fine if this would be more easy.
There is nothing like that provided by the framework; the simplest solution is to store them in something like a LinkedBlockingQueue and check one out, use it, and put it back in the queue when you're done (creating one as necessary if the queue is empty).
ThreadLocal will work too, but I prefer to use a pool.

How to create an application to poll 10 JMX URI and store in DB in Spring

How to create an application to poll 50 JMX URI and store the mbeans in DB.
I have created a class which will connect to jmx server and fetch the mbeans value.
My question is how to trigger my created class and method to continues polling.
Moreover, I need two function to start and stop this polling anytime.
What can I use - Spring Scheduler, Spring Integration and how or should I create different class having thread and infinite loop.
Take a look at Spring Integration and its JMX Tree Polling Channel Adapter.
With Spring Scheduler (#Schedule annotation) it is easy to have a method that is invoked (for example) every minute. But it is not possible* to change (disable) that scheduling.
Anyway you could:
use the quarz-framework directly, then you could change the scheduling, or
do implement a dirty (but easy) hack:
Let the schueduler run, but disable the invocation of the real polling method:
private volantile boolean pollingEnabled;
#Schedule(fixedRate=1000)
public void pollTrigger() {
if (pollingEnabled) {
doPolling();
}
}
*more precise: to my knowlege there is no easy way

Spring Batch - interrupt thread when job stops

On stopping a job the manual says
The shutdown is not immediate, since there is no way to force immediate shutdown, especially if the execution is currently in developer code that the framework has no control over, such as a business service.
My long-running steps performs checks for Thread.currentThread().isInterrupted() and throws InterruptedException. Surely there is some way to interrupt the thread running the job?
(Note: I'm using SimpleAsyncTaskExecutor with TaskletStep)
Create a bean to implement
ApplicationListener<ContextClosedEvent> onApplicationEvent()
Then you can shut down the task.
#Component
class ContextClosedHandler implements ApplicationListener<ContextClosedEvent> {
#Autowired SimpleAsyncTaskExecutor executor;
void onApplicationEvent(ContextClosedEvent event) {
executor.shutdown();
}
}
This post about the lifecycle of beans might help as well
http://technicalmumbojumbo.wordpress.com/2011/04/13/spring-framework-an-introduction-part-ii-object-lifecycle-autowiring-internationalizationi18n/

Ways to get to thread created in SimpleAsyncTaskExecutor.createThread(Runnable runnable)

In my application using Spring Batch 3.0.1 I need to get access to the thread created by SimpleAsyncTaskExecutor.createThread(Runnable runnable) before it is started in doExecute.
This needs to be done to attach security context stuff to the newly created thread.
Of course I could extend SimpleAsyncTaskExecutor and use this class, but that seems a crude solution.
I tried using Spring AOP and interceptors, but cannot intercept the right method.
Any ideas?
Edit:
protected void doExecute(Runnable task) {
Thread thread = (this.threadFactory != null ? this.threadFactory.newThread(task) : createThread(task));
thread.start();
}
I believe the correct way to handle this type of behavior would be to create your own ThreadFactory and pass that to the SimpleAsyncTaskExecutor. That would allow you access to the threads as they are being created.
You can read more about the SimpleAsyncTaskExecutor here: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/task/SimpleAsyncTaskExecutor.html
You can read more about the ThreadFactory here: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadFactory.html

Resources