Running a continuous background task in Play 2.4.2 server - java

I'm creating a web application that displays incoming events as strings onto the web browser. The events should be coming in from a listener that I have defined in another part of the program. These events will be emitted through a socket to the client side.
The problem: I need to keep the listener continually running in order to receive messages. The listener is essentially one block of code that's something like listener.run(), and it doesn't terminate. Right now, I am making a separate thread that runs this block of code and I have configured Global settings to run the thread on application startup.
I don't want my task to stop and start every time I refresh the page. How can I make the listener run right when the server starts?

Look at the Scheduling asynchronous tasks at the end of the doc: https://www.playframework.com/documentation/2.4.x/JavaAkka

Related

In Azure DevOps, how to gracefully restart when deploying an app to Functions

In Azure DevOps Pipeliens, when deploying an app to Functions, the app may restart during the process of the app.
Is there a way to monitor if the Functions app is running in the pipeline, make sure it's done, and then deploy the app?
Conditions
Functions runtime: Java
Trigger: Service Bus Trigger
I tried to check the lock status of Service Bus messages or the processing status of the Functions app with the Azure CLI, but it seems that there is no interface to check the processing status.
https://learn.microsoft.com/en-us/cli/azure/functionapp?view=azure-cli-latest
https://learn.microsoft.com/en-us/cli/azure/servicebus/queue?view=azure-cli-latest
You should never rely on that better build your functions to execute fast.
When Azure function sends signal to stop in c# we have CancellationToken this way we can have extra code to implement shutdown, otherwise as soon as AF get signal to stop it will not accept new events from service bus but will continue to execute current functions, and if they wont stop for some time they can be terminated (cant find exact time but will update answer )
I would also suggest you to utilise deployment slots this way you can minimise your downtime.

Android service and native threads

Given the limitations that are being imposed on background services in later versions of Android, how do you accomplish the following:
The application's JAVA background service and the native C++ threads which are started by the JAVA background service via JNI continue to run regardless of the phone's state (screen on or off) and regardless of the application's state (activity life cycle). If an activity has been destroyed, the background service must continue to run.
If the user clears the application from the task list (menu button), the background service continues to run and so does the C++ threads.
If the user presses the menu back button, the background service continues to run and so does the C++ threads.
If the user navigates to the OS settings (applications) and selects FORCE CLOSE/STOP for the application, then the application AND the background service is stopped/destroyed.
One of the native threads is responsible for listening and processing UDP data via a socket that listens for multicast data. It is critical that this continues to work regardless of phone/app state (unless the app is forcefully closed).
Not sure if I get your problem right, but all these points you mention, one can accomplish by using the Android Service Component like describede here: https://developer.android.com/guide/components/services.html
I worked with this some time ago and it did just these points you require.
Please correct me if I'm wrong.

Creating an asynchronous processo in a java servlet

I want to create a java servlet that runs an .exe application in the server but as an asynchronous process and returns to the client a hash or token to indentify the process so the client can ask later if the process has finished.
For example:
A user writes in the web browser something like: http://server:8080/videomanager?action=process
The servlet receives the request and execute the .exe application but it doens't wait for it to finish, it inmediately responds to the user with a token/hash=12312312ddsadad31231
The user recieves this token in his web browser
The user can then send a request like: http://server:8080/videomanager?action=status&token=12312312ddsadad31231
The servlet recieves this request and checks if the process it run before (with token 12312312ddsadad31231) is still running and responds to the user with "Finished" or "Still running"
I know that I can use ProcessBuilder or Runtime.exec() to run the .exe application, but I don't know how to make the servlet leave the process running and responds.
Thanks!
Regards,
Xtian
The Process returned by ProcessBuilder is run asynchronously in the background (it's a separate process), so as soon as you call .start() you can have the servlet respond.
If you're seeing the subprocess never finish that would be because you're not reading its output, which you must do in order for the process to continue. You may want to do this in a separate background thread.
This is one of the ways to implement this:
Create a runnable that executes the command (Runtime.exec etc) (a few other things will follow)
Create an id lets say a UUID and initialize the status against the id on a persistant storage to say PENDING
Use a ThreadPoolExecutor, add the runnable to the workQueue allocated to the threadpool.
When the runnable starts executing, it may change the status in say DB to IN_PROGRESS
When the task is completed you can mark the status as COMPLETE
Now at any point of time for the request coming in you shall be able to check the status of the task and respond back accordingly.

poller process in activiti

I have created an activiti process for the lifecycle of an email which passes many service tasks.
Now I have another process that polls emails from a queue and starts the new activiti email process everytime a new email is polled.
the poller process should never stop polling.
I understand that an activiti process(the poller) is not meant to never end?
I tried to make this work, but when an error occurs in 1 of my activiti email processes, the poller process also stops.
How should this be made?
I am thinking about the REST API?
(I create my poller outside of activiti, and I call the api everytime a new email process needs to be started?)
I really want to keep everything within my activiti explorer, but is this possible?
You can set the timer for the polling process. such tat polling can happen at periodic intervals.
Do check about Activiti timer events. The timer can also be added at the process level, its not just for tasks.

Android Background Sockets

So, I am making an app, and I need to have a continuous background thread. It must not stop, the socket has to be always open, so don't please tell me to send data and close it, because I sent an auth request every time, and sometimes the app recieves data from the server. So, would AsyncTask be ok to use?
AsyncTask is used for Small fast Background task and not long Background Task for that you should use a Service or at least Thread.

Categories