A method that uses multithreading to solve the problem of intermittent triggering in different time zones of multiple businesses.
1. Background
The project needs to solve the following problem: two services must be triggered every few seconds, but both services only perform this action within the specified time range. For example, during the period from AM to AM, the two businesses must be executed every few seconds, and work stops after work.
2. Ideas
At first, I wanted to use timer to solve this problem. That is, I made two timers, one timer to control one transaction, and then executed in the corresponding time zone of each transaction.
However, considering that Timer is executed in the main Thread and Thread is executed in its own Thread, the efficiency of using Thread in multi-core and multi-Thread CPUs is higher. In addition, as the number of transactions increases in the future, the control is more complex, and timer is not very conducive to expansion. So we finally decided to use multithreading to solve this problem.
After thinking about and referring to similar cases, such as explain:
3. Implementation of IshedulerJob 3.1
3.2 Job
3.3 SchedulerConfiguration
3.4 Scheduler
3.5 viewer
4. Optimization
A. Both transactions involve the addition, deletion, query, and modification of multiple data in the database. To ensure data uniqueness, transaction operations are performed on the database.
B. To avoid repeated connections and shutdown of the database, try to use persistent connections of the database here. At the same time, in order to avoid the problem that two transactions share one database connection, we specially use a simple database connection pool for processing.
C. In order for the connection pool to be shared, perform the singleton mode for the class in which the connection pool is located.