java - How to assign an object retrieved from API REST to a singleton Class? -


i have singleton object , need assign object returned api rest, , nothing modified when it.

singleton:

public class stations {    private static stations instance = null;    private list<station> stations;    private stations() {       // exists defeat instantiation.    }    public static stations getinstance() {       if(instance == null) {          instance = new stations();       }       return instance;    }    public list<station> getstations(){        return this.stations;    } } 

the call:

public class stationscall implements job {     private stations stations = stations.getinstance();     client client = clientbuilder.newclient();     public void execute(jobexecutioncontext context) throws jobexecutionexception {          webtarget targetget = client.target("http://wservice.viabicing.cat/v2/stations");         this.stations = targetget.request(mediatype.application_json_type).get(new generictype<stations>(){});     }     public list<station> getstations(){         list<station> aux = this.stations.getstations();         return aux;     } } 

calling this.stations = targetget.request(...) modify field stations of class stationscall, doesn't modify actual singleton, should not able create instance of stations since constructor private.

what need setter in singleton set current list of stations.

something this:

public class stations {     // use atomicreference make sure threads see last list of stations     private final atomicreference<list<station>> stations = new atomicreference<>();     private stations() {         // exists defeat instantiation.     }     public static stations getinstance() {         // lazy create instance of stations using static inner class         // thread safety         return stationsholder.instance;     }     public list<station> getstations(){         // last list of stations atomicreference         return this.stations.get();     }      public void setstations(list<station> stations) {         // set new list of stations , make unmodifiable thread safety         this.stations.set(collections.unmodifiablelist(stations));     }      private static class stationsholder {         private static final stations instance = new stations();     } } 

nb: improved code make thread-safe singleton has chance used concurrent threads.

then class stationscall be:

public void execute(jobexecutioncontext context) throws jobexecutionexception {     ...     stations.getinstance().setstations(targetget.request(...)); }  public list<station> getstations(){     return stations.getinstance().getstations(); } 

assuming need ability current list of stations centralized access , don't care if singleton or not, code should rather be:

public class stations {     // use atomicreference make sure threads see last instance     private static final atomicreference<stations> instance =          new atomicreference<>(new stations());     private list<station> stations;      public stations() {     }      public stations(final list<station> stations) {         // make safe , unmodifiable copy of list of stations         this.stations = collections.unmodifiablelist(new arraylist<>(stations));     }      public static stations getinstance() {         return instance.get();     }      public list<station> getstations(){         return this.stations;     }      public static void setinstance(stations stations) {         // set new instance         instance.set(new stations(stations.getstations()));     } } 

then class stationscall be:

public void execute(jobexecutioncontext context) throws jobexecutionexception {     ...     stations.setinstance(targetget.request(...)); }  public list<station> getstations(){     return stations.getinstance().getstations(); } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -