java - Designing a service interface to allow both synchronous and asynchronous implementations -


not sure how describe sure, think i've boiled down want in title. elaborate, i'm looking design pattern let me have implementation of service in 1 situation return result of call synchronously in case return details on how complete call asynchronously (say job id).

maybe defining problem it's clear i'm trying breaks idea of designing interface contract. headed in wrong direction entirely.

what thinking of possibly this:

public class data {   private int id;   /* getter/setter */ }  public class queueddata extends data {   private int jobid;   /* getter/setter */ }  public interface myservice {   public data fetchdata(int id); }  public class syncedmyservice implements myservice {   private syncdao syncdao;   public data fetchdata(int id) {     return syncdao.getdata(id);   } }  public class queuedmyservice implements myservice {   private jobqueue queue;   public queueddata fetchdata(int id) {     int jobid = queue.startgetdata(id);     queueddata queueddata = createqueueddate(jobid);     return queueddata;   } }  

is sensible way go task? advice. (there's design-pattern book should reading)

this similar future pattern used in java.util.concurrent package. future represents result available in future after computation completed in separate thread. if computation complete before result required, computed value returned. else call result blocks till computation over.

so think pattern right way go having both synchronous , asynchronous services.

this how can implement solution using future:

public class data {   private int id;   private final string name;    data(string name) { this.name = name; }   public string getname() { return name; }   }  public class futuredata extends data {   private int id;   private final future<string> namefuture;    futuredata(future<string> namefuture) { this.namefuture = namefuture; }   @override public string getname() { return namefuture.get(); }   }  public interface myservice {   public data fetchdata(int id); }  public class syncmyservice implements myservice {   private syncdao syncdao;   public data fetchdata(int id) {     return syncdao.getdata(id);   } }  public class asyncmyservice implements myservice {   private static final executorservice executor =      executors.newfixedthreadpool(10);    public futuredata fetchdata(final int id) {     future<string> future = executor.submit(new callable<string>() {       public string call() {         string name;         //some long computation computes name using id given         return name;       }     });     futuredata futuredata = new futuredata(future);     return futuredata;   } } 

for quartz replace executorservice jobqueue , use quartz's equivalent of future.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -