Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design-Pattern for a Service like Architecture

What Design-Pattern would be intelligent in which following Components(simplified) exists:

3 Components
- GUI
- Data fetcher
- Database

Design

I have no Access to the Server in the Internet, its just a Data-Source. The Data which lays in the Internet is always the newer one, the local-Database is just a copy(cache) of the one in the Internet. The GUI can request and update of the local cache, the Service-like-component fetches then asynchronously the newest data, which could take awhile. The GUI shows only Data from the local Database, which he can fetch synchronously.

So my Question is, what classes would you use for the maybe longterm running Service with Progressbar capabilities ? Are there better Designs for this kind of "problem"? Are there better practices?

like image 875
kadir Avatar asked Sep 20 '11 08:09

kadir


People also ask

What are microservices architecture patterns?

Microservices, aka microservice architecture, is an architectural style that structures an application as a collection of small autonomous services, modeled around a business domain. In a Microservice Architecture, each service is self-contained and implements a single business capability.

What is the best design pattern for microservices?

Strangler The strangler design pattern is a popular design pattern to incrementally transform your monolithic application to microservices by replacing old functionality with a new service.

What is the design pattern used for implementing the database per service?

The most vital design pattern in Microservice Architecture is the Database per Microservice. Implementing this design pattern is challenging and needs several other closely related design patterns (Event Sourcing, CQRS, Saga).

How many design patterns are there in microservices?

When it comes to microservices, there are two main types of service discovery patterns: client-side discovery and server-side discovery.


1 Answers

On the Service Like Component:

  • An interface (method) to kick off the update process. Typically this service would return a jobId to indicate a job that is being processed in the background
  • Another interface (method) to get the status based on a particular jobId. The implementation of this service could return a status, percentCompleted or any other relevant info that will tell the caller the current status of the update process. The implementation of this method needs to be able to report an incremental progress (such as reporting the incremental store in the local storage) in order to make an accurate progress bar, otherwise the best the UI could do is show the spinning progress bar.
    • Note that if such incremental reporting is not possible to implement then the Update process should probably use a typical AsyncTask usage which execute update in the background and report to the user when it is finished. If this operation might take a while, you could implement the completion of the update via Android notification bar or push notification.

Assuming you have the interface to get the progress of the update, you could utilize the AsyncTask onProgressUpdate to report the progress of the update. The method is specifically designed for that.

Your steps are roughly as follow:

  • Execute the interface to update via one AsyncTask. Since your update is happening asynchronously, this particular task should return fairly quickly with a status reporting whether the execution is running successfully or fail because some exception along with the jobId that it is currently executing
  • Launch another AsyncTask that is pinging the status of the update and report the progress via onProgressUpdate. The AsyncTask roughly looks like

public class GetUpdateStatusAsyncTask extends AsyncTask {
    protected Integer doInBackground(Integer... param) {
        // this is the jobId that you get from the previous AsyncTask that you use to kick off the 
        // the update process
        int jobId = param[0];
        double percentCompleted  = fetchStatus(jobId);

        while(percentCompleted != 100.00) {
            publishProgress(percentCompleted);
        }

        // return status code
        return 1;
    }

    protected void onProgressUpdate(Double... progress) {
        // set the progressBar here
    }

    protected void onPostExecute(Long result) {
       // done, handle status code
    }

    private double fetchStatus(int jobId) {
        double percentCompleted = 0.0;
        // call interface to get the update based on the jobId
        // return percentCompleted status or make a more complex object if you need 
        // more detail info
        return percentCompleted;
    }
}
like image 67
momo Avatar answered Sep 21 '22 10:09

momo