To be honest, the problem is simple and clear. you need to use the PayPal API to obtain transaction information. Fortunately, PayPal mentioned this. net Development SDK, you only need to configure the relevant parameters to use. by reading the SDK, we found that two interfaces are required. One transactionsearch interface is used to obtain the list of transaction codes. This is the unique ID of the transaction, but the information returned by this interface is limited. to obtain more detailed information, you need to use another interface transactiondetails. The parameter of this interface is the transaction code. in general, the first interface gets the transaction code list, and the second interface is like getting detailed information from the list point. NET Enterprise developers do not have to think about what I'm talking about.
In the face of this demand, the key points first come to mind
Key Aspect 1: transaction records are generated at any time. Each transaction record must call an API to obtain detailed information. If necessary, a program is run, there will be more and more transaction records. I don't know if tens of thousands of requests submitted to PayPal will be blocked in a short time. therefore, it is best to ensure that the program can run continuously and call the API without stopping too many calls at a time. in addition, the PayPal share server is abroad, and the API is based on the HTTP protocol, the latency of each call is quite high, and transaction information is obtained discretely, this avoids the need to wait for one or two hours.
Key Aspect 2: The API used to obtain the transaction code list is assumed that 100 requests can be obtained each time, and 100 API calls can be made to obtain detailed and clear details. This time, the time consumption is quite time-consuming, and multithreading is obviously used, each thread calls transactiondetail once, which can reduce the waiting time and speed up
With the above tone, I basically determined what the first version of the program is like. I made a winform program and got a button. After the program runs, I clicked the button to capture the transaction information, then the system tray is minimized to show that the program is still running. when the program is running, a thread will be in the loop state and execute the call to obtain the list. Once the transaction code is captured, multiple threads will be created to obtain detailed information. After processing, the thread will sleep for 1 hour, this loop.
From getting the task to making this design, it is obviously a correct direction. Certainly, data is available, but I have not started to write code. I need to take the time to design in detail and hope to discover potential problems during the design process.
Question:
1. thread crashes and the thread that executes transactiondetails crashes due to exceptions, such as network timeout. if all the threads executing transactiondetails crash and the list of obtained transaction codes is not processed, will the program be stuck here.
Solution:
To avoid this situation, the thread that executes transactionsearch must be created at the same time and all the threads that execute gettransactiondetail must be executed all the time. the thread that executes transactionsearch becomes the producer, and the thread that executes gettransactiondetail becomes the consumer, turning this problem into a producer and consumer problem.
The second edition is designed to solve this problem. It is still the winform program and runs in the same way. when the program is running, a producer thread will be created to call transactionsearch to create three threads to call transactiondetails. The transaction code obtained by the producer is written into a queue. The consumer obtains the transaction code from the queue and calls the API to obtain detailed information, write data to the database. google and Baidu can solve the synchronization problem between producers and consumers.
Project Structure
At this time, I plan to start writing code, because at the moment I really don't think of any problems that will make the program unable to run. So I created the project and completed the module division.
The API interface of paypalapi is classified into the paypalhelper class. This class has two static methods: encapsulation of transactionsearch and transactiondetails.
Database access only involves simple operations.. This part of the function is mainly to write the transaction code and transaction record details into the database, and even hard coding is no problem.
Add a model to indicate transaction details. transactiondetail is used to exchange information in each module.
Add an executor class to indicate the execution part.Used to encapsulate creation and synchronization of consumer and producer threads
Add a winform as the main interface and call executor
Although there are not many functions, the idea of stratified modules should always be implemented. I know how to do it. With the detailed module division, coding is actually a very mechanical action. after some time writing and debugging, you can finally run it. after the operation, the transaction record slowly stores the data volume one by one,
Version 2
Problem: After running for a period of time, it is found that the transaction record is different from the transaction record seen in the PayPal background. Some transaction records are missing in the middle, and the transaction record is within the same period of time.
Analyze problems: I found that the problem is that the producer has sleep for one hour. Considering that the sleep time for one hour is because I don't like the program to constantly access the PayPal API, the problem is, if the captured data processing is completed for more than one hour, the producer thread needs to wait for one hour before capturing the data. Therefore, the transaction record for a period of time is missing.
Solution: It is obvious that the execution of the producer thread is affected by the processing speed of the consumption thread. Therefore, the producer's field must be separated from the consumer thread. Why not create a thread? This thread only executes transactionsearch, then, the transaction code is stored in the database and then sleep for 1 hour. The previous producer thread obtains the transaction code from the data. After the consumer thread processes the data, it updates the database to identify that the transaction code has been processed. in general, a thread is added to obtain the transaction code list, while the mode of the producer and consumer threads remains unchanged.
The second edition was launched soon. After running on my computer for a whole day, I checked the data and found that the problem was solved. at this point, I quickly thought that I could not think of any problem that would interfere with the correctness of the program, so I was preparing to run the Remote Desktop Connection server, the server runs normally without any problems, so I quit. After two hours, I went to check the database and found that there was no data. It was so strange that I boarded the server, I found that the system tray was missing and I couldn't see the process when I opened the task manager. I checked the logs and found no exception records. so I am angry that the guy turned off my program. after I re-run it, I quit the Remote Desktop. Then I went to the office and asked the guy why he didn't get the result. Then I checked out the program again !!! I realized that it may still be a program problem. I searched and found it was a remote desktop problem. After the Remote Desktop user session ends, all the programs of the user will be terminated.
Third Edition
Analysis Problem: Generally, desktop applications have users, and their computers have been logged on. Therefore, I have not been able to test it. The programs running on the server have no users, the program running through the Remote Desktop will end after exiting the desktop. what programs can be run without logon on windows? Windows service !!
Now, I want to program my program into a Windows service. honestly, I have never written about services before,. it is not difficult to create a server under. net. baidu went over and found a blog about how to create and install the Windows service. To create a service-type project, put the code in the specified interface. In fact, in the second version, the program entry is under winfom. You only need to remove winform and transfer the code to the service entry. the third edition soon came out. After the service was installed, it ran normally and the data finally went normal.
Version 4
New requirements are coming. They need to customize the time for obtaining the transaction code. from the third-Edition program, we can see that the thread in the program obtains the transaction code every hour after the program starts running. in fact, the transaction records of PayPal are generated regularly, and there are obviously many transactions in half a day, resulting in fewer transactions at night.
Therefore, it is best to develop a more reasonable task plan to obtain the transaction code list.
Analyze problems: At that time, I felt dizzy. I had never done such a detailed task scheduling control before. The only thing I could think of was to use a scheduled task, which may lead to splitting the program into two. the function of obtaining the transaction code list is used as a separate program and then scheduled tasks are used for execution.
Solution: The first method to come up with is to use scheduled tasks. The problem is that the two programs run separately. I don't think it makes any sense to split a program with few functions. So I searched the internet for a clear and simple purpose, which is to find a task scheduling component, if you do not develop one by yourself, you will have to vomit blood. sure enough, I soon discovered quartz. net. For more information, see quartz. net introduction and documentation later found enough to meet the needs, but there is a little worry, so I wrote a demo to test quertz. net. After the test, I decided to introduce the project.
The fourth version is coming soon. the change is to cancel the thread for obtaining the transaction code list and convert it to quartz. net to control the time for obtaining the transaction code. the producer and consumer threads for transaction code processing remain unchanged.
Further Improvement
First, the service runs at the end without a GUI. second, the number of producer and consumer threads in the program is fixed, and the sleep time is also fixed. you can consider a GUI program to start and stop the service and configure the number of producer and consumer threads and the waiting time.