Running a task asynchronously on Weblogic - java

I'm developing a web application and one of it's features is to send email notifications once the form is submitted. I wonder what's the easiest/best way of sending them asynchronously. I'd like to get the response from the server instantly and invoke an email sending job in the background not waiting for it to finish.
Thanks

You can use JMS to send mails asynchronously.
Here is a question about this befure
You can look at thtis link fopr simple JMS Queue implemantation

Related

JMS with Consumer performing long running process (10mins)

I have a scenario in Spring boot applications - where I will be getting requests from other applications via REST "Run Process" Service and these will be placed on MQ queues. Then consumers will process one by one and the consumer calls another REST "Initiate Request" service which will take around 10mins to get a response back. I am looking for ideas/solutions where I can fire the REST "Initiate Request" service and forget then stop the consumer. Once "Initiate Request" completes its processing, the system will send an event notification indicating this process has completed/failed. Based on this I would like to proceed with the next Queue item. Is there a way to Stop and Start consumers based on notification to avoid long-running threads? If you have come across this problem, let me know how you have resolved it.
There are other solutions like
Consumers to persist the data to database and process row by row.
Using webflux we can avoid JMS-Consumer thread but not consumer calling REST

Architecture of Java Servlet Browser push notification

I am implementing sending of browser push notifications via Google Cloud Messaging and Firefox Push Notification System. For this, we have to make HTTP Post requests to GCM and FPNS.
To make HTTP request to GCM/FPNS we should have user registration IDs. Using JavaScript we are collecting registration IDs and storing it in Cassandra. Each record contains user registration information (Registration ID and browser type).
When we make an HTTP request to GCM/FPNS we should send registration IDs along with the request to GCM/FPNS based on browser type (if user registration ID belongs to Chrome we will make GCM request otherwise FPNS request). For example, if we have 10,000 records we should make around 10,000 requests to FPNS/GCM.
Once GCM/FPNS receives the user registration IDs, it will send a push notification to the browser. In browser, we have JavaScript code (Service Worker) to handle the notification event.
For above requirement, synchronous servlet architecture is not good enough. Because to process 10,000 records, it may take assuming 10 to 15 minutes, even if we are using multithreading. It may cause tomcat memory leakage and an out of memory exception.
When I was searching online, people are suggesting asynchronous servlet architecture. Once we take the request from the client to send the notification we will have respond immediately (something like 200 Ok Added to queue) and also this request should be added to Message Queue (JMS). From JMS we use multithreading to make asynchronous HTTP requests.
I am not finding the correct way of doing this. Can you suggest a way of implementing this functionality (Architecture Design and control flow)?
Short of changing to something like PubNub, I would create a worker queue. This could be done with JMS or just a shared Queue (search for producer/consumer). JMS would be, in my opinion, the easiest though it gets harder to distribute in a cluster.
Basically you could continue to have a synchronous servlet - it would take the message, put it on the queue, and return the 200. Placing a message on the queue would have very minimal blocking - a couple of milliseconds at best.
As you indicated, on the queue consumer side you would then have to handle many requests. Depending on the latency requirements of your system you may need to thread or off load that. It really depends on how fast you need to send the messages.
For a totally different architecture, you could consider a "queue in the cloud". I've used Amazon SQS for things like this. You wouldn't even have a servlet - the message would go straight to SQS and then something else would pull it off and process it.
For reference I don't work for Amazon or PubNub.

Regarding schedule time for sending email

Need to schedule time for sending email via javamail on Google app engine, I already tried OOP MailScheduler for this but at the end I am getting error due to it does not support for Google App Engine, it only support to Tomcat, is there any other efficient way to schedule the time for sending emails.
When you create a task and add to the task queue, you can specify when this task should be executed.

Schedule emails to deliver later in a web application

I need my web application to send thousands of per day. Currently, on being triggered by front-end user, i run a for loop in back-end which send mails.
Read this: How can I send an email by Java application using GMail, Yahoo, or Hotmail?
Issues:
If someone closes the web page after sending request, request is incomplete.
It takes long time to send email and user has to wait.
Ideally, i would like to save the email and content in database from where scheduler picks up the job and send email and notify the user once mail is sent/any issue occurs.
How can i achieve this? Will cron jobs help here? What if application is deployed on windows server? Or shall i write a separate java application to pick up the tasks from database and send mail? Can i schedule a job in web application?

Grails App with java service and JMS

I'm starting out with Grails and want to build a sample application.
Below is the flow of the application I'm envisioning. I'll follow up with questions.
The flow of the app:
User uploads a file
controller gets the file and just sends a response back saying "uploaded"
File is put in a JMS queue
Java service running separately fetches the file from the queue and processes it (just reads the first word)
Java service puts the response back (where does it put the response?)
Grails App will read the response and present it to the user
Questions
Where does the java service put the data after reading the file?
How does the grails app read the data put by the java sevice?
Is there something missing from my understanding? I plan to use grails jms plugin and ActiveMQ
Can something be improved in this architecture? This is a prototype I'm putting together for a bigger application.
I would really appreciate any articles/tutorials on an example of a simple app like the one above...?
In you case JMS is used in a synchonous way, so it depends on your JMS provider if you can do this. If the JMS provider is able of doing synchronous communication you put the answer after the file processsing into a reply queue.
In the synchronous JMS way, the java service will wait for a response from the JMS provider so can can present the response from the service back to the controller and then to the user...
So..:
User uploads a file
controller gets the file and just sends a it to the JMS queue and waits for response!
Java service running separately fetches the file from the queue and processes it (just reads the first word)
Java service puts the response back in a reply queue
Controller wil get the response reads the response and present it to the user
Your page could be a nice ajax page that presents the user with a processing spinner.

Categories