简体   繁体   中英

Which Erlang behaviour i.e. gen_server or gen_fsm should I use in this case

I am developing a solution that queries a SOAP web service for certain transactions. Once retrieved, these transactions are meant to be saved in a database after which a callback url is invoked to send some data to another server. How would you best architect a solution to this problem. My point of confusion is whether gen_server or gen_fsm should be used and if so which component of the solution goes where ie if gen_server what task goes to the server which task goes to the client.

Think about what tasks can happen in parallel in your system. Can the SOAP queries be done in parallel, and does it make sense? Can the transactions be written to the database while SOAP is being queried again? Is the data to another server supposed to be sent after the transactions are written to the database, or can it be done at the same time?

Once you know these answers, you can build your pipeline. One example would be:

  • One process that regularly queries the SOAP service and calls a function with each batch of transactions.
  • In this function, two processes are started; one that writes the transactions to the database and one that sends data to the server. These processes are independent of each other.

This is just one example, and depending on your requirements, you might have to structure things another way.

In general, these processes can all be gen_servers as none of them have any clear states. In fact, the two worker processes doesn't have to be gen_servers at all, since they just do one task and then die. Using a gen_server in that case would be overkill.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM