How to update a user attribute automatically when multiple conditions are reached in java?

problem description

how do I update a user"s attribute automatically when multiple conditions are reached in java?

the environmental background of the problems and what methods you have tried

the current implementation is to insert the code that updates the attribute where the detection of each condition occurs, so it feels like a lot of coupling, is there any way to decouple it?
now uses message queuing, too. When a condition is reached, a queue is sent to call the method to update the property.

what result do you expect? What is the error message actually seen?

have thought about using AOP, but want to know what is the common practice in the industry

pseudo-code:

@Data
class User{
    private Long id;
    ...
    private Integer level;
    private Integer num1;
    private Integer num2;
}

when num1 > = 50 and num2 > = 100, levelPP, it is best to add and subtract level--, num1 and num2 in two separate service, let"s call it num1Service and num2Service

Jun.28,2022

can be solved with the idea of state machine. The four states are represented by the binary bit of 2bit. 00 indicates that none of the conditions are satisfied, 10 indicates that the first condition is satisfied, 01 indicates that the second condition is satisfied, and 11 indicates that all conditions are satisfied. Define four events: meet_evt_1 reach condition 1, unmeet_evt_1 lose condition 1, meet_evt_2 reach condition 2, unmeet_evt_2 lose condition 2, define two actions, act_1 means levelPP, and act_2 indicates that the state transition diagram of level--, is as follows:

clipboard.png

as for the state machine implementation, there are many open source versions, and Spring also has a state machine implementation, see http://blog.didispace.com/spr.... It's relatively simple.

above, if there are only two conditions, if the number of conditions is increased to 3, then the number of states will also increase to 8. If there are more conditions, the state machine will undoubtedly become complex, but you can refer to the idea of the state machine for coding. After all, there are only two actions and few events.


can implement a state transition class State (OPEN- > HOLD- > EXECUTING- > DONE- > CLOSED), and then publish and subscribe to that state information. For example, if An executes a task and finally reaches the CLOSED state, then B monitors the state (B optionally listens to the desired state) and obtains information to execute certain logic. In fact, this is somewhat similar to the publish and subscribe model implemented with the help of Zookeeper.

Menu