Managing threads in Grails Services - multithreading

So I have a service set up to import a large amount of data from a file the user uploads. I want to the user to be able to continue working on the site while the file is being processed. I accomplished this by creating a thread.
Thread.start {
//work done here
}
Now the problem arises that I do not want to have multiple threads running simultaneously. Here is what I tried:
class SomeService {
Thread thread = new Thread()
def serviceMethod() {
if (!thread?.isAlive()) {
thread.start {
//Do work here
}
}
}
}
However, this doesn't work. thread.isAlive() always return false. Any ideas on how I can accomplish this?

I would consider using an Executor instead.
import java.util.concurrent.*
import javax.annotation.*
class SomeService {
ExecutorService executor = Executors.newSingleThreadExecutor()
def serviceMethod() {
executor.execute {
//Do work here
}
}
#PreDestroy
void shutdown() {
executor.shutdownNow()
}
}
Using a newSingleThreadExecutor will ensure that tasks execute one after the other. If there's a background task already running then the next task will be queued up and will start when the running task has finished (serviceMethod itself will still return immediately).
You may wish to consider the executor plugin if your "do work here" involves GORM database access, as that plugin will set up the appropriate persistence context (e.g. Hibernate session) for your background tasks.

Another way to do this is to use Spring's #Async annotation.
Add the following to resources.groovy:
beans = {
xmlns task:"http://www.springframework.org/schema/task"
task.'annotation-driven'('proxy-target-class':true, 'mode':'proxy')
}
Any service method you now annotate with #Async will run asynchronously, e.g.
#Async
def reallyLongRunningProcess() {
//do some stuff that takes ages
}
If you only want one thread to run the import at a time, you could do something like this -
class MyService {
boolean longProcessRunning = false
#Async
def reallyLongRunningProcess() {
if (longProcessRunning) return
try {
longProcessRunning = true
//do some stuff that takes ages
} finally {
longProcessRunning = false
}
}
}

Related

how to do something when liferay module stop

i am making cron job like loop to do something using new thread.
when module stop, this thread keeps running, so when i deployed updated module, i'm afraid it will make duplicate thread doing similar task
#Component(immediate = true, service = ExportImportLifecycleListener.class)
public class StaticUtils extends Utils{
private StaticUtils() {}
private static class SingletonHelper{
private static final StaticUtils INSTANCE = new StaticUtils();
}
public static StaticUtils getInstance() {
return SingletonHelper.INSTANCE;
}
}
public class Utils extends BaseExportImportLifecycleListener{
public Utils() {
startTask();
}
protected Boolean CRON_START = true;
private void startTask() {
new Thread(new Runnable() {
public void run() {
while (CRON_START) {
System.out.println("test naon bae lah ");
}
}
}).start();
}
#Deactivate
protected void deactivate() {
CRON_START = false;
System.out.println(
"cron stop lah woooooooooooooooooy");
}
}
i'm using liferay 7
I have populated task that i store from db, so this thread is checking is there a task that it must do, then if it exist execute it.
I'm quite new in osgi and liferay. i've try to use scheduler and failed and also exportimportlifecycle listener but dont really get it yet
think again: Do you really need something to run all the time in the background, or do you just need some asynchronous processing in the background, when triggered? It might be better to start a background task as a one-off, that automatically terminates
Liferay provides an internal MessageBus, that you can utilize to listen to events and implement background processing, without the need for a custom thread
You're in the OSGi world, so you can utilize #Activate, #Modified, #Deactivate (from org.osgi.service.component.annotations) or use a org.osgi.framework.BundleActivator.
But, in general, it's preferable if you don't start your own thread

Vert.x Future/Promise Handler not called in Groovy

I got a pretty small Verticle that should connect to a database and regularly poll a table and send the objects to the event bus. Thus far I can connect to the database, but the handler afterwards gets not executed and my polling timer does not start. I guess it's something obvious and appreciate every help.
I use Vert.x 3.8 Promises as Futures are deprecated (just like in the example behind that link). As you can see in my code, using deprecated Futures works just fine! But who want's to use deprecated code, heh? Either I'm doing something wrong or that's a bug in Vert.x, what I don't assume.
My Vert.x is at 3.8.1
class JdbcVerticle extends AbstractVerticle {
private SQLConnection connection
#Override
void start(Promise<Void> startPromise) {
def jdbcParams = config().getJsonObject('connection')
// This gets executed:
testFuture().handler = { println "Test Future handler runs!" }
// This is never executed :-(
connect(jdbcParams).handler = { println "Connected..." }
}
Future<Void> connect(JsonObject jdbcParams) {
def promise = Promise.<Void>promise()
def client = JDBCClient.createShared(vertx, jdbcParams)
client.getConnection() { connection ->
if(connection.failed()) {
promise.fail(connection.cause())
} else {
this.connection = connection.result()
promise.complete()
}
}
return promise.future()
}
Future<Void> testFuture() {
def future = Future.<Void>future()
vertx.setTimer(200) { future.complete() }
return future
}
}

check asynchronous threads state in java

I have method in class MyClassB which is triggered asynchronously from a method of MyClassA:
public void getProductCall()
{
new Thread(new Runnable() {
#Override
public void run() {
try {
productRequest = service.createS4ProductRequest(getRepriceItems());
//Below is a rest call to another system
String response = pricing.getS4ProductResponse(quote.getAssetQuoteNrAndVrsn(), productRequest);
//I'm using the below 2 lines to check from ClassA's method to see if this process has ended
setProductResponse(response);
productPriceProcessEnded=true;
} catch (Exception e) {
productPriceErrorOccured=true;
e.printStackTrace();
}
}
}).start();
}
This is the piece of code in MyClassA i used to check if the above method is complete.
for(int i=0;i<1000000000;i++)
{
if(!networkAsynCalls.isListPriceErrorOccured())
{
if(networkAsynCalls.isListPriceprocessEnded())
{
return networkAsynCalls.getListReponse();
}
else
{
Thread.sleep(250);
continue;
}
}
else
return null;
}
instead of using this random for loop can i use some inbuilt method or service pool or something ?
Because,
1) This thread on method is in another class
2) In class MyClassB i have few more methods like this, so i need to check the status of all the methods in MyClassA
Thanks for any help.
If I undestand what you're trying to do is dispatch some code to be ran asynchronously, then be able to wait until it is completed (successfully or failed). If that's the case, you should take a look at Futures.
Here is an example based on the Javadoc:
FutureTask<String> future =
new FutureTask<String>(new Callable<String>() {
public String call() {
// do stuff
return "result";
}});
This code creates an object "future" that can be invoked to execute searcher.search(target). At this point, the code is not executed at all. You simply have an object representing a computation that may be executed asynchronously. To do so, you'd call:
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.execute(future);
This snippet created an Executor (which is a fixed pool of 5 threads), then handed over the future to it for execution. The executor will run the computation from Future asynchronously.
Future offers some methods (see the Javadoc) to wait until completion, cancel, check completion status, etc. For example,
String result = future.get();
will block, waiting for the result indefinitely. A get(10, TimeUnit.SECONDS) will wait for 10 seconds and if the future has not completed, throw.

How to hit 1000 endpoints using multi threading in groovy?

I have a requirement to hit endpoints more than 1000 times to fetch some data from website. So i read some tutorials to use Multi Threading to achieving it. But at a time i want to use only 13 threads on same method.
So basically i am using ExecutorService to run 13 threads at one time:
ExecutorService threadPool = Executors.newFixedThreadPool(13);
for (int itLocation = 0; itLocation < locationList.size(); itLocation++) {
//some code like
ScraperService obj = new ScraperService(threadName,url)
threadPool.submit(obj);
}
threadPool.shutdown();
My Groovy Class named as ScraperService is implementing the Runnable interface.
#Override
void run() {
println("In run method...................")
try {
Thread.sleep(5000);
someMethod()
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Problem:
My problem is that my ExecutorService.submit(obj) and ExecutorService.execute(obj) is not calling my run() method of Runnable interface.
In Groovy/Grails:
There is also a executor plugin Executor Plugin in grails but i didn't found any appropriate example how to use it.
GPars is excellent for this type of thing.
Your ScraperService can just be responsible for handling the scraped data like below or maybe fetching it too, whatever.
import groovyx.gpars.GParsPool
def theEndpoint = 'http://www.bbc.co.uk'
def scraperService
GParsPool.withPool( 13 ) {
(1..1000).eachParallel {
scraperService.scrape theEndpoint.toURL().text
}
}
First of all i think there is problem in groovy service class with #Transnational annotation which doesn't allow to call run() method of Runnable interface. If you'll remove the #Transnational then it will call the run() method. It happened also in my case. But i am not sure, there may be some another reason. You can directly use:
ExecutorService threadPool = Executors.newFixedThreadPool(13)
threadPool.execute(new Runnable() {
#Override
void run() {
Thread.sleep(5000);
someMethod()
}
})
Extra (As i read your question)
If you are using multiple threads on same method then it can be complex as all threads will use the same local variables of that method which can occurs problems. It is better to use multiple threads for different different work.
But if you want to use same method for executing multiple threads then in my scenario it is better to use Executors.newSingleThreadExecutor().
ExecutorService threadPool = Executors.newSingleThreadExecutor();
newSingleThreadExecutor() calls a single thread so if you want to execute multiple tasks on it then it does not create multiple threads. Instead, it waits for one task to complete before starting the next task on the same thread.
Speed: newSingleThreadExecutor in comparison of multi threads will be slow but safer to use.
the threadPool.submit does not execute task
use threadPool.execute(obj) or threadPool.submit(obj).get()
instead of threadPool.submit(obj)
check the documentation for details:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#submit(java.util.concurrent.Callable)
example:
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
ExecutorService pool=Executors.newFixedThreadPool(3)
for(int i=0;i<7;i++){
int x=i;
Thread.sleep(444);
pool.execute{
println "start $x"
Thread.sleep(3000+x*100);
println "end $x"
}
}
println "done cycle"
pool.shutdown() //all tasks submitted
while (!pool.isTerminated()){} //waitfor termination
println 'Finished all threads'

Thread thread = new Thread(() -> { /code } and Executorservice.submit(thread). How to write junit on this using powermock

Please help me write Junit for this piece of code using Mockito /Powermock, Finding it difficult due to lamda expression and executor service.
public class myClass {
ExecutorService executorService;
public void testMethod(String a){
Thread thread = new Thread(() -> {
//logic
a= testDAo.getStatus();
while (true) {
if (Thread.interrupted()) {
break;
}
if (a() != "done" || a() != "fail") {
Thread.yield();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
} else {
break;
}
}
}
Future task = executorService.submit(thread);
while (!task.isDone()) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
}
Various things here:
first of all: for testing executors and parallel execution, using a same thread executor can be extremely helpful (because it takes out the parallel aspect)
you have difficulties writing a unit test - because your production code is way too complicated.
Thus the real answer is: step back and improve your production code. Why again are you pushing a thread into an executor service?
The executor service is already doing things on a thread pool (at least that is how you normally use them). So you push a thread into a thread pool, and then you have code that waits "two" times (first within that thread, and then outside on the future). That just adds a ton of complexity for small gain.
Long story short:
I would get rid of that "inner thread" - just have the executor task wait until the result becomes available
Then: if lambda's give you trouble - then don't use them. Just create a named small class that implements that code. And then you can write unit tests for that small class. In other words: don't create huge "units" that do 5 different things. The essence of a good unit is to one thing (single responsibility principle!). And as soon as you follow that idea testing becomes much easier, too.

Resources