This is part 2 of my first post. This is my way to efficiently handle offline struts actions and notify the status as GET API requests. Entire code is available on my Gitlab (@tony2k84). I have included the link at the bottom of the post.
Let’s begin.
Folder Structure
First, my folder structure. I have divided into JSPs, listeners, API, pojo, struts classes and core framework. I will describe each one in summary below.
Request and Response JSP
To get the concept going, I have basic action input (form) and output redirection JSPs.
Nothing fancy here. Just a simple JSP submitting the form to a struts action and struts action REDIRECTING (not just forwarding) the result to a JSP.
Web.xml and Struts Config
Rest APIs
This just gives the access to the status via APIs
The Framework
This is the main part. It has 2 components.
TaskManager
This is responsible for handing all tasks. The main responsibilities includes:
- Singleton access to the same
- Managing pending tasks
- Managing task updates
- Managing thread pool
- Synchronizing secured access to the task processing.
Task Processor
This is responsible for handling one task. This could be your business logic class to handle one task. For sake of simplicity I have considered some fake processing and some updates to the task manager.
Context Listener
This calls to handle the memory leak while starting the threads and stopping them post application load and unload.
Thats it. Above gets the things going. If we talk about the use cases for this, from the top of my head I can think of:
- Handling long running order submission for B2B orders where you cant allow the order to enter database without validation and cant let users stuck on one screen to complete.
- Handling bulk actions for a simple operation.
Part 3 will be coming soon with a React GET APIs to get the order updates. Subscribe for more.
Future extensions can include handling resubmit of tasks in case it fails. I will happy to do a POC in future if someone is interested. What are your thoughts on this concept? Feel free to comment how you will implement the same.
The entire code is hosted in Gitlab to try out @ struts-offline-actions!