Continuously-running code in Java Play! Framework - java

In the Play!-framework, code is triggered by requesting a URL from the server. The thing is, I need to have some code in the background running continuously from start-up, polling a database for new entries every few minutes, as if it were a normal program with a main() function. As far as I can tell the only way to run code is to navigate to a URL, but that's not what I want here. Any way to accomplish this?

Sounds like you want to use some kind of cron task (correct me if I'm wrong)
In Play 2.x it's done with Akka's scheduler mentioned in the docs
Even more informations and/or samples you can find on the original Akka's docs
In general: in Play you can just schedule some task in the onStart() method of the Global class and then repeat in desired duration as long as required.
Edit: of course Akka is built-in the Play 2.x from the very beginning, actually we could say that Play is built on top of the Akka ;)

Related

AWS and Quartz for running scheduled tasks

Grettings everyone,
We are using AWS as PaaS and we have a couple of microservices deployed there. We got some new requirements to use some sort of cron jobs and schedulers.
For example we have the following scenarios:
A user can set some rules when an event must happen. For example he wants to remove some oudated documents every Friday or once a week or every 2 days
A user can configure creating copies of some objects every day till date A
I used to use Quartz before and it is the first idea that comes in my mind. I think that we can use it in AWS, cause it has RDS(with PostgreSQL for instance).
But I would like to know what sort of other options can I use instead of Quartz(http://www.quartz-scheduler.org) + RDS? May be AWS has something out of the box that can do the same?
What do you think about http://docs.aws.amazon.com/batch/latest/userguide/what-is-batch.html in my case?
Thank you for your time and advice :)
Recently(10-Nov-2022) AWS launched a new service called EventBridge Scheduler and I've already added a detailed answer here, please have a look. So you don't need to handle anything manually Because EventBridge Scheduler is serverless.

JavaFX Transition Equivalent

I have a non-gui application where I want to asynchronously interpolate some data over time.
I had a look at the JavaFX Transition API and it looked ideal. However, for the transitions API to work it has to be run as part of a JavaFX application context - which I don't really want to add the overhead unnecessarily for.
I was wondering if anyone could suggest something equivalent? Or is this something I'm going to have to write myself?
I'd use java.util.Timer for that. You can give it a TimerTask (basically a Runnable) that it will execute every x ms (its period). It also runs on a background thread, which I assume is what you meant with asynchronously?
API: http://docs.oracle.com/javase/8/docs/api/java/util/Timer.html

How to have one Alfresco task signal another Alfresco task in an Activiti workflow?

Lets say that you have a bunch of individual tasks moving through your workflow. These tasks have a high level grouping through their properties. When the tasks get to a specific ReceiveTask in the workflow, a listener checks to see if all the tasks in their group are at the ReceiveTask. If not, the listener does nothing. If yes, all the tasks in that group are signaled to move on to the next step in the workflow.
Example: each task represents a page in a book. When the page task gets to the ReceiveTask, a check is made to see if all the pages in the book are there. If yes, they can all move on. If not, they sit there.
What is the correct way for a Java listener on the ReceiveTask to signal the other pages in the book to continue in the workflow? Code samples would be very helpful.
It seems that each page is in its own instance of the workflow, and that the WorkflowService.signal( pathID, transitionID ) is probably the method to make the other page tasks move, but I'm getting all kinds of workflow exceptions when I do this.
SEVERE: Error while closing command context
org.activiti.engine.ActivitiException: org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener doesn't implement interface org.activiti.engine.delegate.ExecutionListener nor interface org.activiti.engine.delegate.JavaDelegate
Doesn't make much sense to me. Am I running into some sort of Activit transaction problem?
Using Alfresco Community edition 4.2c
I haven't got an answer for that directly, but I've been getting that exact same exception on Alfresco Enterprise 4.1.2 and pretty much the only references I've found were your questions on the Alfresco forums and now on here.
It looks to me like there's a bug in Activiti designer which is putting a task listener in places where an execution listener should go. I went to the XML directly and changed org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener to org.alfresco.repo.workflow.activiti.listener.ScriptExecutionListener and that particular error went away.

Web applications and multi-threading

I'm working on porting a desktop application (WinForm) to a web application (Java/Spring/JPA). The problems are many and I'm struggling a bit...
Now the problem is threading!
In the original application, that performs the export of certain data from the DB, there is a progress-bar indicating the progress of the process.
I want to port this progress-bar in the new web application. To do this I thought of using AJAX and use a separate thread to run the data export.
The main concerns are:
Am I following the right approach? Are there problems using multi-threading in web applications?
If during the export process F5 or refresh button are pressed what exactly happens? How can I stop the process?
How do I update the progress bar periodically? Do I have to make calls via ajax to the server?
I'm primarily an ASP.Net developer but from what I know of the HTTP protocol this just isn't the way to go about it. I've seen a lot of fairly clever solutions for this but in the end what becomes clear is that the HTTP protocol simply isn't designed to work like this.
Obviously you're aware that a flash or silverlight app would be able to do this but that comes with it's own set of issues.
Myself I prefer to keep all the weirdness on the server. In the past I've had to come up with a way to deliver several thousand emails through a web application and update the user on how it's coming along. I designed a set of tables to act as a queue. The web application would simply place any delivery requests in this queue and the progress bar would be determined by a request that checks the status of the items in the queue. Running in the background was a windows service which would also check this queue and was actually responsible for delivering the mail and setting the status of each item as it completed or failed.
It was a bit difficult to develop since windows services can be tricky but once it was up and running it was extremely smooth and reliable. Depending on your circumstances perhaps a simple scheduled task set to run every few minutes would do the trick for you.
I wouldn't necessarily jump straight to running a separate thread explicitly for the export. While it would be ideal to do this, the capability of the web container to do this is going to be a limiting factor. Your traditional Java EE app server generally discourages spawning threads for this (though you can hook up to a thread pool for this). Some containers are great at freeing up the threads from blocking until the work is done (Karaf with Jetty and Camel, for instance) so that they can service other web requests while the export is occurring. But my guess is that you're probably okay with the "start export" thread blocking until it receives a response.
How long does this export take? A couple of seconds, or are we talking closer to minutes here? If it's shorter, I'd think that just putting a little "Waiting" icon with the little circular spinner on it (using your favorite Ajax library, whatever that is) would be sufficient.
If you really want a true status bar that periodically refreshes itself, then yes you'd have to poll for it at some frequency. Presumably that could be a simple request that would load some kind of progress for the job from a database table for that job ID.
Find my answers Inline
I am following the right approach? Are there problem in using multi-threading in web applications?
-Yes you are on correct path. No there is no such problem in multi-threading in web application and its as easy as you do it in WinForm. Instead of using Dispatcher to update the UI, you would be making AJAX calls and with javascript DOM manipulation would take place.
If during the export process F5 or refresh button are pressed what exactly happens? How an I stop the process?
-Unfortunately there is no easy way. The standard way is, when such kind of processing is done and the user hits F5, you would show a dialog(with help of javascript) and inform user that the job is still running. If the user still wants to refresh then you have make another request to the server for cancelling the task.(You need to store thread id or cancellation token some where to cancel the task)
How do I update the progress bar periodically? Do I have to make calls via ajax to the server?
-The standard way is, generally you show show a loading image. IF you want to show a context senstive progress bar, it would mean you have to do polling. Here is an example by Dino Espito. Though its in ASP.NET, you could understand the underlying principle
Dino Espito

How to send periodically data from client to server

A part of my program needs to simulate a GPS. So I am setting up a client-server connection. Where on server my main application would run and on client it would send the GPS string periodically after a particular time interval. I am using JAVA for programming it and I am a bit new to networking area, so if someone can just give me an idea about How do i send my data periodically? The emphasis is on just one part. Periodically after a time interval.
you can use TimerTask Class for your solution. Here is a very useful link for its example.
In its run method you need to deploy your uploading code.
I am also working on same kind of project right now.
Add a java timer to your code that triggers at the interval you specify. In the timer handler, just run some code to send data to the server.
Use a TimerTask with a Timer.
Although your requirement might be simple enough, but i suggest you take a look into quartz scheduler.
It supports from plain simple timer tasks (like every minute or every xx seconds) to the more complex timing scenarios.
Here is one simple example that you can dive more deeply from the source code.

Categories