Singleton-Objekt im Java-Web-service

Guten morgen,
Ich bin derzeit an der Entwicklung einer java web-Anwendung stellt einem web-service-Schnittstelle. Um ein globales Objekt im Speicher nutze ich die folgende Klasse als Singleton:

public class SingletonMap {
    private static final SingletonMap instance = new SingletonMap();
    private static HashMap couponMap = null;
    private static long creationTime;

    private SingletonMap() {
        creationTime = System.currentTimeMillis();
        couponMap = new HashMap();
    }

    public static synchronized SingletonMap getInstance() {
        return instance;
    }

    public static long getCreationTime() {
        return creationTime;
    }
}

Ich bin mit der obigen Klasse haben, um die gleiche Instanz von HashMap für alle threads des web-service. Die Web service-Klasse, die behauptet, die SingletonMap-Objekt ist die folgende:

@WebService()
public class ETL_WS {
    private String TOMCAT_TEMP_DIR;
    private final int BUFFER_SIZE = 10000000;
    private static SingletonMap couponMap;
    private static SingletonProductMap productCategoryMap;
    private String dbTable = "user_preferences";

    public ETL_WS() {
        Context context = null;
        try {
            context = (Context) new InitialContext().lookup("java:comp/env");
            this.TOMCAT_TEMP_DIR = (String) context.lookup("FILE_UPLOAD_TEMP_DIR");
        }catch(NamingException e) {
        System.err.println(e.getMessage());
    }

    public long getCouponMapCreationTime() {
        return couponMap.getCreationTime();
    }

}

Den Grund habe ich die Methode getCouponMapCreationTime() ist zu prüfen, ob alle threads des web-service-Zugriff auf das gleiche Objekt. Ist die obige Vorgehensweise korrekt? Wie etwa performance-Overhead? Glaubst du, ich brauche die Singleton-Eigenschaften, oder könnte ich nur eine statische HashMap für alle threads? Wenn ich eine statische HashMap, wird es von der garbage Collection freigegeben, wenn keine Threads aktiv ist?

Vielen Dank für Ihre Zeit.

InformationsquelleAutor nick.katsip | 2012-06-19

Schreibe einen Kommentar